【发布时间】: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),它确实有效 -
谢谢巴斯!我知道这一定很简单。