【问题标题】:Extract the confidence intervals of lmer random effects; plotted with dotplot(ranef())提取 lmer 随机效应的置信区间;用 dotplot(ranef()) 绘制
【发布时间】:2021-12-16 16:37:01
【问题描述】:

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

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

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

【问题讨论】:

  • attach() 在这里是不必要的(并且被认为是有害的)

标签: r plot lme4 confidence-interval


【解决方案1】:
rr <- ranef(fm1)  ## condVar = TRUE has been the default for a while

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

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 中描述的相当复杂的数据结构,并以更用户友好的格式提取条件模式和标准差:

如果‘condVar’为‘TRUE’,则‘"postVar"’ 属性是维度为 j x j x k 的数组(或此类列表 数组)。这个数组的第 k 个面是正定的 j 对称 j 矩阵。如果只有一个分组因子 该模型是整个随机数的方差-协方差矩阵 效应向量,以模型的估计为条件 参数和数据上,将是块对角线;这个 j by j 矩阵是第 k 个对角线块。具有多个分组因素 ‘"postVar"’ 属性的面仍然是对角线 这个条件方差 - 协方差矩阵的块,但 矩阵本身不再是块对角线。

在这种特殊情况下,您需要执行以下操作来复制 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))))

【讨论】:

  • 您是否介意简要说明 as.data.frame.ranef.merattr(rr$Subject, 'postVar') 获取条件 SD 所做的工作(假设)?
  • 谢谢!这正是我想要的。
  • @jay.sf:这有帮助吗?
  • @BenBolker 是的,谢谢,很好的回答 +1
猜你喜欢
  • 1970-01-01
  • 2012-11-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-20
  • 1970-01-01
  • 2020-11-05
  • 1970-01-01
相关资源
最近更新 更多