| variable | description |
|---|---|
| age | Age (years) |
| lifesat | Life Satisfaction score |
| dwelling | Dwelling (town/city in Scotland) |
| size | Size of Dwelling (> or <100k people) |
Extract estimates from a fitted model
Example: Life satisfaction in Scotland
These data come from 112 people across 12 different Scottish dwellings (cities and towns). Information is captured on their ages and a measure of life satisfaction. The researchers are interested in if there is an association between age and life-satisfaction.
Data are available at https://uoepsy.github.io/data/lmm_lifesatscot.csv.
lifesatscot <- read_csv("https://uoepsy.github.io/data/lmm_lifesatscot.csv")Our model is the following (to see how we figured out this random effect structure, see Identify possible random effects).
lifesat_mod <- lmer(
lifesat ~ age + (1 + age | dwelling),
data = lifesatscot
)Extract fixed effects
The function fixef() will return the estimated values for the model’s fixed effects.
fixef(lifesat_mod)(Intercept) age
28.241 0.523
Extract all group-level adjustments
How much does each individual group (here, each dwelling) differ from that fixed intercept and that fixed slope over age?
In other words, how do we need to adjust the fixed intercept and the fixed slope over age to arrive at the best line for each group?
We can extract these adjustments using the function ranef(), which gives us a list containing one element per group. We’ll use the dollar-sign notation to pull out the data frame of adjustments for our grouping variable dwelling:
ranef(lifesat_mod)$dwelling (Intercept) age
Aberdeen -26.62 0.4875
Dumfries -13.54 0.2986
Dundee 16.43 -0.2829
Dunfermline 7.75 0.1815
Edinburgh -5.48 0.2180
Fort William -6.12 -0.0275
Glasgow 24.08 -0.4348
Inverness -12.73 0.2091
Kirkcaldy 14.50 -0.7339
Paisley -3.32 -0.0903
Perth -3.02 0.1901
Stirling 8.07 -0.0154
Each row is a level of the grouping variable (a dwelling in Scotland), and each column shows how that dwelling’s intercept and slope over age differ from the estimated fixed effects. So, since the fixed effects estimate the average intercept and average slope, we can observe (for example) that:
- Aberdeen has an estimated intercept that is 26.62 points lower than average (lower because the value is negative), and an estimated slope over
agethat is 0.48 points higher than average (higher because the value is positive). - Kirkcaldy has an estimated intercept that is 14.50 points higher than average, and an estimated slope over
agethat is 0.73 points lower than average.
Extract group-level intercepts and slopes
To find out the actual parameters of the line estimated for each dwelling, we can use the function coef().
coef(lifesat_mod)$dwelling (Intercept) age
Aberdeen 1.62 1.0110
Dumfries 14.70 0.8221
Dundee 44.67 0.2406
Dunfermline 35.99 0.7049
Edinburgh 22.76 0.7415
Fort William 22.12 0.4959
Glasgow 52.32 0.0886
Inverness 15.51 0.7325
Kirkcaldy 42.74 -0.2105
Paisley 24.92 0.4331
Perth 25.23 0.7136
Stirling 36.31 0.5081
Again, each row is a level of our grouping variable dwelling. Now each column represents the actual intercept and the actual slope of the line estimated for each dwelling.
Using Aberdeen as an example, here’s where these numbers come from:
- Aberdeen’s intercept:
- The fixed intercept is 28.24.
- The adjustment to that fixed intercept is –26.62.
- Combining those two values, we calculate 28.24 – 26.62 = 1.62, which matches the intercept of the line for Aberdeen given by
coef().
- Aberdeen’s slope over
age:- The fixed slope is 0.52.
- The adjustment to that fixed slope is 0.49.
- 0.52 + 0.49 = 1.01, which matches the slope of the line over
agefor Aberdeen given bycoef().
In short: fixef() + ranef() = coef().
Linked flash cards
Outgoing links
- TODO
Backlinks
- TODO