从 lme4 mer 模型对象中提取随机效应方差

发布于 2024-12-21 14:04:52 字数 482 浏览 1 评论 0原文

我有一个具有固定和随机效果的 mer 对象。如何提取随机效应的方差估计?这是我的问题的简化版本。

study <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
study

这给出了一个很长的输出 - 在这种情况下不会太长。无论如何,我如何明确选择

Random effects:
Groups   Name        Variance Std.Dev.
Subject  (Intercept) 1378.18  37.124  
Residual              960.46  30.991  

输出的部分?我想要价值观本身。

我看了好久

str(study)

,什么也没有!还检查了 lme4 包中的任何提取器功能,但均无济于事。请帮忙!

I have a mer object that has fixed and random effects. How do I extract the variance estimates for the random effects? Here is a simplified version of my question.

study <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
study

This gives a long output - not too long in this case. Anyway, how do I explicitly select the

Random effects:
Groups   Name        Variance Std.Dev.
Subject  (Intercept) 1378.18  37.124  
Residual              960.46  30.991  

part of the output? I want the values themselves.

I have taken long looks at

str(study)

and there's nothing there! Also checked any extractor functions in the lme4 package to no avail. Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

故事与诗 2024-12-28 14:04:52

其他一些答案是可行的,但我声称最好的答案是使用为此设计的访问器方法 - VarCorr (这与 lme4 中的相同) > 的前身,nlme 包)。

更新在最新版本的lme4(版本1.1-7,但以下所有内容可能适用于版本> = 1.0)中,VarCorr更比以前更灵活,并且应该可以完成您想要的所有操作,而无需在拟合的模型对象内进行搜索。

library(lme4)
study <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
VarCorr(study)
##  Groups   Name        Std.Dev.
##  Subject  (Intercept) 37.124  
##  Residual             30.991

默认情况下,VarCorr() 打印标准差,但如果您愿意,您也可以获取方差:

print(VarCorr(study),comp="Variance")
##  Groups   Name        Variance
##  Subject  (Intercept) 1378.18 
##  Residual              960.46 

(comp=c("Variance","Std.Dev.") 将打印两者)。

为了获得更大的灵活性,您可以使用 as.data.frame 方法转换 VarCorr 对象,该对象提供分组变量、效应变量和方差/协方差或标准差/相关性:

as.data.frame(VarCorr(study))
##        grp        var1 var2      vcov    sdcor
## 1  Subject (Intercept) <NA> 1378.1785 37.12383
## 2 Residual        <NA> <NA>  960.4566 30.99123

最后,VarCorr 对象的原始形式(如果不需要的话,你可能不应该打扰你)是一个方差-协方差矩阵的列表,其中额外的对标准差和相关性进行编码的(冗余)信息,以及给出残差标准差并指定模型是否具有估计尺度参数的属性 ("sc") ("useSc"< /代码>)。

unclass(VarCorr(fm1))
## $Subject
##             (Intercept)      Days
## (Intercept)  612.089748  9.604335
## Days           9.604335 35.071662
## attr(,"stddev")
## (Intercept)        Days 
##   24.740448    5.922133 
## attr(,"correlation")
##             (Intercept)       Days
## (Intercept)  1.00000000 0.06555134
## Days         0.06555134 1.00000000
## 
## attr(,"sc")
## [1] 25.59182
## attr(,"useSc")
## [1] TRUE
## 

