将lmer的预测值绘制为单个绘图 [英] Plotting predicted values from lmer as a single plot
问题描述
我正在绘制多级模型的预测值(使用lme4软件包)。我可以使用 Effect()
函数成功执行此操作。如下图所示:
library(lme4)
库(效果)
m1 = lmer(价格〜深度*切割+(1 |切割),钻石)
plot(效果(c(cut,depth),m1))
但是,我想将这些数据作为一个传说的单一情节。使用ggplots,我可以做到这一点;但是,我失去了错误栏,如下所示:
ggplot(data.frame(Effect(c(cut, 深度),m1)),
aes(x = depth,y = fit,color = cut,group = cut))+
geom_line()
How我可以重新创建第一个绘图(带有误差线)作为单个绘图吗?
($)
$ $ $ $
$ b $ lmer(价格〜深度*切割+(1 |切割),钻石)
顺便说一下,请注意这个特定的模型没有意义(因素包括固定和随机项)!
ee< - Effect(c(cut, depth),m1)
关键是使用 as.data。 frame()
将效果对象变成有用的东西...
$ p $
theme_set(theme_bw() )
ggplot(as.data.frame(ee),
aes(depth,fit,color = cut,fill = cut))+
geom_line()+
##颜色= NA抑制色带的边缘
geom_ribbon(color = NA,alpha = 0.1,
aes(ymin = lower,ymax = upper))+
## data
geom_rug(data = ee $ data,aes(y = NULL),sides =b)
I am working on graphing the predicted values from a multilevel model (using the lme4 package). I am able to do this successfully using the Effect()
function. As shown below:
library(lme4)
library(effects)
m1=lmer(price~depth*cut+(1|cut),diamonds)
plot(Effect(c("cut","depth"),m1))
But, I want to present these same data as a single plot with a legend. Using ggplots, I can do this; but, I lose the error bars, as shown below:
ggplot(data.frame(Effect(c("cut","depth"),m1)),
aes(x=depth,y=fit,color=cut,group=cut))+
geom_line()
How can I recreate the first plot (with error bars) as a single plot?
How about:
library(effects)
library(lme4)
library(ggplot2)
m1 <- lmer(price~depth*cut+(1|cut),diamonds)
By the way, note that this particular model makes no sense (factor included both as fixed and random term)! I hope you're only using it as an illustration ...
ee <- Effect(c("cut","depth"),m1)
The key is using as.data.frame()
to turn the effects object into something useful ...
theme_set(theme_bw())
ggplot(as.data.frame(ee),
aes(depth,fit,colour=cut,fill=cut))+
geom_line()+
## colour=NA suppresses edges of the ribbon
geom_ribbon(colour=NA,alpha=0.1,
aes(ymin=lower,ymax=upper))+
## add rug plot based on original data
geom_rug(data=ee$data,aes(y=NULL),sides="b")
这篇关于将lmer的预测值绘制为单个绘图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!