从 lme4 mer 模型对象中提取随机效应方差
我有一个具有固定和随机效果的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
其他一些答案是可行的,但我声称最好的答案是使用为此设计的访问器方法 -
VarCorr
(这与lme4
中的相同) > 的前身,nlme
包)。更新在最新版本的
lme4
(版本1.1-7,但以下所有内容可能适用于版本> = 1.0)中,VarCorr
更比以前更灵活,并且应该可以完成您想要的所有操作,而无需在拟合的模型对象内进行搜索。默认情况下,
VarCorr()
打印标准差,但如果您愿意,您也可以获取方差:(
comp=c("Variance","Std.Dev.")
将打印两者)。为了获得更大的灵活性,您可以使用
as.data.frame
方法转换VarCorr
对象,该对象提供分组变量、效应变量和方差/协方差或标准差/相关性:最后,
VarCorr
对象的原始形式(如果不需要的话,你可能不应该打扰你)是一个方差-协方差矩阵的列表,其中额外的对标准差和相关性进行编码的(冗余)信息,以及给出残差标准差并指定模型是否具有估计尺度参数的属性 ("sc"
) ("useSc"< /代码>)。
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 inlme4
's predecessor, thenlme
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.By default
VarCorr()
prints standard deviations, but you can get variances instead if you prefer:(
comp=c("Variance","Std.Dev.")
will print both).For more flexibility, you can use the
as.data.frame
method to convert theVarCorr
object, which gives the grouping variable, effect variable(s), and the variance/covariance or standard deviation/correlations: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"
).lmer
返回一个 S4 对象,所以这应该可以工作:Which prints:
...一般来说,您可以查看
print
和summary “mer”对象的方法:
lmer
returns an S4 object, so this should work:Which prints:
...In general, you can look at the source of the
print
andsummary
methods for "mer" objects:这个包对于这样的事情很有用(https://easystats.github.io/insight /reference/index.html)
This package is useful for things like this (https://easystats.github.io/insight/reference/index.html)
这个答案很大程度上基于@Ben Bolker的答案,但是如果人们对此不熟悉并且想要值本身,而不仅仅是值的打印输出(正如OP似乎想要的那样),那么您可以按如下方式提取值:
将
VarCorr
对象转换为数据框。然后访问每个单独的值:
使用此方法(在您创建的日期范围内指定行和列),您可以访问您想要的任何值。
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.Then access each individual value:
With this method (specifying rows and columns in the date frame you created) you can access whichever values you'd like.
另一种可能性是
Another possibility is
尝试
举个例子:
结束。
Try
As an example:
The end.