R: lme, cannot evaluate groups for desired levels on 'newdata'

二次信任 提交于 2021-02-11 15:25:33

问题


I'm running an lme -model from the package nlme on R v.3.0.2. I'm trying to extract the model estimates with predict.lme, but it returns an error. Here's the code to replicate the error:

my.model = lme(fixed = Maxi ~ Time*Origin, random = ~ 1 |Genotype, method = "REML", weights=varPower(), data=dd)
new.my.model <-  data.frame(Origin = c("Ka", "Ka", "La", "La"), Time = c("mor", "eve", "mor", "eve"))
predict(my.model, new.my.model, level = 0:1)
#Error in predict.lme(my.model, new.my.model, level = 0:1) : 
#cannot evaluate groups for desired levels on 'newdata'

and here's the data:

dd<-read.table(text="Origin Genotype  Time Maxi
Ka    Ka1     mor 14,59
Ka    Ka1     eve 13,42
Ka    Ka11    mor 14,08
Ka    Ka11    eve 16,29
Ka    Ka15    mor 14,38
Ka    Ka15    eve 14,56
La    La1     mor 17,82
La    La1     eve 13,28
Ka    Ka1     mor 16,44
Ka    Ka1     eve 15,52
Ka    Ka15    mor 13,76
Ka    Ka15    eve 13,55
Ka    Ka1     mor 19,15
Ka    Ka1     eve 19,12
La    La6     mor 10,54
La    La6     mor 11,38
La    La6     eve 10,48
Ka    Ka15    mor 15,25
Ka    Ka15    eve 16,51
La    La1     mor 17,46
La    La1     eve 15,57
Ka    Ka1     mor 16,83
Ka    Ka1     eve 15,63
Ka    Ka15    mor 14,54
Ka    Ka15    eve 15,09
La    La1     mor 11,3
La    La1     eve 11,94", header=TRUE, dec=",")

Data is two factors (Ka, La) with two levels each (mor, eve) and a random effect. I'm trying to extract the estimates for the 4 levels of data, i.e. Ka,mor ; Ka,eve ; La,mor ; La,eve.

First asked over at CV in another context, but I was recommended here.


回答1:


If you want to ignore the random component, then setting levels=0:1 is not correct; you should just use levels=0.

new.my.model <- expand.grid(Origin=c("Ka","La"), Time=c("mor","eve"))
( pm<-predict(my.model, new.my.model, level = 0) )

# [1] 15.30408 15.51259 12.99398 12.05721
# attr(,"label")
# [1] "Predicted values"

cbind(new.my.model, pred.mean=pm)

#   Origin Time pred.mean
# 1     Ka  mor  15.30408
# 2     La  mor  12.99398
# 3     Ka  eve  15.51259
# 4     La  eve  12.05721


来源:https://stackoverflow.com/questions/28684645/r-lme-cannot-evaluate-groups-for-desired-levels-on-newdata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!