R语言中变量长度不一样的问题

问题遇到的现象和发生背景

本人在进行成分数据的分析,跟着本网站的一个代码教程写的。前提是每日的时间总和是一定的,但是现在到最后两步遇见了问题。即回归方程已经建立,在用均数代入方程进行预测时出现问题。

遇到的现象和发生背景,请写出第一个错误信息

Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) :
变数的长度不一样('b')

用代码块功能插入代码,请勿粘贴截图。 不用代码块回答率下降 50%
attach(mydata)
library(compositions)
comp<-cbind(sleep,x1,SED,LPA,MPA,VPA)
comp<-acomp(comp)
comp.mean<-mean(comp)
lm    <- lm(BMI    ~ ilr(comp)    + Age3)
mean.pred    <- predict(lm,    newdata=list(comp=comp.mean,    Age3=mean(Age3)))
运行结果及详细报错内容
> mean.pred    <- predict(lm,    newdata=list(comp=comp.mean,    Age3=mean(Age3)))
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  变数的长度不一样('Age3')
> table(comp.mean)
comp.mean
0.0063532728884529 0.0243705383508746 0.0296751798496391 0.0528902119744366 
                 1                  1                  1                  1 
 0.361325194554032  0.525385602382565 
                 1                  1 
> table(Age3.mean)
Age3.mean
29.48738170347 
             1 

我的解答思路和尝试过的方法,不写自己思路的,回答率下降 60%

这是之前借鉴的代码。

#    load    the    required    R    package,    Compositions    
library(Compositions)
#    make the    composition by    binding    the    components    together
comp    <- cbind(sleep,SED,LPA,MVPA)    #variables are    in    min/day
#    tell    R    that    comp is    a    compositional    variable
comp    <- acomp(comp)
#    make the    ilr    multiple    linear    regression    model.    BF    represents    %body    fat.    # ilr()    is    the    default    isometric    log    ratio    transformation    included    in    the    
#  Compositions    package.
lm    <- lm(BF    ~ ilr(comp)    + ses)    #ses, an    example    covariate
#    determine the    mean    composition
comp.mean    <- mean(comp)    
#    because    comp    has    been    designated    as    a    compositional    
# variable,    R    calls    on    mean.acomp()    to    calculate the    
# compositional    mean    (i.e.,    geometric    mean of    each    component,
# then adjusted    so    all    components    sum    to    1). The    mean    is    
# therefore    expressed    in    proportions.
#    predict %BF    for    the    mean    composition from    above,    keeping    ses    
# constant    at    its    mean.
mean.pred    <- predict(lm,    newdata=list(comp=comp.mean,    ses=mean(ses)))    
#    next, construct a    new    composition,    where    30    min of    sedentary    
#time    have    been reallocated    to    sleep. However,    here    30    min    must    
#be    expressed    as    a    proportion    (remember,    from    above,    that    
#mean.acomp()    adjusts    the    mean    to    proportions).
#    The    reallocated    time    is    therefore    30/1440.
new.comp<- acomp(comp.mean+c(30/1440,-30/1440,0,0))    
#    Now,    we    predict    %BF    for    the    new    composition (new.comp),    
#keeping    ses    constant    at    its    mean.
pred    <- predict(lm,    newdata=list(comp=new.comp,    ses=mean(ses)))
#    Finally,    the    estimated    difference    in    %BF    for the    above    time    
#reallocation    is (Equation    4):
pred- mean.pred

我想要达到的结果,如果你需要快速回答,请尝试 “付费悬赏”

想问一下这个问题怎么可以解决?

有没有完整的代码?

R语言报错 变数的长度不一样,需要改成元素自变量对应的名称。在公式中如果用了.来表示对应变量的名称,那么就会报错,需要改成元素自变量对应的名称。
比如这个例子中:

https://blog.csdn.net/weixin_46623488/article/details/120222879

问题原因是在使用predict函数预测值时,newdata的变量名称和原始数据中的变量名称不一致。在预测时,newdata中的变量名称应该与原始数据中的变量名称相同。在这里,应该将newdata中的变量名称改为comp,而不是b。

将代码修改为:
mean.pred <- predict(lm, newdata=list(comp=comp.mean, Age3=mean(Age3)))
这样就可以避免变量长度不一样的错误。