The model:
library(lme4)
data(sleepstudy)
fm1 <- lmer(Reaction ~ Days + (Days|Subject), sleepstudy)
The function coef
is the right approach for extracting individual
differences.
> coef(fm1)$Subject
(Intercept) Days
308 253.6637 19.6662581
309 211.0065 1.8475834
310 212.4449 5.0184067
330 275.0956 5.6529540
331 273.6653 7.3973908
332 260.4446 10.1951151
333 268.2455 10.2436611
334 244.1725 11.5418622
335 251.0714 -0.2848735
337 286.2955 19.0955694
349 226.1950 11.6407008
350 238.3351 17.0814915
351 255.9829 7.4520286
352 272.2687 14.0032989
369 254.6806 11.3395025
370 225.7922 15.2897513
371 252.2121 9.4791308
372 263.7196 11.7513155
These values are a combination of the fixed effects and the variance components
(random effects). You can use summary
and coef
to obtain the coefficients
of the fixed effects.
> coef(summary(fm1))[ , "Estimate"]
(Intercept) Days
251.40510 10.46729
The intercept is 251.4 and the slope (associated with Days
) is 10.4.
These coeffcients are the mean of all subjects. To obtain the random effects,
you can use ranef
.
> ranef(fm1)$Subject
(Intercept) Days
308 2.2585637 9.1989722
309 -40.3985802 -8.6197026
310 -38.9602496 -5.4488792
330 23.6905025 -4.8143320
331 22.2602062 -3.0698952
332 9.0395271 -0.2721709
333 16.8404333 -0.2236248
334 -7.2325803 1.0745763
335 -0.3336936 -10.7521594
337 34.8903534 8.6282835
349 -25.2101138 1.1734148
350 -13.0699598 6.6142055
351 4.5778364 -3.0152574
352 20.8635944 3.5360130
369 3.2754532 0.8722166
370 -25.6128737 4.8224653
371 0.8070401 -0.9881551
372 12.3145406 1.2840295
These values are the variance components of the subjects. Every row
corresponds to one subject. Inherently the mean of each column is zero since
the values correspond to the differences in relation to the fixed effects.
> colMeans(ranef(fm1)$Subject)
(Intercept) Days
4.092529e-13 -2.000283e-13
Note that these values are equal to zero, deviations are due to imprecision
of floating point number representation.
The result of coef(fm1)$Subject
incoporates the fixed effects into
the random effects, i.e., the fixed effect coefficients are added to
the random effects. The results are individual intercepts and slopes.
A couple of different things your post brings up (hopefully you recognize that).
The first relates to deciding random vs fixed effects. In my experience deciding between fixed and random effects has two pieces:
Statistical fit. Assessed using things like a Hausman test, standard fare in most packages like Stata, SAS, R, etc. This will tell you if a random intercept "works" better with your data than a fixed effect.
Theoretical fit. How are you conceptualizing the effect? Is it truly a fixed, unchanging entity, not coming from a theoretical distribution? For example, I have rarely seen States treated like random effects - there are 50 and only 50 states (or 51 +DC, or more if you add territories), and they never change being the same states. Same thing with years when there are a few years in the panel, those are often treated as fixed, because you want to capture a common shock to all observations in that year and quantify it as a fixed effect. Other things, however, are not so clear. I'm doing an analysis of counties - of course you could treat counties as fixed effects, but there are over 3,000, and I don't think anyone would really want to be so focused on a single county. So I'm treating them as a random effect, coming from a distribution. When doing repeated measures, again, the individual is treated as random (representative hopefully of a larger population that has parameters you estimate).
The second issue you bring up is intercept vs slope. In this regard random effects and fixed effects are not comparable. A fixed effect literally just adjusts the intercept for each fixed effect - it captures the mean relationship between the given effect and the outcome variable. What that results in, is the slopes you have are within group effects, because you've already captured the variance attributable to the fixed effects. If you think think that the slope is different for each effect, the interpretation is that the effects moderate the effect of the given variable whose slope you are interested in:
$y=\alpha+\beta x + \Sigma{Z_i\theta_i}$
Where X is your covariate of interest, and Z is your vector of each fixed effect i and $\theta_i$ is the effect. Now, if you are thinking the slope is going to differ for each, you actually create an interaction term or moderator for each effect (which explodes the number of coefficients and cuts your degrees of freedom):
$y=\alpha+\beta x + \Sigma{Z_i\theta_i}+\Sigma{Z_ix\gamma_i}$
$\gamma$ is your vector of coefficients, so to get the slope for any given effect you need to add up the $\beta$ with the relevant $\gamma$. So what does $\beta$ mean here? It is not the average slope across all the effects, as it is in a random effect model. It is the slope for the omitted effect.
So, to get back to your original question:
- Start with the theoretical definition of your effects. Are they truly fixed, or do they reflect a random distribution, that has population parameters you can estimate? Answer that and then I believe you should be on your way to figuring out the next step.
- If you believe that the effects should indeed be random, then do your statistical tests. Test if random effects fit better. Then do your appropriate tests to see if a random coefficient is appropriate.
Best Answer
There seems to be some confusion in your question. The purpose of random effects (intercepts) in a growth model, is to account for the correlations within individuals. The difference in slope that you expect, between boys and girls would be modelled with an interaction between the slope (ie the fixed effect for the time variable) and sex. Additionally, you can allow each subject to have their own slope, or rather an offset from the fixed slope, by fitting random slopes.
You say:
This is correct. Sex is a fixed effect.
In the example in your question you would fit a model such as:
This will estimate an overall linear trend for
time
(the fixed effect fortime
) for both boys and girls (the fixed effect forsex
) and also allow trend to be different for boys and girls (thesex:time
interaction), while also adjusting the dependence between measurements in each person (thesubject
random intercept).As mentioned in the first paragraph, You could also allow each subject to have their own slope by fitting random slopes for
time
: