【问题标题】:How to plot estimate values for a lmer regression model in R?如何在 R 中绘制 lmer 回归模型的估计值?
【发布时间】:2017-09-05 01:52:06
【问题描述】:

我的数据如下所示:

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)

我在 R 中运行一个设置为随机效应的 lmer 模型:

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

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

我使用predict函数如下

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") 

但是,我注意到 predict 函数只有一个奇异输出。我如何绘制它以实现与上述相同的效果?还是我必须存储两个不同的预测响应,然后将其插入 ggplot 的 x,y 中?

【问题讨论】:

    标签: r ggplot2 regression predict


    【解决方案1】:

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

    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")
    

    【讨论】:

      猜你喜欢
      • 2020-10-14
      • 2014-05-24
      • 1970-01-01
      • 2022-11-09
      • 2019-08-10
      • 1970-01-01
      • 1970-01-01
      • 2020-07-13
      • 1970-01-01
      相关资源
      最近更新 更多