Some of the other answers are workable, but I claim that the best answer is to use the accessor method that is designed for this -- VarCorr (this is the same as in lme4's predecessor, the nlme package).

UPDATE in recent versions of lme4 (version 1.1-7, but everything below is probably applicable to versions >= 1.0), VarCorr is more flexible than before, and should do everything you want without ever resorting to fishing around inside the fitted model object.

library(lme4)
study <- lmer(Reaction ~ Days + (1|Subject), data = sleepstudy)
VarCorr(study)
##  Groups   Name        Std.Dev.
##  Subject  (Intercept) 37.124  
##  Residual             30.991

By default VarCorr() prints standard deviations, but you can get variances instead if you prefer:

print(VarCorr(study),comp="Variance")
##  Groups   Name        Variance
##  Subject  (Intercept) 1378.18 
##  Residual              960.46 

(comp=c("Variance","Std.Dev.") will print both).

For more flexibility, you can use the as.data.frame method to convert the VarCorr object, which gives the grouping variable, effect variable(s), and the variance/covariance or standard deviation/correlations:

as.data.frame(VarCorr(study))
##        grp        var1 var2      vcov    sdcor
## 1  Subject (Intercept) <NA> 1378.1785 37.12383
## 2 Residual        <NA> <NA>  960.4566 30.99123

Finally, the raw form of the VarCorr object (which you probably shouldn't mess with you if you don't have to) is a list of variance-covariance matrices with additional (redundant) information encoding the standard deviations and correlations, as well as attributes ("sc") giving the residual standard deviation and specifying whether the model has an estimated scale parameter ("useSc").

unclass(VarCorr(fm1))
## $Subject
##             (Intercept)      Days
## (Intercept)  612.089748  9.604335
## Days           9.604335 35.071662
## attr(,"stddev")
## (Intercept)        Days 
##   24.740448    5.922133 
## attr(,"correlation")
##             (Intercept)       Days
## (Intercept)  1.00000000 0.06555134
## Days         0.06555134 1.00000000
## 
## attr(,"sc")
## [1] 25.59182
## attr(,"useSc")
## [1] TRUE
## 
何处潇湘 2024-12-28 14:04:52

lmer 返回一个 S4 对象,所以这应该可以工作:

remat <- summary(study)@REmat
print(remat, quote=FALSE)

Which prints:

 Groups   Name        Variance Std.Dev.
 Subject  (Intercept) 1378.18  37.124  
 Residual              960.46  30.991  

...一般来说,您可以查看 printsummary “mer”对象的方法:

class(study) # mer
selectMethod("print", "mer")
selectMethod("summary", "mer")

lmer returns an S4 object, so this should work:

remat <- summary(study)@REmat
print(remat, quote=FALSE)

Which prints:

 Groups   Name        Variance Std.Dev.
 Subject  (Intercept) 1378.18  37.124  
 Residual              960.46  30.991  

...In general, you can look at the source of the print and summary methods for "mer" objects:

class(study) # mer
selectMethod("print", "mer")
selectMethod("summary", "mer")
烟凡古楼 2024-12-28 14:04:52
> attributes(summary(study))$REmat
 Groups     Name          Variance  Std.Dev.
 "Subject"  "(Intercept)" "1378.18" "37.124"
 "Residual" ""            " 960.46" "30.991"
> attributes(summary(study))$REmat
 Groups     Name          Variance  Std.Dev.
 "Subject"  "(Intercept)" "1378.18" "37.124"
 "Residual" ""            " 960.46" "30.991"
土豪我们做朋友吧 2024-12-28 14:04:52

这个包对于这样的事情很有用(https://easystats.github.io/insight /reference/index.html)

library("insight")

get_variance_random(study) #Where study is your fit mixed model

This package is useful for things like this (https://easystats.github.io/insight/reference/index.html)

library("insight")

get_variance_random(study) #Where study is your fit mixed model
霊感 2024-12-28 14:04:52

这个答案很大程度上基于@Ben Bolker的答案,但是如果人们对此不熟悉并且想要值本身,而不仅仅是值的打印输出(正如OP似乎想要的那样),那么您可以按如下方式提取值:

VarCorr 对象转换为数据框。

re_dat = as.data.frame(VarCorr(study))

然后访问每个单独的值:

int_vcov = re_dat[1,'vcov']
resid_vcov = re_dat[2,'vcov']

使用此方法(在您创建的日期范围内指定行和列),您可以访问您想要的任何值。

This answer is heavily based on that on @Ben Bolker's, but if people are new to this and want the values themselves, instead of just a printout of the values (as OP seems to have wanted), then you can extract the values as follows:

Convert the VarCorr object to a data frame.

re_dat = as.data.frame(VarCorr(study))

Then access each individual value:

int_vcov = re_dat[1,'vcov']
resid_vcov = re_dat[2,'vcov']

With this method (specifying rows and columns in the date frame you created) you can access whichever values you'd like.

临风闻羌笛 2024-12-28 14:04:52

另一种可能性是

sum <- summary (study)
var <- data.frame (sum$varcor)

Another possibility is

sum <- summary (study)
var <- data.frame (sum$varcor)
欲拥i 2024-12-28 14:04:52

尝试

attributes(study)

举个例子:

> women
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

> lm1 <- lm(height ~ weight, data=women)
> attributes(lm1)
$names
 [1] "coefficients"  "residuals"     "effects"       "rank"         
 [5] "fitted.values" "assign"        "qr"            "df.residual"  
 [9] "xlevels"       "call"          "terms"         "model"        

$class
[1] "lm"

> lm1$coefficients
(Intercept)      weight 
 25.7234557   0.2872492 

> lm1$coefficients[[1]]

[1] 25.72346


> lm1$coefficients[[2]]

[1] 0.2872492

结束。

Try

attributes(study)

As an example:

> women
   height weight
1      58    115
2      59    117
3      60    120
4      61    123
5      62    126
6      63    129
7      64    132
8      65    135
9      66    139
10     67    142
11     68    146
12     69    150
13     70    154
14     71    159
15     72    164

> lm1 <- lm(height ~ weight, data=women)
> attributes(lm1)
$names
 [1] "coefficients"  "residuals"     "effects"       "rank"         
 [5] "fitted.values" "assign"        "qr"            "df.residual"  
 [9] "xlevels"       "call"          "terms"         "model"        

$class
[1] "lm"

> lm1$coefficients
(Intercept)      weight 
 25.7234557   0.2872492 

> lm1$coefficients[[1]]

[1] 25.72346


> lm1$coefficients[[2]]

[1] 0.2872492

The end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文