简体   繁体   English

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

[英]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.

相关问题 R:如何从 lmer() model 的回归结果中提取协变量 p 值列表? - R: how to extract list of covariate p-values from a regression results of an lmer() model? R:如何使用 plot ROC 进行逻辑回归 model 缺少值 - R: how to plot ROC for logistic regression model whit missing values 如何为lmer模型结果绘制标准误差的预测值? - How to plot predicted values with standard errors for lmer model results? 如何绘制 lmer 模型的一些术语 - How to plot some terms of a lmer model 如何估计 R 中的分位数回归预测 - How to estimate quantile regression predictions in R 如何在 R 中的同一个图上绘制线性回归模型和泊松回归模型? - How to plot a linear regression model and a poisson regression model on the same plot in R? 结合 R lmer() 中多个变量的 BLUP 估计 - Combine BLUP estimate for mulitple variables in R lmer() 如何使用 `R` `cem` package 在 CEM 匹配数据上估计线性回归 model 的系数? - How to estimate coefficients for linear regression model on CEM matched data using `R` `cem` package? 如何在R中绘制lm()/ glm()回归模型的排序系数? - How to plot the sorted coefficients for a lm()/glm() regression model in R? 如何从R中的回归模型中绘制多个变量? - How to plot multiple variables from regression model in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM