lmer 对象的预测函数出错
我正在尝试使用 lmer
而不是 重现 Roland 的代码 中的情节>nlme
模型,但我在使用 predict
函数时遇到错误:
library(nlme)
library(lme4)
library(ggplot2)
fm2 <- lmer(distance ~ age + Sex + (1|Subject), data = Orthodont)
newdat <- expand.grid(Sex=unique(Orthodont$Sex),
age=c(min(Orthodont$age),
max(Orthodont$age)))
p <- ggplot(Orthodont, aes(x=age, y=distance, colour=Sex)) +
geom_point(size=3) +
geom_line(aes(y=predict(fm2), group=Subject, size="Subjects")) +
geom_line(data=newdat, aes(y=predict(fm2, level=0, newdata=newdat), size="Population")) +
scale_size_manual(name="Predictions", values=c("Subjects"=0.5, "Population"=3)) +
theme_bw(base_size=22)
这是我得到的错误:
Error in eval(predvars, data, env) : object 'Subject' not found
In addition: Warning message:
In predict.merMod(fm2, level = 0, newdata = newdat) :
unused arguments ignored
I'm trying to reproduce the plot from Roland's code with lmer
instead of nlme
model but I'm getting an error with the predict
function:
library(nlme)
library(lme4)
library(ggplot2)
fm2 <- lmer(distance ~ age + Sex + (1|Subject), data = Orthodont)
newdat <- expand.grid(Sex=unique(Orthodont$Sex),
age=c(min(Orthodont$age),
max(Orthodont$age)))
p <- ggplot(Orthodont, aes(x=age, y=distance, colour=Sex)) +
geom_point(size=3) +
geom_line(aes(y=predict(fm2), group=Subject, size="Subjects")) +
geom_line(data=newdat, aes(y=predict(fm2, level=0, newdata=newdat), size="Population")) +
scale_size_manual(name="Predictions", values=c("Subjects"=0.5, "Population"=3)) +
theme_bw(base_size=22)
This is the error I get:
Error in eval(predvars, data, env) : object 'Subject' not found
In addition: Warning message:
In predict.merMod(fm2, level = 0, newdata = newdat) :
unused arguments ignored
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想绘制主题级别的预测,则需要在预测框架中包含
Subject
:或者,您可以在
中包含
调用:这相当于(忽略!)当前代码中的re.form = ~0
Predictlevel=0
规范,仅适用于lme
...If you want to plot subject-level predictions, you would need to include
Subject
in your prediction frame:Alternately you could include
re.form = ~0
in yourpredict
call: this is equivalent to the (ignored!)level=0
specification in your current code, which only works forlme
...