2017-09-05 92 views
0

我有一个看起来像这样的数据:如何绘制R中lmer回归模型的估计值?

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) 

我运行集11聚物模型作为R中随机效应:

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

现在,我要绘制的模型的估计和在x轴和高度在y轴上绘制回归,具有重量,小刻面(〜型)

我使用预测函数如下

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?

回答

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