繁体   English   中英

如何在R中为lmer回归模型绘制估计值?

[英]How to plot estimate values for a lmer regression model in R?

我有看起来像这样的数据:

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)

我运行一个lmer模型,并将其设置为R中的随机效果:

mod <- lmer(weight~height + type + (1|set), data = dat)

现在,我想绘制模型的估计值并绘制回归,x轴上的权重和y轴上的高度,facet(〜type)

我使用预测功能如下

dat$pred <- predict(mod, type = "response")

我想实现一个如下所示的ggplot:

ggplot(dat,aes(x = weight, y = height)) +
geom_point() + geom_smooth(method="lm", fill=NA) + facet_grid(~ type, scales = "free") 

但是,我注意到预测函数只有一个奇异的输出。 我该如何实现上述目标? 还是我必须存储两个不同的预测响应,然后将其插入ggplot的x,y?

我可以调整您的图以显示原始值与预测值,如下所示:

ggplot(dat,aes(y = height)) +
    geom_point(aes(x = weight)) +
    geom_line(aes(x = pred)) + 
    facet_grid(~ type, scales = "free")

在示例图中,尽管您具有weight ,但是模型中x轴上的结果变量令人困惑。 通常,您将在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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM