kjhla 2023-01-17 21:58 采纳率: 0%
浏览 150
已结题

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

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

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

  • 写回答

3条回答 默认 最新

  • heart_6662 2023-01-17 22:23
    关注

    有没有完整的代码?

    评论

报告相同问题?

问题事件

  • 系统已结题 1月25日
  • 修改了问题 1月19日
  • 修改了问题 1月17日
  • 修改了问题 1月17日
  • 展开全部

悬赏问题

  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?
  • ¥20 用户端如何上传图片到服务器和数据库里
  • ¥15 现在研究生在烦开题,看了一些文献,但不知道自己要做什么,求指导。
  • ¥30 vivado封装时总是显示缺少一个dcp文件
  • ¥100 pxe uefi启动 tinycore
  • ¥15 我pycharm运行jupyter时出现Jupyter server process exited with code 1,然后打开cmd显示如下
  • ¥15 可否使用carsim-simulink进行四轮独立转向汽车的联合仿真,实现四轮独立转向汽车原地旋转、斜向形式、横移等动作,如果可以的话在carsim中如何进行相应设置
  • ¥15 Caché 2016 在Java环境通过jdbc 执行sql报Parameter list mismatch错误,但是同样的sql使用连接工具可以查询出数据
  • ¥15 疾病的获得与年龄是否有关