gpt4 book ai didi

r - 使用 qqmath 或 dotplot : How to make it look fancy? 绘制来自 lmer(lme4 包)的随机效应

转载 作者:行者123 更新时间:2023-12-03 07:53:04 27 4
gpt4 key购买 nike

qqmath 函数使用 lmer 包的输出制作了很好的随机效应毛毛虫图。也就是说,qqmath 擅长绘制层次模型中的截距及其围绕点估计的误差。下面是 lmer 和 qqmath 函数的一个示例,使用 lme4 包中称为 Dyestuff 的内置数据。该代码将使用 ggmath 函数生成分层模型和漂亮的绘图。

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

最后一行代码生成了一个非常漂亮的每个截距图,每个估计都有误差。但是格式化 qqmath 函数似乎非常困难,我一直在努力格式化绘图。我提出了一些我无法回答的问题,我认为其他人如果使用 lmer/qqmath 组合也可以从中受益:
  • 有没有办法把上面的qqmath函数加几个
    选项,例如使某些点为空与填充,或
    不同的点有不同的颜色?例如,您能否将 Batch 变量的 A、B 和 C 的点填满,而其余的点为空?
  • 是否可以为每个点添加轴标签(也许沿着
    例如顶部或右侧 y 轴)?
  • 我的数据有接近 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")

    对象 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))

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

    enter image description here

    关于r - 使用 qqmath 或 dotplot : How to make it look fancy? 绘制来自 lmer(lme4 包)的随机效应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13847936/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com