Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
Sociology 761
John Fox
1. Introduction (and Review)
I The standard linear model
l = 1 + 2{2l + + s{sl + %l
%l NID(0> 2)
assumes independently sampled observations, and hence independent
errors %l.
In matrix form,
y = X + %
% Nq(0> 2Iq)
where
y = (1> 2> ===> q)0 is the response vector;
X is the model matrix, with typical row x0l = (1> {2l> ===> {sl);
=( 1> 2> ===> s)0 is the vector of regression coefficients;
% = (%1> %2> ===> %q)0 is the vector of errors;
Nq represents the qvariable multivariatenormal distribution;
Lecture Notes
Introduction to MixedEffects Models
for Hierarchical and Longitudinal Data
(Part I)
Copyright 2004 by John Fox
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
0 is an q 1 vector of zeroes; and
Iq is the order q identity matrix.
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
The standard linear model has one random effect, the error term %l,
and one variance component, 2 = Var(%l).
When the assumptions of the standard linear model hold, ordinaryleastsquares (OLS) regression provides maximumlikelihood estimates of the regression coefficients,
b = (X0X)1X0y
Sociology 761
c
Copyright 2006
by John Fox
The MLE of the error variance
is
0
b
b
y X
y X
b2 =
q
b2 is a biased estimator of 2; usually, the unbiased estimator
b
b
y X
y X
v2 =
qs
is preferred.
I The standard linear model and OLS regression are generally inappropriate for dependent observations.
Dependent (or clustered) data arise in many contexts, the two most
common of which are hierarchical data and longitudinal data.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
I Hierarchical data are collected when sampling takes place at two or
more levels, one nested within the other. Some examples:
Students within schools (two levels).
Students within classrooms within schools (three levels).
Data collected at irregular intervals on recovery of IQ among coma
patients.
Individuals within communities within nations (three levels).
Patients within physicians (two levels).
Annual data on employment and income for a sample of adult
Canadians.
Patients within physicians within hospitals (three levels).
I There can also be nonnested multilevel data for example, highschool students who each have multiple teachers.
I In all of these cases, it is not generally reasonable to assume that observations within the same higherlevel unit, or longitudinal observations
within the same individual, are independent of oneanother.
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
Biannual data on weightpreoccupation and exercise among adolescent girls.
Individuals within nations (two levels).
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
I Longitudinal data are collected when individuals (or other units of
observation) are followed over time. Some examples:
Annual data on vocabulary growth among children.
I Mixedeffect models make it possible to take account of dependencies
in hierarchical, longitudinal, and other dependent data.
Unlike the standard linear model, mixedeffect models include more
than one source of random variation i.e., more than one random
effect.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
What distinguishes modern mixed models from their predecessors
is generality: for example, the ability to accommodate irregular and
missing observations.
Mixedeffects models have been developed in a variety of disciplines,
with varying names and terminology: randomeffects models (statistics, econometrics), variance and covariancecomponent models
(statistics), hierarchical linear models (education), multilevel models
(sociology), contextualeffects models (sociology), randomcoefficient
models (econometrics), repeatedmeasures models (statistics, psychology).
Mixedeffects models have a long history, dating to Fisher and Yatess
work on splitplot agricultural experiments.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
I Principal sources for these lectures on mixed models:
Stephen Raudenbush and Anthony Bryk, Hierarchical Linear Models,
Second Edition, Sage, 2002.
Jose Pinheiro and Douglas Bates, MixedEffects Models in S and
SPLUS, Springer, 2000.
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
2. The Linear MixedEffects Model
I This section introduces a very general linear mixed model, which we will
adapt to particular circumstances.
I The LairdWare form of the linear mixed model:
lm = 1 + 2{2lm + + s{slm + e1l}1lm + + etl}tlm + %lm
enl Q(0> # 2n )> Cov(enl> en0l) = # nn0
enl> en0l0 are independent for l 6= l0
%lm Q(0> 2lmm )> Cov(%lm > %lm 0 ) = 2lmm 0
%lm > %l0m 0 are independent for l 6= l0
where
lm is the value of the response variable for the m th of ql observations
in the lth of P groups or clusters.
I Topics:
The linear mixedeffects model.
Modeling hierarchical data.
Modeling longitudinal data.
Generalized linear mixed models (time permitting).
1> 2> = = = > s are the fixedeffect coefficients, which are identical for all
groups.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
10
{2lm > = = = > {slm are the fixedeffect regressors for observation m in group
l; there is also implicitly a constant regressor, {1lm = 1.
e1l> = = = > etl are the randomeffect coefficients for group l, assumed
to be multivariately normally distributed, independent of the random
effects of other groups. The random effects, therefore, vary by group.
The eln are thought of as random variables, not as parameters, and
are similar in this respect to the errors %lm .
}1lm > = = = > }tlm are the randomeffect regressors.
The } s are almost always a subset of the {s (and may include all of
the { s).
When there is a random intercept term, }1lm = 1.
# 2n are the variances and # nn0 the covariances among the random
effects, assumed to be constant across groups.
In some applications, the # s are parametrized in terms of a smaller
number of fundamental parameters.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
11
%lm is the error for observation m in group l=
The errors for group l are assumed to be multivariately normally
distributed, and independent of errors in other groups.
2lmm 0 are the covariances between errors in group l.
Generally, the lmm 0 are parametrized in terms of a few basic
parameters, and their specific form depends upon context.
When observations are sampled independently within groups and
are assumed to have constant error variance (as is typical in
hierarchical models), lmm = 1, lmm 0 = 0 (for m 6= m 0), and thus the only
free parameter to estimate is the common error variance, 2.
If the observations in a group represent longitudinal data on a
single individual, then the structure of the s may be specified to
capture serial (i.e., overtime) dependencies among the errors.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
12
I The LairdWare model in matrix form:
yl = Xl + Zlbl + %l
bl Nt (0> [)
bl> bl0 are independent for l 6= l0
%l Nql (0> 2\l)
%l> %l0 are independent for l 6= l0
where
yl is the ql 1 response vector for observations in the lth group.
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
13
[ is the t t covariance matrix for the random effects.
2\l is the ql ql covariance matrix for the errors in group l, and is
2Iql if the withingroup errors are homoscedastic and independent.
I Notice that there are two things that distinguish the linear mixed model
from the standard linear model:
(1) There are structured random effects bl in addition to the errors %l.
(2) The model can accommodate heteroscedasticity and dependencies
among the errors.
Xl is the ql s model matrix for the fixed effects for observations in
group l.
is the s 1 vector of fixedeffect coefficients.
Zl is the ql t model matrix for the random effects for observations in
group l=
bl is the t 1 vector of randomeffect coefficients for group l.
%l is the ql 1 vector of errors for observations in group l.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
14
3. Modeling Hierarchical Data
I Applications of mixed models to hierarchical data have become common
in the social sciences, and nowhere more so than in research on
education.
I Ill restrict myself to twolevel models, but three or more levels can also
be handled through an extension of the LairdWare model.
I The following example is borrowed from Raudenbush and Bryk, and has
been used by others as well (though we will learn some things about the
data that apparently havent been noticed before).
The data are from the 1982 High School and Beyond survey, and
pertain to 7185 U.S. highschool students from 160 schools about
45 students on average per school.
70 of the high schools are Catholic schools and 90 are public
schools.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
15
The object of the data analysis is to determine how students math
achievement scores are related to their family socioeconomic status.
We will entertain the possibility that the level of math achievement
and the relationship between achievement and SES vary among
schools.
If there is evidence of variation among schools, we will examine
whether this variation is related to school characteristics in
particular, whether the school is a public school or a Catholic school
and the average SES of students in the school.
I A good place to start is to examine the relationship between math
achievement and SES separately for each school.
160 schools are too many to look at individually, so I sampled 18
Catholic school and 18 public schools at random.
The scatterplots for the sampled schools are in Figures 1 and 2.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
16
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
Catholic
2
5819
1
2
1433
17
Public
1
2336
2
4642
7345
1
2
8628
25
25
20
20
15
15
10
10
1
2
1
2
1
2336
4642
5619
6469
7345
8627
7919
7635
2458
5640
1477
1461
0
3716
6484
9359
6089
2768
3039
25
Math Achievement
Math Achievement
25
20
15
10
5
20
15
10
5
0
8854
8009
9292
3499
8983
5937
4523
25
25
20
20
15
15
10
10
5783
3377
1296
9508
1288
0
2
1
2
1
1
Centered SES
2
1
2
Figure 1. Math achievement by SES for students in 18 randomly selected
Catholic schools. SES is centered at the mean of each school.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
18
In each scatterplot, the broken line is the linear leastsquares fit to the
data, while the solid line gives a nonparametricregression fit. The
number at the top of each panel is the ID number of the school.
Particularly given the relatively small numbers of students in individual
schools, the linear regressions seem to do a reasonable job of
summarizing the relationship between math achievement and SES
within schools.
Although there is substantial variation in the regression lines among
schools, there also seems to be a systematic difference between
Catholic and public schools: The lines for the public schools appear to
have steeper slopes on average.
I SES in these scatterplots is expressed as deviations from the school
mean SES.
That is, the average SES for students in a particular school is
subtracted from each individual students SES.
Sociology 761
c
Copyright 2006
by John Fox
1
2
1
1
Centered SES
2
1
Figure 2. Math achievement by SES for students in 18 randomly selected
public schools.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
19
Centering SES in this manner makes the withinschool intercept from
the regression of math achievement on SES equal to the average
math achievement score in the school:
In the lth school we have the regressing equation
mathachlm = 0l + 1l(seslm sesl) + %lm
where
Pql
m=1 seslm
sesl =
ql
Then the leastsquares estimate P
of the intercept is
ql
m=1 mathachlm
b 0l = mathachl =
ql
A more general point is that it is helpful for interpretation of hierarchical
(and other!) models to scale the explanatory variables so that the
parameters of the model represent quantities of interest.
Sociology 761
c
Copyright 2006
by John Fox
20
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
21
Catholic
(Intercept)
school
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
I Having satisfied myself that linear regressions reasonably represent the
withinschool relationship between math achievement and SES, I fit this
model by least squares to the data from each of the 160 schools.
Here are three displays of these coefficients:
Figures 3 and 4 shows confidence intervals for the intercept and
slope estimates for Catholic and public schools.
Figure 5 shows boxplots of the intercepts and slopes for Catholic
and public schools.
It is apparent that the individual slopes and intercepts are not
estimated very precisely, and there is also a great deal of variation
from school to school.
On average, however, Catholic schools have larger intercepts (i.e.,
a higher average level of math achievement) and lower slopes (i.e.,
less of a relationship between math achievement and SES).
ses

9586
9198
8628
8193
7688
7332
7011
6469
6366
5619
5404
3427
3020
2990
2755
2526
1436
1433
1308
3039
1477
2208
9359
3838
3610
2458
7635
5761
4173
3992
1906
6074
4042
8150
9104
8165
3688
3498
5720
5667
4931
1462
4223
2629
1317
8857
4292
9508
2658
5650
7364
3499
7342
4253
3533
3705
9347
6578
4511
9021
4530
8009
2277
6816
4523
5192
8800
2305
4868
7172













15
20
5









10




























































































































































































































Figure 3. Confidence intervals for leastsquares intercepts (left) and slopes
(right) for the withinschool regressions of math achievement on centered
SES: 70 Catholic high schools.
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
22
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
23
Public































20
5
































15













10

































































































Catholic


Public
Catholic
Public
10
Figure 4. Confidence intervals for leastsquares intercepts (left) and slopes
(right) for the withinschool regressions of math achievement on centered
SES: 90 public high schools.
Sociology 761
Slopes














































Intercepts
















ses

2




20
15
10
school
(Intercept)
8627
8202
7697
7345
7276
4642
3657
3351
3332
3152
2771
2626
2336
1946
1942
1637
1461
6397
5819
2768
6089
5640
9225
8874
8175
7734
6897
6484
4325
3999
4420
8531
8357
2030
6170
2917
7232
8946
9158
5838
2995
3881
6600
1374
2467
2651
1909
3716
7919
5937
6464
9550
6415
3967
1224
6291
1288
8477
1499
8707
4410
8188
8983
9292
2655
9397
4350
1296
3377
2639
7101
3013
5783
9340
2818
6808
6443
6144
7890
8775
3088
4383
1358
7341
5815
6990
5762
4458
8854
8367
c
Copyright 2006
by John Fox
Figure 5. Boxplots of withinschool coefficients for the leastsquares regression of math achievement on schoolcentered SES, for 70 Catholic
and 90 public schools.
Sociology 761
c
Copyright 2006
by John Fox
25
Slope
2
10
20
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
15
24
Figure 6 shows the relationship between the withinschool intercepts
and slopes and mean school SES.
There is a moderately strong and reasonably linear relationship between the withinschool intercepts (i.e., average math achievement)
and the average level of SES in the schools.
The slopes are weakly and, apparently, nonlinearly related to
average SES.
Intercept
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
1.0
0.5
0.0
0.5
Mean SES
1.0
0.5
0.0
0.5
Mean SES
Figure 6. Withinschool intercepts and slopes by mean SES. In each
panel, the broken line is the linear leastsquares fit and the solid line is
from a nonparametric regression.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
26
3.1 Formulating a Mixed Model
I We already have a level1 model for math achievement:
mathachlm = 0l + 1lcseslm + %lm
where cseslm = seslm sesl
I A level2 model relates the coefficients in the level1 model to
characteristics of schools.
Our exploration of the data suggests the following level2 model:
0l = 00 + 01sesl + 02sectorl + x0l
1l = 10 + 11sesl + 12ses2l + 13sectorl + x1l
where sector is a dummy variable, coded 1 (say) for Catholic schools
and 0 for public schools.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
27
I Substituting the schoollevel equation into the individuallevel equation
produces the combined or composite model:
mathachlm = ( 00 + 01sesl + 02sectorl + x0l)
+ 10 + 11sesl + 12ses2l + 13sectorl + x1l cseslm + %lm
= 00 + 01sesl + 02sectorl + 10cseslm
+ 11sesl cseslm + 12ses2l cseslm + 13sectorl cseslm
+x0l + x1lcseslm + %lm
Except for notation, this is a mixed model in LairdWare form, as we
can see by replacing s with s and xs with es:
lm = 1 + 2{2lm + 3{3lm + 4{4lm
+ 5{5lm + 6{6lm + 7{7lm
+e1l + e2l}2lm + %lm
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
28
Note that all explanatory variables in the LairdWare form of the model
carry subscripts l for schools and m individuals within schools, even
when the explanatory variable in question is constant within schools.
Thus, for example, {2lm = sesl (and so all individuals in the same
school share a common value of schoolmean SES).
There is both a datamanagement issue here and a conceptual
point:
With respect to data management, software that fits the LairdWare
form of the model (such as the lme or lmer functions in R) requires
that level2 explanatory variables (here sector and schoolmean
SES, which are characteristics of schools) appear in the level1 (i.e.,
student) data set much as the person timeperiod data set
that we employed in survival analysis with timevarying covariates
required that timeconstant covariates appear on the data record for
each time period.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
30
29
The conceptual point is that the model can incorporate contextual
effects characteristics of the level2 units can influence the level1
response variable.
Such contextual effects are of two kinds:
Compositional effects, such as the effect of schoolmean SES,
which are composed from characteristics of individuals within a
level2 unit.
Effects of characteristics of the level2 units, such as school sector,
that do not pertain to the level1 units.
I Rather than proceeding with this relatively complicated model, let us
first investigate some simpler mixedeffects models derived from it.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
31
%lm , representing the deviation of individual m s math achievement in
school l from the school mean.
3.1.1 RandomEffects OneWay Analysis of Variance
I Consider the following level1 and level2 models:
mathachlm = 0l + %lm
0l = 00 + x0l
Two observations lm and lm 0 in school l are not independent because
they share the random effect, e1l.
I There are also two variance components for this model:
# 21 = Var(e1l) is the variance among school means.
I The combined model is
mathachlm = 00 + x0l + %lm
In LairdWare form:
lm = 1 + e1l + %lm
2 = Var(%lm ) is the variance among individuals in the same school.
I This is a randomeffects oneway ANOVA model with one fixed effect,
1, representing the general population mean of math achievement, and
two random effects:
e1l, representing the deviation of math achievement in school l from
the general mean; that is, l = 1 + e1l is mean math achievement in
school l.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
I Because the e1l and %lm are assumed to be independent, variation in
math scores among individuals can be decomposed into these two
variance components:
Var(lm ) = H (e1l + %lm )2 = # 21 + 2
[since H(e1l) = H(%lm ) = 0, and hence H(lm ) = 1].
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
32
The intraclass correlation coefficient is the proportion of variation in
individuals scores due to differences among schools:
# 21
#2
=
= 2 1 2
Var(lm ) #1 +
may also be interpreted as the correlation between the math scores
of two individuals from the same school. That is,
Cov(lm > lm 0 ) = H [(e1l + %lm )(e1l + %lm 0 )] = H(e21l) = # 21
Var(lm ) = Var(lm 0 ) = # 21 + 2
#2
Cov(lm > lm 0 )
= 2 1 2 =
Cor(lm > lm 0 ) = p
Var(lm ) Var(lm 0 ) #1 +
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
33
I The lme function in the nlme package in R provides two methods to
estimate mixedeffects models (as does the lmer function in the lme2
package):
Full maximumlikelihood (ML) estimation of the model maximizes
the likelihood with respect to all of the parameters of the model
simultaneously (i.e., both the fixedeffects parameters and the
variance components).
Restricted (or residual) maximumlikelihood (REML) estimation
integrates the fixed effects out of the likelihood and estimates
the variance components; given the estimates of the variance
components, estimates of the fixed effects are recovered.
A disadvantage of ML estimates of variance components is that they
are biased downwards in finite samples (much as the ML estimate of
the error variance in the standard linear model is biased downwards).
The REML estimates, in contrast, correct for loss of degrees of
freedom due to estimating the fixed effects.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
34
The difference between the ML and REML estimates can be important
when the number of clusters (i.e., level2 units) in the data is small.
I ML and REML estimates for the current example, where there are 160
schools (level2 units), are nearly identical:
Parameter ML Estimate REML Estimate
1
12=637
12=637
2=925
2=935
#1
6=257
6=257
Note that the standard deviations (rather than the variances) of the
random effects are shown.
The estimated intraclass correlation coefficient is
2=9352
b
=
= 0=180
2=9352 + 6=2572
and so 18 percent of the variation in students mathachievement
scores is attributable to differences among schools.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
35
3.1.2 RandomCoefficients Regression Model
I Let us introduce schoolcentered SES into the level1 model as an
explanatory variable,
mathachlm = 0l + 1lcseslm + %lm
and allow for random intercepts and slopes in the level2 model:
0l = 00 + x0l
1l = 10 + x1l
I The combined model is now
mathachlm = ( 00 + x0l) + ( 10 + x1l) cseslm + %lm
= 00 + 10cseslm + x0l + x1lcseslm + %lm
In LairdWare form:
lm = 1 + 2{2lm + e1l + e2l}2lm + %lm
I This model is a randomcoefficients regression model.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
36
I The fixedeffects coefficients 1 and 2 represent, respectively, the
average withinschools population intercept and slope.
Because SES is centered within schools, the intercept 1 represents
the general level of math achievement in the population (in the sense
of the average withinschool mean).
I The model has four variancecovariance components:
# 21 = Var(e1l) is the variance among school intercepts (i.e., school
means, because SES is schoolcentered).
# 22 = Var(e2l) is the variance among withinschool slopes.
# 12 = Cov(e1l> e2l) is the covariance between withinschool intercepts
and slopes.
2 = Var(%lm ) is the error variance around the withinschool regressions.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
38
I ML and REML estimates for the model are as follows:
Parameter ML Estimate Std. Error REML Estimate Std. Error
1
12=636
0=244
12=636
0=245
2=193
0=128
2=193
0=128
2
2=936
2=946
#1
0=823
0=833
#2
0=041
0=042
# 12
6=058
6=058
Again, the ML and REML estimates are very close.
Note that Ive given standard errors only for the fixed effects.
Standard errors for variance and covariance components can be
obtained in the usual manner from the inverse of the information
matrix, but tests and confidence intervals based on these standard
errors tend not to be accurate.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
37
I The composite error for individual m in school l is
lm = e1l + e2l}2lm + %lm
The variance of thecomposite error is
2
Var( lm ) = H( 2lm ) = H (e1l + e2l}2lm + %lm )2 = # 21 + }2lm
#22 + 2}2lm #12 + 2
And the covariance of the composite errors for two individuals m and m 0
in the same school is
Cov( lm > lm 0 ) = H( lm lm 0 ) = H [(e1l + e2l}2lm + %lm )(e1l + e2l}2lm 0 + %lm 0 )]
= #21 + }2lm }2lm 0 #22 + (}2lm + }2lm 0 )#12
Consequently the composite errors are heteroscedastic, and errors for
individuals in the same school are correlated.
But the composite errors for two individuals in different schools are
independent.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
39
We can, however, test variance and covariance components by a
likelihoodratio test, contrasting the (restricted) loglikelihood for the
fitted model with that for a model removing the random effects in
question.
For example, for the current model (say model 1), removing el2}2lm
from the model (producing, say, model 0) implies that the SES slope
is identical across schools.
Removing el2}2lm from the model gets rid of two variancecovariance
parameters, # 2 and #12.
A likelihoodratio test for these parameters suggests that they
should not be omitted from the model:
logh O1 = 23> 357=12
logh O0 = 23> 362=00
J2 = 2(logh O1 logh O0) = 9=76, gi = 2, s = =008
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
40
I Cautionary Remarks:
Because REML estimates are calculated integrating out the fixed
effects, one cannot legitimately perform likelihoodratio tests across
models with different fixed effects when the models are estimated by
REML.
Likelihoodratio for variancecovariance components across nested
models with identical fixed effects are perfectly fine, however.
A common source of estimation difficulties in mixed models is the
specification of overly complex random effects.
Interest usually centers in the fixed effects, and it often pays to try to
simplify the randomeffect part of the model.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
42
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
41
3.1.3 CoefficientsasOutcomes Model
I The regressioncoefficientsasoutcomes model introduces explanatory
variables at level 2 to account for variation among the level1 regression
coefficients. This returns us to the model that we originally formulated
for the mathachievement data:
at level 1,
mathachlm = 0l + 1lcseslm + %lm
at level 2,
0l = 00 + 01sesl + 02sectorl + x0l
1l = 10 + 11sesl + 12ses2l + 13sectorl + x1l
The combined model:
mathachlm = 00 + 01sesl + 02sectorl + 10cseslm
+ 11sesl cseslm + 12ses2l cseslm + 13sectorl cseslm
+x0l + x1lcseslm + %lm
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
43
Thus, the test for random intercepts is highly statistically significant,
J2 = 219=86, gi = 2, s ' 0.
The combined model in LairdWare form:
lm = 1 + 2{2lm + 3{3lm + 4{4lm
+ 5{5lm + 6{6lm + 7{7lm
+e1l + e2l}2lm + %lm
This model has more fixed effects than the preceding randomcoefficients regression model, but the same random effects and
variance components: # 21 = Var(e1l), # 22 = Var(e2l), #12 = Cov(e1l> e2l),
and 2 = Var(%lm ).
But the test for random slopes is not, J2 = 0=46, gi = 2, s = =80:
Apparently, the level2 explanatory variables do a sufficiently good job
of accounting for differences in slopes that the variance component for
slopes is no longer needed.
I After fitting this model to the data by REML, I tested to check whether
random intercepts and slopes are still required:
Model Omitting logh O
1
23> 247=70
2
# 21> # 12 23> 357=86
3
# 22> # 12 23> 247=93
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
44
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
45
These estimates, all of which are statistically significant, have the
following interpretations:
b1 = 12=128 is the estimated general level of math achievement
in public schools (where the dummy variable sector is coded 0) at
mean school SES.
The interpretation of this coefficient depends upon the fact that
sesl (school SES) is centered to a mean of 0 across schools.
b
2 = 5=337 is the estimated increase in mean math achievement
associated with a oneunit increase in school SES.
b3 = 1=225 is the estimated difference in mean math achievement
between Catholic and public schools at fixed levels of school SES.
b1,
b2, and
b3, therefore, describe the betweenschools regression
of mean math achievement on school characteristics.
c
Copyright 2006
by John Fox
46
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
Public
Catholic
1
Level1 SES Effect
b4,
b5,
b6, and
b7 combine to
Figure 7 shows how the coefficients
produce the level1 (i.e., withinschool) coefficient for SES.
At fixed levels of school SES, individual SES is more positively
related to math achievement in public than in Catholic schools.
The maximum positive effect of individual SES is in schools with a
slightly higher than average SES level; the effect declines at low
and high levels of school SES, and becomes negative at the lowest
levels of school SES.
An alternative, and more intuitive representation of the fitted model
is shown in Figure 8, which graphs the fitted withinschool regression
of math achievement on centered SES for Catholic and public
schools and for three levels of school SES: 0=7 (the approximate
10th percentile of school SES), 0 (the median), and 0=7 (the 90th
percentile).
47
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
I Refitting the model removing el2}2lm produces the following REML
estimates:
REML Estimate Std. Error
Parameter Term
1
intercept
12=128
0=199
2
sesl
5=337
0=369
sectorl
1=225
0=306
3
cseslm
3=140
0=166
4
sesl cseslm
0=755
0=308
5
ses2l cseslm
1=647
0=575
6
sectorl cseslm
1=516
0=237
7
#1
(intercept)
1=541
(%lm )
6=060
1.5
1.0
0.5
0.0
0.5
1.0
Mean School SES
Figure 7. The level1 effect of SES as a function of type of school (Catholic
or public) and mean school SES.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
48
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
49
4. Modeling Longitudinal Data
Catholic Schools
15
20
I In most respects, modeling longitudinal data where there are multiple
observations on individuals who are followed over time is similar to
modeling hierarchical data.
We can think of individuals as analogous to level2 units, and
measurement occasions as analogous to level1 units.
Just as it is generally unreasonable to suppose in hiearchical data that
observations for individuals in the same level2 unit are independent,
so is it generally unreasonable to suppose that observations taken on
different occasions for the same individual are independent.
10
Math Achievement
15
10
5
Math Achievement
20
Public Schools
An additional complication in longitudinal data is that it may no longer
be reasonable to assume that the level1 errors %lm are independent,
since observations taken close in time on the same individual may well
be more similar than observations farther apart in time.
Low School SES
Medium School
High School SES
3
2
1
SES
3
2
1
SES
Figure 8. Fitted withinschool regressions of math achievement on centered SES for public and Catholic schools at three levels of mean school
SES.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
50
When this happens, we say that the errors are autocorrelated.
The linear mixed model makes provision for autocorrelated errors.
I In composing a mixed model for longitudinal data, we can work either
with the hierarchical form of the model or with the composite (LairdWare) form.
I Consider the following example, drawn from work by Blackmoor, Davis,
and Fox on the exercise histories of 138 teenaged girls who were
hospitalized for eating disorders and of 93 control subjects.
There are several observations for each subject, but because the girls
were hospitalized at different ages, the number of observations and
the age at the last observation vary.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
51
The variables in the data set are:
subject: an identification number, necessary to keep track of which
observations belong to each subject.
age: the subjects age, in years, at the time of observation. All but
the last observation for each subject were collected retrospectively
at intervals of two years, starting at age eight. The age at the last
observation is recorded to the nearest day.
exercise: the amount of exercise in which the subject engaged,
expressed as hours per week.
group: a factor indicating whether the subject is a patient or a
control.
It is of interest here to determine the typical trajectory of exercise
over time, and to establish whether this trajectory differs between
eatingdisordered and control subjects.
Preliminary examination of the data suggests a log transformation of
exercise.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Figure 10 shows the exercise trajectories for 20 randomly selected
control subjects and 20 randomly selected patients.
The small number of observations per subject and the substantial
irregular intrasubject variation make it hard to draw conclusions, but
there appears to be a more consistent pattern of increasing exercise
among patients than among the controls.
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
54
Patients
8 10 12 14 16
log2Exercise
309
317
127
161
8 10 12 14 16
196
338
141
162
8 10 12 14 16
152
337
325
175
8 10 12 14 16
121
189
104
329
8 10 12 14 16
150
149
163
307
4
2
0
2
4
4
2
0
2
4
8 10 12 14 16
8 10 12 14 16
8 10 12 14 16
Age
8 10 12 14 16
8 10 12 14 16
Control Subjects
8 10 12 14 16
log2Exercise
210
217
231
263
8 10 12 14 16
283
234
213
201
8 10 12 14 16
227
229a
250
258
8 10 12 14 16
281
273a
236
205
8 10 12 14 16
204
235
215
229b
4
2
0
2
4
4
2
0
2
4
8 10 12 14 16
8 10 12 14 16
8 10 12 14 16
Age
8 10 12 14 16
8 10 12 14 16
53
2
0
2
log2exercise
10 15 20 25 30
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
52
Because about 12 percent of the data values are 0, it is necessary
to add a small constant to the data before taking logs. I used
5@60 = 1@12 (i.e., 5 minutes).
An alternative would be to fit a model (such as an appropriate
generalized linear model) that takes explicit account of the nonnegative character of the response variable.
Figure 9, for example, shows that the original exercise scores are
highly skewed, but that the logtransformed scores are much more
symmetrically distributed.
Exercise (hours/week)
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
control
patient
control
patient
Figure 9. Boxplots of exercise and logexercise for controls and patients,
for measurements taken on all occasions. Note that logs are to the base
2.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
55
With so few observations per subject, and without clear evidence that
it is inappropriate, we would be loath to fit a model more complicated
than a linear trend.
I A linear growth curve characterizing subject ls trajectory suggests the
level1 model
log exerciselm = 0l + 1l(agelm 8) + %lm
I have subtracted 8 from age, and so 0l represents the level of
exercise at 8 years of age the start of the study.
I Our interest in detecting differences in exercise histories between
subjects and controls suggests the level2 model
0l = 00 + 01groupl + x0l
1l = 10 + 11groupl + x1l
where group is a dummy variable coded 1 for subjects (say) and 0 for
controls.
Figure 10. Exercise trajectories for 20 randomly selected patients and 20
randomly selected controls.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
56
I Substituting the level2 model into the level1 model produces the
combined model
log exerciselm = ( 00 + 01groupl + x0l)
+( 10 + 11groupl + x1l)(agelm 8) + %lm
= 00 + 01groupl + 10(agelm 8)
+ 11groupl (agelm 8) + x0l + x1l(agelm 8) + %lm
or, in LairdWare form,
lm = 1 + 2{2lm + 3{3lm + 4{4lm + e1 + e2}2lm + %lm
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
58
Letting model 1 represent the model above, I tested whether random
intercepts or random slopes could be omitted from the model:
logh O
Model Omitting
1
1807=07
2
#21> #12 (random intercepts) 1911=04
3
#22> #12 (random slopes)
1816=13
Both likelihoodratio tests are highly statistically significant (particularly the one for random intercepts), suggesting that both random
intercepts and random slopes are required.
I The model that I have fit to the Blackmoor et al. data assumes
independent errors, %lm .
The composite errors, lm = e1 + e2}2lm + %lm , are correlated within
individuals, however, as we previously established for mixed models
applied to hierarchical data.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
57
I Fitting this model to the data produces the following estimates of the
fixed effects and variancecovariance components:
REML Estimate Std. Error
Parameter Term
1
intercept
0=2760
0=1824
groupl
0=3540
0=2353
2
agelm 8
0=0640
0=0314
3
groupl (agelm 8)
0=2399
0=0394
4
#1
(intercept)
1=4435
(agelm 8)
0=1648
#2
#12
(intercept, agelm 8)
0=0668
(%lm )
1=2441
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
59
In the current context }2lm is the time of observation (i.e., age minus
eight years), and the variance and covariances of the composite
residuals are (as we previously established)
2
Var( lm ) = # 21 + }2lm
# 22 + 2}2lm # 12 + 2
Cov( lm > lm 0 ) = # 21 + }2lm }2lm 0 #22 + (}2lm + }2lm 0 )#12
The actual observations are not taken at entirely regular intervals, but
assume that we have observations for the same individual l taken at
}2l1 = 0> }2l2 = 2> }2l3 = 4> and }2l4 = 6 (i.e., at 8, 10, 12, and 14 years
of age).
Then the estimated covariance
5 matrix for the composite6 errors is
3=631 1=950 1=816 1=683
9 1=950 3=473 1=900 1=875 :
d ( > > > ) = 9
:
Cov
l1 l2 l3 l4
7 1=816 1=900 3=532 2=068 8
1=683 1=875 2=068 3=808
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
60
and the correlations for the composite
errors are
5
6
1=0 =549 =507 =453
9
:
d ( > > > ) = 9 =549 1=0 =543 =516 :
Cor
l1 l2 l3 l4
7 =507 =543 1=0 =564 8
=453 =516 =564 1=0
The correlations across composite errors are moderately high, and
the pattern is what we would expect: The correlations tend to decline
with the timeseparation between occasions. This pattern, however,
does not have to hold.
I The linear mixed model allows for correlated level1 errors within
individuals,
%l Nql (0> 2\l)
For a model with correlated errors to be identified, however, the matrix
\l cannot consist of independent parameters; instead, the elements
of this matrix are expressed in terms of a much smaller number of
fundamental parameters.
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
0.7
0.7
1.0
0.5
s
0.5
0.0
I
Us
0.0
6
lag s
10
10
lag s
Figure 11. Autocorrelation functions for ! = =7 (left) and ! = =7 (right).
Sociology 761
Then the autocorrelation between two errors one timeperiod apart
(i.e., at lag 1) is (1) = !, and the autocorrelation between two errors
v timeperiods apart (at lag v) is (v) = !v.
Figure 11 shows two autocorrelation functions corresponding to firstorder autoregressive processes, one for ! = =7, and the other for
! = =7.
Note that in both cases, the autocorrelations decay as the lag grows.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
63
The occasions for the Blackmoor et al. data are not equally spaced,
however.
For data such as these, lme provides a continuous firstorder
autoregressive process, with the property that
corr(%lw> %l>w+v) = (v) = !v
where the timeinterval between observations, v, need not be an
integer.
0.8
0.6
0.2
0.4
I
U s
61
For example, for equally spaced occasions, a very common model for
the intraindividual errors is the firstorder autoregressive [or AR(1)]
process:
%lm = !%l>m1 + m
where
2
m QLG(0> )
and
! ? 1
The lme function in R provides several other timeseries error
processes for equally spaced observations besides AR(1), as well as
the possibility of adding still more such processes.
1.0
62
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
I I tried to fit the same mixedeffects model to the data as before, except
allowing for firstorder autoregressive level1 errors.
The estimation process did not converge; a closer inspectation
suggests that the model has redundant parameters.
I then fit two additional models, retaining autocorrelated withinsubject
errors, but omitting in turn random slopes and random intercepts.
Sociology 761
c
Copyright 2006
by John Fox
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
64
These models are not nested, so they cannot be compared via
likelihoodratio tests, but we can still compare the fit of the models to
the data:
LogLikelihood gi
Model
Independent withinsubject errors,
1807=068 8
random intercepts and slopes
Correlated withinsubject errors,
1795=484 7
random intercepts
Correlated withinsubject errors,
1802=294 7
random slopes
Thus, the randomintercept model with autocorrelated withinsubject
errors produces the best fit to the data.
Tradingoff parameters for the dependence of the withinsubject
errors against random effects is a common pattern: All three models
produce similar estimates of the fixed effects.
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
c
Copyright 2006
by John Fox
66
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
65
Estimates for a final model, incorporating random intercepts and
autocorrelated errors, are as follows:
REML Estimate Std. Error
Parameter Term
1
intercept
0=3070
0=1895
groupl
0=2838
0=2447
2
agelm 8
0=0728
0=0317
3
groupl (agelm 8)
0=2274
0=0397
4
#1
(intercept)
1=1497
(%lm )
1=5288
(error autocorrelation at lag 1)
0=6312
!
b3) is statistically significant,
Notice that the slope for the control group (
and the differences in slopes between the patient group and the
b4) is highly statistically significant.
controls (
b2, the estimated
The initial difference between the groups (i.e.,
difference at age 8) is nonsignificant.
c
Copyright 2006
by John Fox
Sociology 761
Introduction to MixedEffects Models for Hierarchical and Longitudinal Data (Part I)
67
Patients
Controls
Exercise (hours/week)
A graph showing the fit of the model, translating back from logexercise
to the exercise scale, appears in Figure 12.
10
12
14
16
18
Age (years)
Figure 12. Fitted exercise as a function of age and group: Average trajectories based on fixed effects.
Sociology 761
c
Copyright 2006
by John Fox
Sociology 761
c
Copyright 2006
by John Fox