我如何使用包 lme4 获得随机效应的解决方案?
How I do to obtain the solution for Random Effects using package lme4?
我有一个类似这样的模型:
model=lmer(y ~ (1|ID) + Factor.A + Factor.B, data=df)
我想得到随机效应的解,但是我只能得到固定效应的解,使用这个代码:
coef(summary(model))
summary(model)
我也试过这段代码:
coef(model)
但我想这个输出不是为了解决随机效应。是否有使用包 lme4 或其他包获得随机效应解的代码?
我认为清楚地说明您的问题和您正在尝试做的事情会有所帮助。但是,根据评论,我想我知道你想做什么。
正如@Marius 所说,ranef(model)
会给你截距。
软件包 arm
有一个 se.ranef
功能,可以为您提供 "standard errors"。我不确定这些是如何计算的。查看此 link 以确保它正在执行您希望它执行的操作:
https://rdrr.io/cran/arm/man/se.coef.html
总之:
library(lme4)
model=lmer(y ~ (1|ID) + Factor.A + Factor.B, data=df)
ranef(model)
library(arm)
se.ranef(model)
仅使用 lme4
包,您可以最方便地获得 条件模式 以及 条件标准偏差 通过as.data.frame(ranef(fitted_model))
:
library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
as.data.frame(ranef(fm1))
## grpvar term grp condval condsd
## 1 Subject (Intercept) 308 2.2575329 12.070389
## 2 Subject (Intercept) 309 -40.3942719 12.070389
## 3 Subject (Intercept) 310 -38.9563542 12.070389
## ... etc.
我不确定我是否愿意称呼这些 "standard errors" - 这里有一大堆蠕虫关于你可以根据观察到的随机变量的条件值做出什么样的推论......根据Doug Bates
Regarding the terminology, I prefer to call the quantities that are
returned by the ranef
extractor "the conditional modes of the random
effects". If you want to be precise, these are the conditional modes
(for a linear mixed model they are also the conditional means) of the
random effects B given Y = y, evaluated at the parameter estimates.
One can also evaluate the cond[i]tional variance-covariance of B given Y
= y and hence obtain a prediction interval.
我有一个类似这样的模型:
model=lmer(y ~ (1|ID) + Factor.A + Factor.B, data=df)
我想得到随机效应的解,但是我只能得到固定效应的解,使用这个代码:
coef(summary(model))
summary(model)
我也试过这段代码:
coef(model)
但我想这个输出不是为了解决随机效应。是否有使用包 lme4 或其他包获得随机效应解的代码?
我认为清楚地说明您的问题和您正在尝试做的事情会有所帮助。但是,根据评论,我想我知道你想做什么。
正如@Marius 所说,ranef(model)
会给你截距。
软件包 arm
有一个 se.ranef
功能,可以为您提供 "standard errors"。我不确定这些是如何计算的。查看此 link 以确保它正在执行您希望它执行的操作:
https://rdrr.io/cran/arm/man/se.coef.html
总之:
library(lme4)
model=lmer(y ~ (1|ID) + Factor.A + Factor.B, data=df)
ranef(model)
library(arm)
se.ranef(model)
仅使用 lme4
包,您可以最方便地获得 条件模式 以及 条件标准偏差 通过as.data.frame(ranef(fitted_model))
:
library(lme4)
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
as.data.frame(ranef(fm1))
## grpvar term grp condval condsd
## 1 Subject (Intercept) 308 2.2575329 12.070389
## 2 Subject (Intercept) 309 -40.3942719 12.070389
## 3 Subject (Intercept) 310 -38.9563542 12.070389
## ... etc.
我不确定我是否愿意称呼这些 "standard errors" - 这里有一大堆蠕虫关于你可以根据观察到的随机变量的条件值做出什么样的推论......根据Doug Bates
Regarding the terminology, I prefer to call the quantities that are returned by the
ranef
extractor "the conditional modes of the random effects". If you want to be precise, these are the conditional modes (for a linear mixed model they are also the conditional means) of the random effects B given Y = y, evaluated at the parameter estimates. One can also evaluate the cond[i]tional variance-covariance of B given Y = y and hence obtain a prediction interval.