[英]How to plot estimate values for a lmer regression model in R?
I have data that looks like this: 我有看起来像这样的数据:
height <- c(1,2,3,4,2,4,6,8)
weight <- c(12,13,14,15,22,23,24,25)
type <- c("Wheat","Wheat","Wheat","Wheat","Rice","Rice","Rice","Rice")
set <- c(1,1,1,1,2,2,2,2)
dat <- data.frame(set,type,height,weight)
I run a lmer model with set as a random effect in R: 我运行一个lmer模型,并将其设置为R中的随机效果:
mod <- lmer(weight~height + type + (1|set), data = dat)
Now, I want to plot the estimates of the model and plot a regression, with weight on the x-axis and height on the y-axis, facet(~type) 现在,我想绘制模型的估计值并绘制回归,x轴上的权重和y轴上的高度,facet(〜type)
I use the predict function as follows 我使用预测功能如下
dat$pred <- predict(mod, type = "response")
And I want to achieve a ggplot that will look like this: 我想实现一个如下所示的ggplot:
ggplot(dat,aes(x = weight, y = height)) +
geom_point() + geom_smooth(method="lm", fill=NA) + facet_grid(~ type, scales = "free")
However, I note that the predict function has only a singular output. 但是,我注意到预测函数只有一个奇异的输出。 How do I plot that to achieve the same as above?
我该如何实现上述目标? Or do I have to store two different predict responses, and then plug it into the x,y of ggplot?
还是我必须存储两个不同的预测响应,然后将其插入ggplot的x,y?
I can adapt your plot to show raw vs. predicted values like this: 我可以调整您的图以显示原始值与预测值,如下所示:
ggplot(dat,aes(y = height)) +
geom_point(aes(x = weight)) +
geom_line(aes(x = pred)) +
facet_grid(~ type, scales = "free")
In your example plot though you have weight
, the outcome variable in your model, on the x-axis, which is confusing. 在示例图中,尽管您具有
weight
,但是模型中x轴上的结果变量令人困惑。 Normally you would have the outcome/predicted variable on the y-axis, so I would have plotted your model predictions like: 通常,您将在y轴上具有结果/预测变量,因此我会绘制模型预测,例如:
ggplot(dat,aes(x = height)) +
geom_point(aes(y = weight)) +
geom_line(aes(y = pred)) +
facet_grid(~ type, scales = "free")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.