如何在R中绘制lmer回归模型的估计值?
我有如下数据:如何在R中绘制lmer回归模型的估计值?,r,ggplot2,regression,predict,R,Ggplot2,Regression,Predict,我有如下数据: 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) ggplot(dat,aes(x = we
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)
ggplot(dat,aes(x = weight, y = height)) +
geom_point() + geom_smooth(method="lm", fill=NA) + facet_grid(~ type, scales = "free")
ggplot(dat,aes(y = height)) +
geom_point(aes(x = weight)) +
geom_line(aes(x = pred)) +
facet_grid(~ type, scales = "free")
然而,我注意到predict函数只有一个奇异输出。如何绘制该图以实现与上述相同的效果?或者我必须存储两个不同的预测响应,然后将其插入ggplot的x,y?我可以调整您的绘图以显示原始值与预测值,如下所示:
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)
ggplot(dat,aes(x = weight, y = height)) +
geom_point() + geom_smooth(method="lm", fill=NA) + facet_grid(~ type, scales = "free")
ggplot(dat,aes(y = height)) +
geom_point(aes(x = weight)) +
geom_line(aes(x = pred)) +
facet_grid(~ type, scales = "free")
在示例图中,虽然您在x轴上有权重
,但模型中的结果变量令人困惑。通常情况下,y轴上会有结果/预测变量,因此我会绘制模型预测,如下所示:
ggplot(dat,aes(x = height)) +
geom_point(aes(y = weight)) +
geom_line(aes(y = pred)) +
facet_grid(~ type, scales = "free")