问题描述
我正在 R v.3.0.2 上运行 nlme
包中的 lme
-模型.我正在尝试使用 predict.lme
提取模型估计值,但它返回一个错误.这是复制错误的代码:
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'
这是数据:
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=",")
数据是两个因子(Ka、La),每个因子有两个水平(mor、eve)和一个随机效应.我正在尝试提取 4 个数据级别的估计值,即 Ka,mor ;卡, 夏娃;拉,摩尔;晚安.
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.
第一次在 CV 上被问到是在另一个上下文中,但我被推荐到这里.
First asked over at CV in another context, but I was recommended here.
推荐答案
如果想忽略随机分量,那么设置levels=0:1
是不对的;你应该只使用 levels=0
.
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
这篇关于R:lme,无法评估“newdata"上所需级别的组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!