我以前在这里寻求过帮助很棒,我希望能再次得到一些帮助.
我估计一个相当大的混合效应模型,其中一个随机效应有超过150个不同的水平.这将使标准的毛毛虫情节变得非常难以理解.
如果可能的话,我想得到一个只是随机效应水平的毛虫情节,因为缺乏更好的术语,"显着".那就是:我想要一个毛虫图,其中随机截距或变化系数的随机斜率具有不包括零的"置信区间"(我知道它不完全是什么).
从标准的sleepstudy数据中考虑这个标准模型lme4.
library(lme4)
fit <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
ggCaterpillar(ranef(fit,condVar=TRUE), QQ=FALSE, likeDotplot=TRUE, reorder=FALSE)[["Subject"]]
Run Code Online (Sandbox Code Playgroud)
我会得到这个毛毛虫情节.

我使用的毛虫图来自此代码.注意我倾向于使用较不保守的界限(即1.645*se而不是1.96*se).
基本上,我想要一个毛虫图,其中只包括308,309,310,330,331,335,337,349,350,352和370的水平,因为这些水平有截距或斜率,其间隔不包括零.我问,因为我的150多个不同级别的毛虫情节有点难以理解,我认为这可能是一个值得解决的问题.
随后是可重现的代码.我真的很感激任何帮助.
# https://stackoverflow.com/questions/34120578/how-can-i-sort-random-effects-by-value-of-the-random-effect-not-the-intercept
ggCaterpillar <- function(re, QQ=TRUE, likeDotplot=TRUE, reorder=TRUE) {
require(ggplot2)
f <- function(x) {
pv <- attr(x, "postVar")
cols <- 1:(dim(pv)[1])
se <- unlist(lapply(cols, function(i) sqrt(pv[i, i, ])))
if (reorder) {
ord <- unlist(lapply(x, order)) + rep((0:(ncol(x) - 1)) * nrow(x), each=nrow(x))
pDf <- data.frame(y=unlist(x)[ord],
ci=1.645*se[ord],
nQQ=rep(qnorm(ppoints(nrow(x))), ncol(x)),
ID=factor(rep(rownames(x), ncol(x))[ord], levels=rownames(x)[ord]),
ind=gl(ncol(x), nrow(x), labels=names(x)))
} else {
pDf <- data.frame(y=unlist(x),
ci=1.645*se,
nQQ=rep(qnorm(ppoints(nrow(x))), ncol(x)),
ID=factor(rep(rownames(x), ncol(x)), levels=rownames(x)),
ind=gl(ncol(x), nrow(x), labels=names(x)))
}
if(QQ) { ## normal QQ-plot
p <- ggplot(pDf, aes(nQQ, y))
p <- p + facet_wrap(~ ind, scales="free")
p <- p + xlab("Standard normal quantiles") + ylab("Random effect quantiles")
} else { ## caterpillar dotplot
p <- ggplot(pDf, aes(ID, y)) + coord_flip()
if(likeDotplot) { ## imitate dotplot() -> same scales for random effects
p <- p + facet_wrap(~ ind)
} else { ## different scales for random effects
p <- p + facet_grid(ind ~ ., scales="free_y")
}
p <- p + xlab("Levels of the Random Effect") + ylab("Random Effect")
}
p <- p + theme(legend.position="none")
p <- p + geom_hline(yintercept=0)
p <- p + geom_errorbar(aes(ymin=y-ci, ymax=y+ci), width=0, colour="black")
p <- p + geom_point(aes(size=1.2), colour="blue")
return(p)
}
lapply(re, f)
}
library(lme4)
fit <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
ggCaterpillar(ranef(fit,condVar=TRUE), QQ=FALSE, likeDotplot=TRUE, reorder=FALSE)[["Subject"]]
ggsave(file="sleepstudy.png")
Run Code Online (Sandbox Code Playgroud)
首先,感谢将"重要"放在引号中...每个阅读此内容的人都应该记住,在这种情况下,重要性没有任何统计意义(使用Z-statistic(value/std.error)标准可能更好例如| Z |> 1.5或| Z |> 1.75而不是强调这不是推理阈值......)
我最终得到了一点点......我决定稍微重构/模块化一些东西会更好,所以我写了一个augment方法(设计用于处理broom包),从ranef.mer对象构造有用的数据帧......一旦完成,您想要的操作非常简单.
我把augment.ranef.mer代码放在我的答案的最后 - 它有点长(你需要在你可以运行代码之前获取它).
library(broom)
library(reshape2)
library(plyr)
Run Code Online (Sandbox Code Playgroud)
将augment方法应用于RE对象:
rr <- ranef(fit,condVar=TRUE)
aa <- augment(rr)
names(aa)
## [1] "grp" "variable" "level" "estimate" "qq" "std.error"
## [7] "p" "lb" "ub"
Run Code Online (Sandbox Code Playgroud)
现在ggplot代码非常基本.我使用的geom_errorbarh(height=0),而不是geom_pointrange()+coord_flip()因为ggplot2不能使用coord_flip与facet_wrap(...,scales="free")...
## Q-Q plot:
g0 <- ggplot(aa,aes(estimate,qq,xmin=lb,xmax=ub))+
geom_errorbarh(height=0)+
geom_point()+facet_wrap(~variable,scale="free_x")
## regular caterpillar plot:
g1 <- ggplot(aa,aes(estimate,level,xmin=lb,xmax=ub))+
geom_errorbarh(height=0)+
geom_vline(xintercept=0,lty=2)+
geom_point()+facet_wrap(~variable,scale="free_x")
Run Code Online (Sandbox Code Playgroud)
现在找到您想要保留的级别:
aa2 <- ddply(aa,c("grp","level"),
transform,
keep=any(p<0.05))
aa3 <- subset(aa2,keep)
Run Code Online (Sandbox Code Playgroud)
只更新具有"显着"斜率或截距的水平的毛虫图:
g1 %+% aa3
Run Code Online (Sandbox Code Playgroud)
如果您只想强调"重要"级别而不是完全删除"非重要"级别
ggplot(aa2,aes(estimate,level,xmin=lb,xmax=ub,colour=factor(keep)))+
geom_errorbarh(height=0)+
geom_vline(xintercept=0,lty=2)+
geom_point()+facet_wrap(~variable,scale="free_x")+
scale_colour_manual(values=c("black","red"),guide=FALSE)
Run Code Online (Sandbox Code Playgroud)
##' @importFrom reshape2 melt
##' @importFrom plyr ldply name_rows
augment.ranef.mer <- function(x,
ci.level=0.9,
reorder=TRUE,
order.var=1) {
tmpf <- function(z) {
if (is.character(order.var) && !order.var %in% names(z)) {
order.var <- 1
warning("order.var not found, resetting to 1")
}
## would use plyr::name_rows, but want levels first
zz <- data.frame(level=rownames(z),z,check.names=FALSE)
if (reorder) {
## if numeric order var, add 1 to account for level column
ov <- if (is.numeric(order.var)) order.var+1 else order.var
zz$level <- reorder(zz$level, zz[,order.var+1], FUN=identity)
}
## Q-Q values, for each column separately
qq <- c(apply(z,2,function(y) {
qnorm(ppoints(nrow(z)))[order(order(y))]
}))
rownames(zz) <- NULL
pv <- attr(z, "postVar")
cols <- 1:(dim(pv)[1])
se <- unlist(lapply(cols, function(i) sqrt(pv[i, i, ])))
## n.b.: depends on explicit column-major ordering of se/melt
zzz <- cbind(melt(zz,id.vars="level",value.name="estimate"),
qq=qq,std.error=se)
## reorder columns:
subset(zzz,select=c(variable, level, estimate, qq, std.error))
}
dd <- ldply(x,tmpf,.id="grp")
ci.val <- -qnorm((1-ci.level)/2)
transform(dd,
p=2*pnorm(-abs(estimate/std.error)), ## 2-tailed p-val
lb=estimate-ci.val*std.error,
ub=estimate+ci.val*std.error)
}
Run Code Online (Sandbox Code Playgroud)