# I am confused with ranef function in R

Do the ranef and fixef functions in lmer give the random and fixed effect coefficients? If not what do they really give?

Data looks something like (this is a fake data):

id      1  1  1  2  2  2
weight 34 45 56 78 12 45
count  23 12 13 16 14 22


Model looks like mod <- lmer(weight ~ count + (1+count|id), data=d1)

where id is a random effect in the model and count is fixed effect.

I believed that the statement coef(model)$id[,"count"] in R will give the random coefficients for count by each id. Am I correct? If yes then what does ranef(mod) give? Any help is much appreciated. Sorry for the confusion. • This post repeatedly references a "below statement" that seems to be missing. Please edit it to include the absent material, which seems to be crucial for its understanding. – whuber Commented Aug 13, 2014 at 14:33 • For better understanding please provide details of your question and the R codes you tried. Commented Aug 14, 2014 at 5:08 • I have edited the question Please let me know if it clear the doubts Commented Aug 14, 2014 at 9:22 ## 1 Answer Example from the lme4 package: fm1 <- lmer(Reaction ~ Days + (Days | Subject), sleepstudy)  For this model coef(fm1) gives $Subject
(Intercept)       Days
308    253.6637 19.6662581
309    211.0065  1.8475834
310    212.4449  5.0184067
...


This is the 'summed up' version, assuming you want to know the subject-specific intercept and subject specific slope for Day. This is constructed by combining the fixed effects, which provide the mean, with the random effects, which are zero centred and provide the variation. fixef(fm1) gives

(Intercept)        Days
251.40510    10.46729


and ranef(fm1) gives

\$Subject
(Intercept)        Days
308   2.2585637   9.1989722
309 -40.3985802  -8.6197026
310 -38.9602496  -5.4488792
...


Looking at subject 308 we see that their personal intercept 253.6637 is equal to the grand mean 251.40510 plus 2.2585637 and their personal slope 19.6662581 is equal to the fixed effect slope 10.46729 plus their personal slope 9.1989722.

The advantage of ranef in all this is that you can get the posterior uncertainty (or whatever it is that lme4 actually computes) over the random effects using ranef(fm1, condVar = TRUE). What you got before were only point estimates of random variables.

• Thank you! Can you explain what does posterior uncertainty mean and how to interpret them? Commented Aug 14, 2014 at 20:26
• You might want to read something on mixed or multilevel models, but in short you can treat the distributions that you get if you, say, dotplot(ranef(fm1, condVar=TRUE)) as measures of uncertainty about what the subject-specific effects are (you'll want to require(lattice) before doing this) in roughly the same way as standard errors work for the fixed effects. Commented Aug 14, 2014 at 21:20
• In case you're looking for further reading, this is all covered in detail with examples in Gelman and Hill's textbook on mixed effects modeling. It's a very, very good reference.
– Sycorax
Commented Aug 17, 2014 at 14:07
• If you have level 1 and level 2 randomized predictors (e.g., year and site), how can I get ranef to give me site and not year as default? Commented Feb 8, 2021 at 16:53