Lui*_*cía 4 plot r lme4 confidence-interval
I\xc2\xb4m 尝试提取用 绘制的置信区间和截距值dotplot(ranef())。我怎样才能做到这一点?
attach(sleepstudy)\nlibrary(lme4)\nfm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)\nlattice::dotplot(ranef(fm1, condVar=TRUE))\nRun Code Online (Sandbox Code Playgroud)\n\n我尝试探索列表对象fm1,但找不到 CI。
rr <- ranef(fm1) ## condVar = TRUE has been the default for a while\nRun Code Online (Sandbox Code Playgroud)\nWith as.data.frame: 给出条件模式和 SD,您可以从中计算间隔(从技术上讲,这些不是“置信区间”,因为 BLUP/条件模式的值不是参数...)
dd <- as.data.frame(rr)\ntransform(dd, lwr = condval - 1.96*condsd, upr = condval + 1.96*condsd)\nRun Code Online (Sandbox Code Playgroud)\n或者与broom.mixed::tidy:
broom.mixed::tidy(m1, effects = "ran_vals", conf.int = TRUE)\nRun Code Online (Sandbox Code Playgroud)\nbroom.mixed::tidy()内部使用as.data.frame.ranef.mer()(由 调用的方法as.data.frame):该函数采用中描述的相当复杂的数据结构?lme4::ranef,并以更用户友好的格式提取条件模式和标准差:
\n\n如果 \xe2\x80\x98condVar\xe2\x80\x99 是 \xe2\x80\x98TRUE\xe2\x80\x99 则 \xe2\x80\x98"postVar"\xe2\x80\x99\nattribute 是维度为 j 的数组by j by k(或此类数组的列表)。该数组的第 k 个面是正定对称 j × j 矩阵。如果模型中只有一个分组因子,则整个随机效应向量的方差-协方差矩阵(以模型参数和数据的估计为条件)将是块对角线;这个 j × j\n 矩阵是第 k 个对角线块。使用多个分组因子\n\n\n\n\n\n\xe2\x80\x98"postVar"\xe2\x80\x99 属性的面仍然是此条件方差-协方差矩阵的对角\n块,但\n矩阵本身不再是块对角线。
\n
在这种特殊情况下,您需要执行以下操作来复制condsd列as.data.frame():
## get the \'postVar\' attribute of the first (and only) RE term\naa <- attr(rr$Subject, "postVar")\n## for each slice of the array, extract the diagonal;\n## transpose and drop dimensions;\n## take the square root\nsqrt(c(t(apply(aa, 3, diag))))\nRun Code Online (Sandbox Code Playgroud)\n