Reputation: 877
I have a model similar to this:
model=lmer(y ~ (1|ID) + Factor.A + Factor.B, data=df)
I would like to obtain the solution of random effects, but I only could obtain the solution of fixed effects, using this codes:
coef(summary(model))
summary(model)
I tried this code too:
coef(model)
but I suppose this output is not for the solution of random effects. Is there a code to obtain the solution of random effects using the package lme4 or another one?
Upvotes: 1
Views: 1053
Reputation: 2232
I think clearly stating your question, and what you are trying to do would be helpful. However, based on the comments, I think I know what you are trying to do.
As @Marius said, ranef(model)
will give you the intercepts.
the package arm
has a se.ranef
function that gives you "standard errors". I am not sure how these are calculated. See this link to make sure that it is doing what you want it to:
https://rdrr.io/cran/arm/man/se.coef.html
So all together:
library(lme4)
model=lmer(y ~ (1|ID) + Factor.A + Factor.B, data=df)
ranef(model)
library(arm)
se.ranef(model)
Upvotes: 2
Reputation: 226162
Using only the lme4
package, you can most conveniently get the conditional modes along with the conditional standard deviations via 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.
I'm not sure I would be comfortable calling these "standard errors" - there's a whole can of worms here about what kind of inferences you can make on the observed conditional values of random variables ... according to 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.
Upvotes: 3