2013-12-16 21 views
1

模型預測我想從我的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'上評估組所需的級別「

是否semesi適用於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) 

但情節是完全錯誤的。任何人都可以幫我使用正確的代碼嗎?提前致謝。

最良好的祝願,安娜

+1

沒有一些實際的數據,很難。 –

+1

'predict.lme'沒有'se.fit'參數。但是,傳遞一個沒有任何影響。錯誤是由於省略了'level = 0'而沒有爲隨機截取提供分組變量。第二種方法的錯誤描述(「情節完全錯誤」)不允許診斷。 – Roland

回答

0

對於預測你需要的所有模型中的RHS的項目... _as_named_:

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

此刻你已經名不副實了time變量time_code和未能包含任何「隨機效應」變量。以下是nlme :: lme幫助頁面的相關位:「固定和隨機效應模型中使用的所有變量以及分組因子必須存在於數據框中,如果缺失,則返回擬合值。」