【问题标题】:How to fix Error in rep(0, nobs) : invalid 'times' argument when using predict function for model plots如何修复 rep(0, nobs) 中的错误:对模型图使用预测函数时,“次”参数无效
【发布时间】:2021-03-26 22:37:24
【问题描述】:

我正在尝试绘制 No_Squares 和 Sex 之间的交互以及它们对 Active_co2 的影响:


AMRdata <- structure(list(Week = c(1L, 1L, 2L, 3L, 3L, 3L, 3L, 4L, 5L, 5L, 
6L, 7L, 7L, 7L, 7L, 8L, 1L, 2L, 2L, 3L, 3L, 4L, 6L, 6L, 8L, 8L, 
8L, 8L, 9L, 9L, 9L, 10L), Sex = structure(c(1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("F", 
"M"), class = "factor"), No_Squares = c(23L, 
17L, 14L, 7L, 99L, 78L, 90L, 1L, 9L, 35L, 81L, 9L, 77L, 84L, 
1L, 44L, 9L, 30L, 8L, 92L, 28L, 74L, 29L, 76L, 66L, 43L, 36L, 
13L, 4L, 82L, 14L, 59L),  Active_co2 = c(8.79514591, 16.71840387, 14.1932374, 
    10.90741585, 10.7436911, 14.97469781, 19.88267242, 12.43274774, 
    15.12038794, 10.43636012, 15.59780954, 8.776376951, 9.995133069, 
    12.38314719, 9.611533444, 9.633809968, 12.56430759, 10.29433452, 
    9.422792731, 22.5092972, 10.38682245, 8.248907506, 11.84916117, 
    11.05467852, 19.53495917, 12.14440531, 12.09564168, 6.78392472, 
    10.51570692, 8.527792046, 8.731880804, 10.71404367)), class = "data.frame", row.names = c(NA, 
-32L))

mod1 <- glmer(Active_co2 ~ No_Squares*Sex+(1|Week),
              data = AMRdata, family=Gamma(link='log'))

plot(AMRdata$No_Squares,AMRdata$Active_co2,type="n",xlab="No_Squares",ylab="AMR")

spp<-split(AMRdata$Active_co2,AMRdata$Sex)
bio<-split(AMRdata$No_Squares,AMRdata$Sex)

points(bio[[1]],spp[[1]],pch=16)
points(bio[[2]],spp[[2]],pch=17)


# make legend 
legend("topright",
       title="Sex",
       legend=c("female","male"),
       pch=c(16,17,1),lty=c(1,2,4),bty="n")


NEWSQUARES<-seq(1,99,length=100)
levels(AMRdata$Sex)

FACTORfemale<-rep("F",100)
PREDfemaleAMR<-predict(mod1,list(Sex=factor(FACTORfemale),No_Squares=NEWSQUARES),type="response",se=TRUE)

但是在最后一点,我收到了错误消息


Error in rep(0, nobs) : invalid 'times' argument

我上网查看过,但无法解决问题。任何关于我最后一段代码有什么问题的建议将不胜感激!

【问题讨论】:

  • 我认为您的列表中缺少 Week 变量,并且可能该列表应该是 data.frame 。如果你通过data.frame(Sex=factor(FACTORfemale),No_Squares=NEWSQUARES, Week = 1),它确实有效
  • 谢谢巴斯!我知道这一定很简单。

标签: r predict lme4


【解决方案1】:

您可能需要一个data.frame,其中还包括"Week",我不完全知道您想要哪些值。不幸的是,se=TRUE 在此方法中是未使用的参数。

predict(mod1, data.frame(Week=1:10, Sex=factor(FACTORfemale), No_Squares=NEWSQUARES),
        type="response"
        # , se=TRUE  ## unused argument
)
# 1        2        3        4        5        6        7        8        9       10       11       12 
# 11.91329 11.67287 12.21171 11.29497 11.74180 11.73457 11.04548 11.78727 11.33035 11.55541 12.10291 11.85865 
# 13       14       15       16       17       18       19       20       21       22       23       24 
# 12.40607 11.47474 11.92869 11.92134 11.22128 11.97488 11.51069 11.73933 12.29554 12.04740 12.60353 11.65738 
# 25       26       27       28       29       30       31       32       33       34       35       36 
# 12.11855 12.11109 11.39988 12.16548 11.69390 11.92618 12.49124 12.23915 12.80414 11.84292 12.31143 12.30385 
# 37       38       39       40       41       42       43       44       45       46       47       48 
# 11.58133 12.35911 11.88002 12.11600 12.69005 12.43395 13.00793 12.03141 12.50738 12.49968 11.76566 12.55582 
# 49       50       51       52       53       54       55       56       57       58       59       60 
# 12.06911 12.30884 12.89203 12.63186 13.21497 12.22291 12.70645 12.69863 11.95292 12.75566 12.26120 12.50475 
# 61       62       63       64       65       66       67       68       69       70       71       72 
# 13.09723 12.83291 13.42530 12.41745 12.90869 12.90075 12.14317 12.95869 12.45635 12.70378 13.30569 13.03716 
# 73       74       75       76       77       78       79       80       81       82       83       84 
# 13.63898 12.61509 13.11415 13.10608 12.33644 13.16494 12.65461 12.90598 13.51746 13.24466 13.85607 12.81588 
# 85       86       87       88       89       90       91       92       93       94       95       96 
# 13.32288 13.31468 12.53280 13.37448 12.85603 13.11139 13.73261 13.45547 14.07660 13.01986 13.53493 13.52660 
# 97       98       99      100 
# 12.73227 13.58735 13.06065 13.32008

【讨论】:

    猜你喜欢
    • 2021-02-05
    • 2020-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-23
    • 1970-01-01
    相关资源
    最近更新 更多