[英]How to extract only the random effects correlation parameters from an lmer model?
I am trying to extract random effect correlation parameters from an lmer output.我正在尝试从 lmer output 中提取随机效应相关参数。
This is my model:这是我的 model:
m <- lmer(RT ~ Condition + (1 + Condition| Participant), data)
Giving me the following output:给我以下 output:
REML criterion at convergence: 6533.6
Scaled residuals:
Min 1Q Median 3Q Max
-3.4666 -0.6318 -0.0232 0.5696 4.1010
Random effects:
Groups Name Variance Std.Dev. Corr
Participant (Intercept) 0.045483 0.21327
Condition2 0.001271 0.03565 -0.43
Condition3 0.005774 0.07599 -0.04 -0.09
Condition4 0.003817 0.06178 -0.57 0.60 0.69
Residual 0.147445 0.38399
Number of obs: 6841, groups: Participant, 39
Fixed effects:
Estimate Std. Error t value
(Intercept) 1.57546 0.03537 44.544
Condition2 0.06677 0.01420 4.703
Condition3 -0.09581 0.01798 -5.328
Condition4 0.02710 0.01639 1.653
Correlation of Fixed Effects:
(Intr) Cndtn2 Cndtn3
Condition2 -0.334
Condition3 -0.157 0.307
Condition4 -0.476 0.508 0.571
However, I only want to extract specific correlation parameters of the random effects, say the correlation between Condition3 and Condition2 (-0.04).但是,我只想提取随机效应的特定相关参数,比如 Condition3 和 Condition2 之间的相关性(-0.04)。 Does anyone know how to do that?
有谁知道这是怎么做到的吗?
I tried using the VarCorr()
function which only displays the results for the random effects, but still does not let me extract specific values from it.我尝试使用
VarCorr()
function,它只显示随机效果的结果,但仍然不允许我从中提取特定值。 I would really appreciate any help!我真的很感激任何帮助!
You want to use lme4::VarCorr
to extract those values.您想使用
lme4::VarCorr
来提取这些值。 Here is an example.这是一个例子。
library(lme4)
data("sleepstudy")
sl <- sleepstudy
m1 <- lmer(
Reaction ~ Days + (Days | Subject),
data = sl
)
summary(m1)
Linear mixed model fit by REML ['lmerMod']
Formula: Reaction ~ Days + (Days | Subject)
Data: sl
REML criterion at convergence: 1743.6
Scaled residuals:
Min 1Q Median 3Q Max
-3.9536 -0.4634 0.0231 0.4634 5.1793
Random effects:
Groups Name Variance Std.Dev. Corr
Subject (Intercept) 612.10 24.741
Days 35.07 5.922 0.07
Residual 654.94 25.592
Number of obs: 180, groups: Subject, 18
Fixed effects:
Estimate Std. Error t value
(Intercept) 251.405 6.825 36.838
Days 10.467 1.546 6.771
Correlation of Fixed Effects:
(Intr)
Days -0.138
Here, we want to extract that correlation between (Intercept)
and Days
.在这里,我们想要提取
(Intercept)
和Days
之间的相关性。 We do that like so:我们这样做:
(ranef_vals <- data.frame(VarCorr(m1)))
grp var1 var2 vcov sdcor
1 Subject (Intercept) <NA> 612.100158 24.74065799
2 Subject Days <NA> 35.071714 5.92213766
3 Subject (Intercept) Days 9.604409 0.06555124
4 Residual <NA> <NA> 654.940008 25.59179572
The value we'd want here is on the third row in the sdcor
column.我们想要的值在
sdcor
列的第三行。
ranef_vals$sdcor[3]
[1] 0.06555124
@mfidino's answer is good. @mfidino 的回答很好。 Alternatively
或者
cc <- cov2cor(VarCorr(m1)$Subject)
cc["Days", "(Intercept)"]
or或者
cc <- attr(VarCorr(m1)$Subject, "corr")
cc["Days", "(Intercept)"]
The $Subject
part is required because lmer
models can have multiple random effects terms, so VarCorr
is always returned as a list of covariance matrices (named according to the name of the corresponding grouping variable) $Subject
部分是必需的,因为lmer
模型可以有多个随机效应项,因此VarCorr
总是作为协方差矩阵列表返回(根据相应分组变量的名称命名)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.