在R中,使用qqmath或dotplot绘制lmer(lme4包)中的随机效果:如何使它看起来很花哨? [英] In R, plotting random effects from lmer (lme4 package) using qqmath or dotplot: how to make it look fancy?

查看:615
本文介绍了在R中,使用qqmath或dotplot绘制lmer(lme4包)中的随机效果:如何使它看起来很花哨?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

qqmath函数使用lmer软件包的输出产生了很大的随机效应的履带图。也就是说,qqmath非常擅长绘制一个等级模型的截距,其误差在点估计附近。 lmer和qqmath函数的一个示例如下,使用名为Dyestuff的lme4包中的内置数据。该代码将生成分层模型和使用ggmath函数的漂亮图。

  library(lme4)
数据(包=lme4)

#染料
#从六个批次生产的样品中获得收益
#的平衡单向分类

总结(染料)

#批量是随机效应的一个例子
#拟合单向随机效应线性模型
fit1 < - lmer(Yield〜1 + (1 | Batch),Dyestuff)
总结(fit1)
coef(fit1)#批中每个级别的截距

#随机效应及其变化的qqplot
qqmath(ranef(fit1,postVar = TRUE),strip = FALSE)$ Batch

代码行生成一个非常好的每个截距与每个估算周围的误差。但格式化qqmath函数似乎非常困难,我一直在努力格式化图表。我提出了几个我无法回答的问题,并且我认为如果他们使用lmer / qqmath组合,其他人也可以从中受益:


  1. 是否有办法使用上面的qqmath函数并添加一些
    选项,例如,为不同的点使某些点为空而不是填充,或
    为不同的颜色?例如,您是否可以填充Batch变量的A,B和C点,但其余点是否为空?

  2. 是否可以为每个点添加轴标签点(也许沿着
    顶部或右边的y轴)?
  3. 我的数据接近45个截距,因此可以在
    之间添加间距这些标签不会碰到对方?
    主要的是,我有兴趣在
    图上的点之间区分/标记,这在ggmath函数中似乎很麻烦/不可能。

到目前为止,在qqmath函数中添加任何附加选项会产生错误,因为如果它是标准情节,我不会收到错误,所以我不知所措。



另外,如果您觉得有更好的包装/功能来绘制lmer输出的截取内容,我很乐意听到它! (例如,你可以使用dotplot做点1-3吗?)



谢谢。

编辑:如果可以合理格式化,我也可以选择另一个点图。我只是喜欢ggmath情节的样子,所以我开始提出一个关于它的问题。

使用库 ggplot2 绘制类似的图形,然后您可以调整绘图的外观。 首先, ranef 对象保存为 randoms 。然后截取的差异保存在对象 qq 中。

  randoms < ranef(fit1,postVar = TRUE)
qq < - attr(ranef(fit1,postVar = TRUE)[[1]],postVar)
$ b

对象 rand.interc 包含随机截取的关卡名称。

  rand.interc <-randoms $ Batch 

所有对象放在一个数据框中。对于错误间隔 sd.interc 计算为方差平方根的2倍。

  df <-data.frame(Intercepts = randoms $ Batch [,1],
sd.interc = 2 * sqrt(qq [,, 1:length(qq)]),
lev .names = rownames(rand.interc))

如果需要截取的图根据值,那么 lev.names 应该重新排序。

  df $ lev.names< -factor(df $ lev)此行可以跳过。 .names,levels = df $ lev.names [order(df $ Intercepts)])

此代码产生情节。现在点数会根据因子水平的不同而被 shape >

  library( ggplot2)
p < - ggplot(df,aes(lev.names,Intercepts,shape = lev.names))

#在y = 0时添加水平线, (yinter = sd.interc,ymax = Intercepts + sd.interc),width = 0,color =black )+ geom_point(aes(size = 2))

#删除图例并将scale_shape_manual点形状设置为1和16
p <-p + guides(size = FALSE,shape = FALSE )+ scale_shape_manual(values = c(1,1,1,16,16,16))

#剧情改变外观(黑白主题)和x和y轴标签
p < - p + theme_bw()+ xlab(Levels)+ ylab()

#plot
p < - p + theme(axis.text.x = element_text(size = rel(1.2)),
axis.title.x = element_text(size = rel(1.3)),
axis.text.y = element_text(size = rel(1.2)) ,
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank())

#把水平放在y轴上,你只需要使用coord_flip()
p < - p + coord_flip()
print(p)


The qqmath function makes great caterpillar plots of random effects using the output from the lmer package. That is, qqmath is great at plotting the intercepts from a hierarchical model with their errors around the point estimate. An example of the lmer and qqmath functions are below using the built-in data in the lme4 package called Dyestuff. The code will produce the hierarchical model and a nice plot using the ggmath function.

library("lme4")
data(package = "lme4")

# Dyestuff 
# a balanced one-way classiï¬cation of Yield 
# from samples produced from six Batches

summary(Dyestuff)             

# Batch is an example of a random effect
# Fit 1-way random effects linear model
fit1 <- lmer(Yield ~ 1 + (1|Batch), Dyestuff) 
summary(fit1)
coef(fit1) #intercept for each level in Batch 

# qqplot of the random effects with their variances
qqmath(ranef(fit1, postVar = TRUE), strip = FALSE)$Batch

The last line of code produces a really nice plot of each intercept with the error around each estimate. But formatting the qqmath function seems to be very difficult, and I've been struggling to format the plot. I've come up with a few questions that I cannot answer, and that I think others could also benefit from if they are using the lmer/qqmath combination:

  1. Is there a way to take the qqmath function above and add a few options, such as, making certain points empty vs. filled-in, or different colors for different points? For example, can you make the points for A,B, and C of the Batch variable filled, but then the rest of the points empty?
  2. Is it possible to add axis labels for each point (maybe along the top or right y axis, for example)?
  3. My data has closer to 45 intercepts, so it is possible to add spacing between the labels so they do not run into each other? MAINLY, I am interested in distinguishing/labeling between points on the graph, which seems to be cumbersome/impossible in the ggmath function.

So far, adding any additional option in the qqmath function produce errors where I would not get errors if it was a standard plot, so I'm at a loss.

ALSO, if you feel there is a better package/function for plotting intercepts from lmer output, I'd love to hear it! (for example, can you do points 1-3 using dotplot?)

Thanks.

EDIT: I'm also open to an alternative dotplot if it can be reasonably formatted. I just like the look of a ggmath plot, so I'm starting with a question about that.

解决方案

One possibility is to use library ggplot2 to draw similar graph and then you can adjust appearance of your plot.

First, ranef object is saved as randoms. Then variances of intercepts are saved in object qq.

randoms<-ranef(fit1, postVar = TRUE)
qq <- attr(ranef(fit1, postVar = TRUE)[[1]], "postVar")

Object rand.interc contains just random intercepts with level names.

rand.interc<-randoms$Batch

All objects put in one data frame. For error intervals sd.interc is calculated as 2 times square root of variance.

df<-data.frame(Intercepts=randoms$Batch[,1],
              sd.interc=2*sqrt(qq[,,1:length(qq)]),
              lev.names=rownames(rand.interc))

If you need that intercepts are ordered in plot according to value then lev.names should be reordered. This line can be skipped if intercepts should be ordered by level names.

df$lev.names<-factor(df$lev.names,levels=df$lev.names[order(df$Intercepts)])

This code produces plot. Now points will differ by shape according to factor levels.

library(ggplot2)
p <- ggplot(df,aes(lev.names,Intercepts,shape=lev.names))

#Added horizontal line at y=0, error bars to points and points with size two
p <- p + geom_hline(yintercept=0) +geom_errorbar(aes(ymin=Intercepts-sd.interc, ymax=Intercepts+sd.interc), width=0,color="black") + geom_point(aes(size=2)) 

#Removed legends and with scale_shape_manual point shapes set to 1 and 16
p <- p + guides(size=FALSE,shape=FALSE) + scale_shape_manual(values=c(1,1,1,16,16,16))

#Changed appearance of plot (black and white theme) and x and y axis labels
p <- p + theme_bw() + xlab("Levels") + ylab("")

#Final adjustments of plot
p <- p + theme(axis.text.x=element_text(size=rel(1.2)),
               axis.title.x=element_text(size=rel(1.3)),
               axis.text.y=element_text(size=rel(1.2)),
               panel.grid.minor=element_blank(),
               panel.grid.major.x=element_blank())

#To put levels on y axis you just need to use coord_flip()
p <- p+ coord_flip()
print(p)

这篇关于在R中,使用qqmath或dotplot绘制lmer(lme4包)中的随机效果:如何使它看起来很花哨?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