[英]How to get covariance matrix for random effects (BLUPs/conditional modes) from lme4
So, I've fitted a linear mixed model with two random intercepts in R: 因此,我为R中的两个随机截距拟合了线性混合模型:
Y = X beta + Z b + e_i,
where b ~ MVN (0, Sigma)
; 其中
b ~ MVN (0, Sigma)
; X
and Z
are the fixed- and random-effects model matrices respectively, and beta
and b
are the fixed-effect parameters and random-effects BLUPs/conditional modes. X
和Z
分别是固定效应模型矩阵和随机效应模型矩阵,而beta
和b
是固定效应参数和随机效应BLUP /条件模式。
I would like to get my hands on the underlying covariance matrix of b
, which doesn't seem to be a trivial thing in lme4
package. 我想了解
b
的基础协方差矩阵,这在lme4
包中似乎并不重要。 You can get only the variances by VarCorr
, not the actual correlation matrix. 您只能通过
VarCorr
获得方差,而不能获得实际的相关矩阵。
According to one of the package vignettes (page 2), you can calculate the covariance of beta: e_i * lambda * t(lambda)
. 根据其中一个小插图 (第2页),您可以计算beta的协方差:
e_i * lambda * t(lambda)
。 And all those components you can extract from the output of lme4
. 您可以从
lme4
的输出中提取所有这些组件。
I was wondering if this is the way to go? 我想知道这是走的路吗? Or do you have any other suggestions?
或者您还有其他建议吗?
From ?ranef
: 从
?ranef
:
If 'condVar' is 'TRUE' each of the data frames has an attribute called '"postVar"' which is a three-dimensional array with symmetric faces;
如果“ condVar”为“ TRUE”,则每个数据帧都具有一个称为““ postVar””的属性,该属性是具有对称面的三维数组; each face contains the variance-covariance matrix for a particular level of the grouping factor.
每个面都包含特定级别分组系数的方差-协方差矩阵。 (The name of this attribute is a historical artifact, and may be changed to 'condVar' at some point in the future.)
(此属性的名称是历史人工制品,将来可能会更改为“ condVar”。)
Set up an example: 设置示例:
library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
rr <- ranef(fm1,condVar=TRUE)
Get the variance-covariance matrix among the b
values for the intercept 获取截距的
b
值之间的方差-协方差矩阵
pv <- attr(rr[[1]],"postVar")
str(pv)
##num [1:2, 1:2, 1:18] 145.71 -21.44 -21.44 5.31 145.71 ...
So this is a 2x2x18 array; 所以这是一个2x2x18的数组; each slice is the variance-covariance matrix among the conditional intercept and slope for a particular subject (by definition, the intercepts and slopes for each subject are independent of the intercepts and slopes for all other subjects).
每个切片是特定主体的条件截距和斜率之间的方差-协方差矩阵(根据定义,每个主体的截距和斜率与所有其他主体的截距和斜率无关)。
To convert this to a variance-covariance matrix (see getMethod("image",sig="dgTMatrix")
...) 将此转换为方差-协方差矩阵(请参见
getMethod("image",sig="dgTMatrix")
...)
library(Matrix)
vc <- bdiag( ## make a block-diagonal matrix
lapply(
## split 3d array into a list of sub-matrices
split(pv,slice.index(pv,3)),
## ... put them back into 2x2 matrices
matrix,2))
image(vc,sub="",xlab="",ylab="",useRaster=TRUE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.