繁体   English   中英

使用LME进行模型预测时出错

[英]Error in model prediction with lme

我正在尝试根据我的lme模型做出预测:

mod<-lme(height~direction+time+distnest+loop+twc+twc:direction,random=~1|bird_id/FT_no,data=dat,correlation=corAR1(0.5))      

不幸的是,我有一个嵌套的随机和5种固定效果,并且并不真正知道如何处理所有这些效果。

除“方向”和“时间”这两个级别的因素(“陆地”或“海洋”;“白天”或“夜晚”)外,所有固定效果都是数字形式的。

对于模型预测,我尝试了以下方法:

newdat<-data.frame(loop=seq(min(dat$loop),max(dat$loop),length=100),
               direction=factor("land",levels(dat$direction)),
               time_code=factor("1",levels(dat$time)),
               distnest=mean(dat$distnest),
               twc=mean(dat$twc))

newdat$pred<-predict(mod,newdata=newdat,level=0)
plot(dat$loop,dat$height,pch=16,las=1,cex.lab=1.2)
lines(newdat$loop,yhat,lwd=2) ### for plotting one of the fixed effects
newdat$predse<-predict(mod,newdat,se.fit=TRUE)$se.fit

但是,当使用se.fit = TRUE时,会出现错误:“ predict.lme(mod3,newdat,se.fit = TRUE)中的错误:无法在'newdata'上评估所需级别的组”

se.fit是否不适用于lmes? 忽略级别= 0无效。 代码是否错误?

我还尝试了glmm.wikidot中的代码:

newdat<-expand.grid(direction=c("land","sea"),time_code=c("1","2"),loop30=c(0.08,1),distne    st=c(0.01,99.43),twc=c(-56.88744,57.93735))
newdat$pred<-predict(mod3,newdat,level=0)
Designmat <- model.matrix(eval(eval(mod3$call$fixed)[-2]), newdat[-ncol(newdat)]) 
predvar <- diag(Designmat %*% mod3$varFix %*% t(Designmat)) 
newdat$SE <- sqrt(predvar) 
newdat$SE2 <- sqrt(predvar+mod3$sigma^2)
library(ggplot2) 
pd <- position_dodge(width=0.4) 
g0 <- ggplot(newdat,aes(x=loop30,y=pred,colour=direction))+ 
geom_point(position=pd) 
g0 + geom_linerange(aes(ymin=pred-2*SE,ymax=pred+2*SE), position=pd)
## prediction intervals 
g0 + geom_linerange(aes(ymin=pred-2*SE2,ymax=pred+2*SE2), position=pd)

但是情节是完全错误的。 谁能帮助我提供正确的代码? 非常感谢。

最好的祝福,安娜

为了进行预测,您需要模型的RHS中的所有项目... _as_named_:

direction + time + distnest + loop + twc 
...and...  bird_id ...and... FT_no

目前,您已将time变量重命名为time_code并且未能包含任何“随机效应”变量。 这是nlme :: lme帮助页面中的相关内容:“固定和随机效应模型中使用的所有变量以及分组因子必须存在于数据框中。如果丢失,则返回拟合值。”

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM