简体   繁体   English

如何从lme4获取随机效应(BLUP /条件模式)的协方差矩阵

[英]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. XZ分别是固定效应模型矩阵和随机效应模型矩阵,而betab是固定效应参数和随机效应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.

相关问题 从lme4中提取(i)随机效应的估计方差-协方差矩阵和/或(ii)混合模型方程解作为矩阵? - Extracting the (i) estimated variance-covariance matrix of random effects and/or (ii) mixed model equation solutions from lme4 as matrices? 具有固定和随机效应的库(lme4)中的MLM设计矩阵 - Design matrix for MLM from library(lme4) with fixed and random effects 如何从 lme4 中提取公式的随机效应部分 - How to extract just the random effects part of the formula from lme4 在lme4中指定随机效果 - specify random effects in lme4 获取lme4中的残差方差-协方差矩阵 - Get Residual Variance-Covariance Matrix in lme4 从 lmer 对象中提取随机效应的原始模型矩阵 (lme4, R) - Extract raw model matrix of random effects from lmer objects (lme4, R) 如何在nlme与lme4中指定不同的随机效果? - How to specify different random effects in nlme vs. lme4? 使用 qqmath 或 dotplot 绘制 lmer(lme4 包)的随机效应:如何让它看起来很花哨? - Plot random effects from lmer (lme4 package) using qqmath or dotplot: How to make it look fancy? 我如何正确地解释lme4的嵌套随机效应? - How do I get properly accounted for nested random effects with lme4? 如何从包裹在dlply中的lme4中提取随机效应和方差分量 - How to extract random effects and variance components from lme4 wrapped in dlply
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM