如何从lme4获取随机效应(BLUP/条件模式)的协方差矩阵 [英] How to get covariance matrix for random effects (BLUPs/conditional modes) from lme4
问题描述
因此,我在R中拟合了带有两个随机截距的线性混合模型:
So, I've fitted a linear mixed model with two random intercepts in R:
Y = X beta + Z b + e_i,
其中b ~ MVN (0, Sigma)
; X
和Z
分别是固定效应模型矩阵和随机效应模型矩阵,beta
和b
是固定效应参数和随机效应BLUP/条件模式.
where 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.
我想了解b
的基础协方差矩阵,这在lme4
包中似乎并不重要.您只能通过VarCorr
获得方差,而不能获得实际的相关矩阵.
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. You can get only the variances by VarCorr
, not the actual correlation matrix.
根据其中一个小插图(第2页),您可以计算beta的协方差:e_i * lambda * t(lambda)
.您可以从lme4
的输出中提取所有这些组件.
According to one of the package vignettes (page 2), you can calculate the covariance of beta: e_i * lambda * t(lambda)
. And all those components you can extract from the output of lme4
.
我想知道这是走的路吗?或者您还有其他建议吗?
I was wondering if this is the way to go? Or do you have any other suggestions?
推荐答案
来自?ranef
:
如果"condVar"为"TRUE",则每个数据框都有一个属性 称为"postVar"",这是一个具有对称形状的三维阵列 脸每个面都包含一个方差-协方差矩阵 分组因子的特定级别. (此属性的名称 是一个历史文物,在某些情况下可能会更改为"condVar" 指向未来.)
If ‘condVar’ is ‘TRUE’ each of the data frames has an attribute called ‘"postVar"’ which is a three-dimensional array with symmetric faces; 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.)
设置示例:
library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)
rr <- ranef(fm1,condVar=TRUE)
获取截距的b
个值中的方差-协方差矩阵
Get the variance-covariance matrix among the b
values for the intercept
pv <- attr(rr[[1]],"postVar")
str(pv)
##num [1:2, 1:2, 1:18] 145.71 -21.44 -21.44 5.31 145.71 ...
所以这是一个2x2x18的数组;每个切片是特定主体的条件截距和斜率之间的方差-协方差矩阵(根据定义,每个主体的截距和斜率与所有其他主体的截距和斜率无关).
So this is a 2x2x18 array; 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).
要将其转换为方差-协方差矩阵(请参见getMethod("image",sig="dgTMatrix")
...)
To convert this to a variance-covariance matrix (see 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)
这篇关于如何从lme4获取随机效应(BLUP/条件模式)的协方差矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!