rr <- ranef(fm1) ## condVar = TRUE has been the default for a while
使用as.data.frame
函数:可以得到条件模式和标准偏差,从中计算出区间(严格来说,这不是“置信区间”,因为BLUPs/条件模式的值不是参数...)
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*j*k的数组(或者一列这样的数组)。该数组的第k个面是一个正定对称的j*j矩阵。如果模型中只有一个分组因素,则完整随机效应向量的方差-协方差矩阵,在模型参数和数据估计的条件下,将是块对角的。这个j*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.mer
的作用,以从 attr(rr$Subject, 'postVar')
(假定)中获取条件SD吗? - jay.sf
attach()
(并且被认为是有害的)。 - Ben Bolker