提取 lmer 随机效应的置信区间;用 dotplot(ranef()) 绘制

Extract the confidence intervals of lmer random effects; plotted with dotplot(ranef())

我正在尝试提取用 dotplot(ranef()) 绘制的置信区间和截距值。我该怎么做?

attach(sleepstudy)
library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
lattice::dotplot(ranef(fm1, condVar=TRUE))

我尝试探索列表对象 fm1 但找不到 CI。

rr <- ranef(fm1)  ## condVar = TRUE has been the default for a while

With as.data.frame:给出条件模式和 SD,您可以从中计算区间(从技术上讲,这些不是“置信区间”,因为 BLUPs/conditional 模式的值不是参数...)

dd <- as.data.frame(rr)
transform(dd, lwr = condval - 1.96*condsd, upr = condval + 1.96*condsd)

broom.mixed::tidy:

broom.mixed::tidy(m1, effects = "ran_vals", conf.int = TRUE)

broom.mixed::tidy() 在内部使用 as.data.frame.ranef.mer()as.data.frame 调用的方法):该函数采用 ?lme4::ranef 中描述的相当复杂的数据结构并提取条件模式和以更加用户友好的格式表示的标准偏差:

If ‘condVar’ is ‘TRUE’ the ‘"postVar"’ attribute is an array of dimension j by j by k (or a list of such arrays). The kth face of this array is a positive definite symmetric j by j matrix. If there is only one grouping factor in the model the variance-covariance matrix for the entire random effects vector, conditional on the estimates of the model parameters and on the data, will be block diagonal; this j by j matrix is the kth diagonal block. With multiple grouping factors the faces of the ‘"postVar"’ attributes are still the diagonal blocks of this conditional variance-covariance matrix but the matrix itself is no longer block diagonal.

在这种特殊情况下,您需要执行以下操作来复制 as.data.frame()condsd 列:

## get the 'postVar' attribute of the first (and only) RE term
aa <- attr(rr$Subject, "postVar")
## for each slice of the array, extract the diagonal;
##  transpose and drop dimensions;
##  take the square root
sqrt(c(t(apply(aa, 3, diag))))