Professional Documents
Culture Documents
Roberto G. Gutierrez
Director of Statistics
StataCorp LP
R. Gutierrez (StataCorp)
1 / 30
Outline
One-Level Models
Two-Level Models
Factor Notation
R. Gutierrez (StataCorp)
2 / 30
Outline
One-Level Models
Two-Level Models
Factor Notation
R. Gutierrez (StataCorp)
2 / 30
Outline
One-Level Models
Two-Level Models
Factor Notation
R. Gutierrez (StataCorp)
2 / 30
Outline
One-Level Models
Two-Level Models
Factor Notation
R. Gutierrez (StataCorp)
2 / 30
Outline
One-Level Models
Two-Level Models
Factor Notation
R. Gutierrez (StataCorp)
2 / 30
Definition
y = X + Zu +
where
y is the n 1 vector of responses
X is the n p fixed-effects design matrix
are the fixed effects
Z is the n q random-effects design matrix
u are the random effects
is the n 1 vector of errors such that
G
0
u
N 0,
0 2 In
R. Gutierrez (StataCorp)
3 / 30
Variance components
R. Gutierrez (StataCorp)
4 / 30
Variance components
R. Gutierrez (StataCorp)
4 / 30
Variance components
R. Gutierrez (StataCorp)
4 / 30
Panel representation
Classical representation has roots in the design literature, but can
make model specification hard
When the data can be thought of as M independent panels, it is more
convenient to express the mixed model as (for i = 1, ..., M)
yi = Xi + Zi ui + i
where ui N(0, S), for q q variance S, and
Z1 0
0
u1
0 Z2
0
.
Z = ..
.. . .
.. ; u = .. ;
.
.
.
.
uM
0 0
0 ZM
R. Gutierrez (StataCorp)
G = IM S
5 / 30
Panel representation
Classical representation has roots in the design literature, but can
make model specification hard
When the data can be thought of as M independent panels, it is more
convenient to express the mixed model as (for i = 1, ..., M)
yi = Xi + Zi ui + i
where ui N(0, S), for q q variance S, and
Z1 0
0
u1
0 Z2
0
.
Z = ..
.. . .
.. ; u = .. ;
.
.
.
.
uM
0 0
0 ZM
R. Gutierrez (StataCorp)
G = IM S
5 / 30
R. Gutierrez (StataCorp)
6 / 30
R. Gutierrez (StataCorp)
6 / 30
R. Gutierrez (StataCorp)
6 / 30
R. Gutierrez (StataCorp)
6 / 30
One-level models
Example
Consider the Junior School Project data which compares math scores
of various schools in the third and fifth years
Data on n = 887 pupils in M = 48 schools
Lets fit the model
math5ij = 0 + 1 math3ij + ui + ij
for i = 1, ..., 48 schools and j = 1, ..., n i pupils. ui is a random effect
(intercept) at the school level
R. Gutierrez (StataCorp)
7 / 30
One-level models
Example
Consider the Junior School Project data which compares math scores
of various schools in the third and fifth years
Data on n = 887 pupils in M = 48 schools
Lets fit the model
math5ij = 0 + 1 math3ij + ui + ij
for i = 1, ..., 48 schools and j = 1, ..., n i pupils. ui is a random effect
(intercept) at the school level
R. Gutierrez (StataCorp)
7 / 30
Coef.
math3
_cons
.6088557
30.36506
Std. Err.
.0326751
.3531615
Random-effects Parameters
z
18.63
85.98
=
=
=
=
=
887
48
5
18.5
62
=
=
347.21
0.0000
P>|z|
0.000
0.000
.5448137
29.67287
.6728978
31.05724
Estimate
Std. Err.
sd(_cons)
2.038896
.3017985
1.525456
2.72515
sd(Residual)
5.306476
.1295751
5.058495
5.566614
school: Identity
8 / 30
For the most part, the previous is what you would get using xtreg
Consider instead the model
math5ij = 0 + 1 math3ij + u0i + u1i math3ij + ij
In essence, each school has its own random regression line such that
the intercept is N(0 , 02 ) and the slope on math3 is N(1 , 12 )
R. Gutierrez (StataCorp)
9 / 30
For the most part, the previous is what you would get using xtreg
Consider instead the model
math5ij = 0 + 1 math3ij + u0i + u1i math3ij + ij
In essence, each school has its own random regression line such that
the intercept is N(0 , 02 ) and the slope on math3 is N(1 , 12 )
R. Gutierrez (StataCorp)
9 / 30
For the most part, the previous is what you would get using xtreg
Consider instead the model
math5ij = 0 + 1 math3ij + u0i + u1i math3ij + ij
In essence, each school has its own random regression line such that
the intercept is N(0 , 02 ) and the slope on math3 is N(1 , 12 )
R. Gutierrez (StataCorp)
9 / 30
Coef.
math3
_cons
.6135888
30.36542
Std. Err.
.0442106
.3596906
z
13.88
84.42
Number of obs
Number of groups
Obs per group: min
avg
max
=
=
=
=
=
887
48
5
18.5
62
Wald chi2(1)
Prob > chi2
=
=
192.62
0.0000
P>|z|
0.000
0.000
.5269377
29.66044
.7002399
31.0704
Random-effects Parameters
Estimate
Std. Err.
school: Independent
sd(math3)
sd(_cons)
.1911842
2.073863
.0509905
.3078237
.113352
1.550372
.3224593
2.774112
sd(Residual)
5.203947
.1309477
4.953521
5.467034
R. Gutierrez (StataCorp)
10 / 30
Predict
Random effects are not estimated, but the can be predicted (BLUPs)
value
label
r1
float %9.0g
r0
float %9.0g
. gen b0 = _b[_cons] + r0
. gen b1 = _b[math3] + r1
. bysort school: gen tolist = _n==1
R. Gutierrez (StataCorp)
variable label
BLUP r.e. for school: math3
BLUP r.e. for school: _cons
11 / 30
Predict
Random effects are not estimated, but the can be predicted (BLUPs)
value
label
r1
float %9.0g
r0
float %9.0g
. gen b0 = _b[_cons] + r0
. gen b1 = _b[math3] + r1
. bysort school: gen tolist = _n==1
R. Gutierrez (StataCorp)
variable label
BLUP r.e. for school: math3
BLUP r.e. for school: _cons
11 / 30
Predict
b0
b1
1.
26.
36.
44.
68.
1
2
3
4
5
27.52259
30.35573
31.49648
28.08686
30.29471
.5527437
.5036528
.5962557
.7505417
.5983001
93.
106.
116.
142.
163.
6
7
8
9
10
31.04652
31.93729
30.83009
27.90685
31.31212
.5532793
.6756551
.6885387
.6950143
.7024184
R. Gutierrez (StataCorp)
12 / 30
Option fitted
We could use these intercepts and slopes to plot the estimated lines for
each school. Equivalently, we could just plot the fitted values
R. Gutierrez (StataCorp)
13 / 30
Option fitted
We could use these intercepts and slopes to plot the estimated lines for
each school. Equivalently, we could just plot the fitted values
R. Gutierrez (StataCorp)
13 / 30
40
Fitted values: xb + Zu
30
35
25
20
15
10
10
math3
R. Gutierrez (StataCorp)
14 / 30
Covariance structures
In our previous model, it was assumed that u 0i and u1i are independent.
That is,
2
0 0
S=
0 12
R. Gutierrez (StataCorp)
15 / 30
Covariance structures
In our previous model, it was assumed that u 0i and u1i are independent.
That is,
2
0 0
S=
0 12
R. Gutierrez (StataCorp)
15 / 30
Coef.
math3
_cons
.6123977
30.34799
Std. Err.
.0428514
.374883
z
14.29
80.95
=
=
=
=
=
887
48
5
18.5
62
=
=
204.24
0.0000
P>|z|
0.000
0.000
.5284104
29.61323
.696385
31.08274
Random-effects Parameters
Estimate
Std. Err.
school: Unstructured
var(math3)
var(_cons)
cov(math3,_cons)
.0343031
4.872801
-.3743092
.0176068
1.384916
.1273684
.012544
2.791615
-.6239466
.0938058
8.505537
-.1246718
var(Residual)
26.96459
1.346082
24.45127
29.73624
R. Gutierrez (StataCorp)
16 / 30
Notes
R. Gutierrez (StataCorp)
17 / 30
Notes
R. Gutierrez (StataCorp)
17 / 30
Notes
R. Gutierrez (StataCorp)
17 / 30
ML vs. REML
R. Gutierrez (StataCorp)
18 / 30
ML vs. REML
R. Gutierrez (StataCorp)
18 / 30
ML vs. REML
R. Gutierrez (StataCorp)
18 / 30
ML vs. REML
R. Gutierrez (StataCorp)
18 / 30
ML vs. REML
R. Gutierrez (StataCorp)
18 / 30
ML vs. REML
R. Gutierrez (StataCorp)
18 / 30
Two-level models
Example
Baltagi et al. (2001) estimate a Cobb-Douglas production function
examining the productivity of public capital in each states private output.
For y equal to the log of the gross state product measured each year from
1970-1986, the model is
yij = Xij + ui + vj(i ) + ij
for j = 1, ..., Mi states nested within i = 1, ..., 9 regions. X consists of
various economic factors treated as fixed effects.
R. Gutierrez (StataCorp)
19 / 30
. xtmixed gsp private emp hwy water other unemp || region: || state:
Mixed-effects REML regression
Number of obs
=
Group Variable
No. of
Groups
region
state
9
48
Log restricted-likelihood =
gsp
Coef.
private
emp
hwy
water
other
unemp
_cons
.2660308
.7555059
.0718857
.0761552
-.1005396
-.0058815
2.126995
R. Gutierrez (StataCorp)
816
90.7
17.0
Wald chi2(6)
Prob > chi2
1404.7101
Std. Err.
.0215471
.0264556
.0233478
.0139952
.0170173
.0009093
.1574864
136
17
z
12.35
28.56
3.08
5.44
-5.91
-6.47
13.51
P>|z|
0.000
0.000
0.002
0.000
0.000
0.000
0.000
=
=
18382.39
0.0000
.3082624
.8073579
.1176464
.1035853
-.0671862
-.0040994
2.435663
20 / 30
Random-effects Parameters
Estimate
Std. Err.
sd(_cons)
.0435471
.0186292
.0188287
.1007161
sd(_cons)
.0802737
.0095512
.0635762
.1013567
sd(Residual)
.0368008
.0009442
.034996
.0386987
region: Identity
state: Identity
R. Gutierrez (StataCorp)
chi2(2) =
1162.40
21 / 30
Estimate
Std. Err.
sd(hwy)
sd(unemp)
sd(_cons)
.0052752
.0052895
.0596008
.0108846
.001545
.0758296
.0000925
.002984
.0049235
.3009897
.0093764
.721487
sd(_cons)
.0807543
.009887
.0635259
.1026551
sd(Residual)
.0353932
.000914
.0336464
.0372307
region: Independent
state: Identity
R. Gutierrez (StataCorp)
chi2(4) =
1199.67
22 / 30
Estimate
Std. Err.
region: Identity
sd(hwy unemp)
.0052896
.0015446
.0029844
.0093752
sd(_cons)
.0595029
.0318238
.0208589
.1697401
sd(_cons)
.080752
.0097453
.0637425
.1023006
sd(Residual)
.0353932
.0009139
.0336465
.0372306
region: Identity
state: Identity
R. Gutierrez (StataCorp)
chi2(3) =
1199.67
23 / 30
Factor Notation
Sometimes random effects are crossed rather than nested
Example
Consider a dataset consisting of weight measurements on 48 pigs at each
of 9 weeks. We wish to fit the following model
weightij = 0 + 1 weekij + ui + vj + ij
for i = 1, ..., 48 pigs and j = 1, ..., 9 weeks
Note that the week random effects vj are not nested within pigs, they are
assumed to be the same for each pig
R. Gutierrez (StataCorp)
24 / 30
Factor Notation
Sometimes random effects are crossed rather than nested
Example
Consider a dataset consisting of weight measurements on 48 pigs at each
of 9 weeks. We wish to fit the following model
weightij = 0 + 1 weekij + ui + vj + ij
for i = 1, ..., 48 pigs and j = 1, ..., 9 weeks
Note that the week random effects vj are not nested within pigs, they are
assumed to be the same for each pig
R. Gutierrez (StataCorp)
24 / 30
Factor Notation
Sometimes random effects are crossed rather than nested
Example
Consider a dataset consisting of weight measurements on 48 pigs at each
of 9 weeks. We wish to fit the following model
weightij = 0 + 1 weekij + ui + vj + ij
for i = 1, ..., 48 pigs and j = 1, ..., 9 weeks
Note that the week random effects vj are not nested within pigs, they are
assumed to be the same for each pig
R. Gutierrez (StataCorp)
24 / 30
u1
..
.
2
u48
u I48
0
u=
N(0, G); G =
0
v2 I9
v1
..
.
v9
25 / 30
u1
..
.
2
u48
u I48
0
u=
N(0, G); G =
0
v2 I9
v1
..
.
v9
25 / 30
Coef.
week
_cons
6.209896
19.35561
Std. Err.
.0578669
.6493996
Random-effects Parameters
z
107.31
29.81
=
=
=
=
=
432
1
432
432.0
432
=
=
11516.16
0.0000
P>|z|
0.000
0.000
6.096479
18.08281
6.323313
20.62841
Estimate
Std. Err.
sd(R.id)
3.892648
.4141707
3.15994
4.795252
sd(R.week)
.3337581
.1611824
.1295268
.8600111
sd(Residual)
2.072917
.0755915
1.929931
2.226496
_all: Identity
_all: Identity
R. Gutierrez (StataCorp)
26 / 30
Some notes
all tells xtmixed to treat the whole data as one big panel
R.varname is the random-effects analog of xi. It creates an
(overparameterized) set of indicator variables, but unlike xi, does this
behind the scenes
When you use R.varname, covariance structure reverts to Identity
There are alternate ways to fit this model with lower dimension
The trick is to realize that all effects are nested within the data as a
whole
R. Gutierrez (StataCorp)
27 / 30
Some notes
all tells xtmixed to treat the whole data as one big panel
R.varname is the random-effects analog of xi. It creates an
(overparameterized) set of indicator variables, but unlike xi, does this
behind the scenes
When you use R.varname, covariance structure reverts to Identity
There are alternate ways to fit this model with lower dimension
The trick is to realize that all effects are nested within the data as a
whole
R. Gutierrez (StataCorp)
27 / 30
Some notes
all tells xtmixed to treat the whole data as one big panel
R.varname is the random-effects analog of xi. It creates an
(overparameterized) set of indicator variables, but unlike xi, does this
behind the scenes
When you use R.varname, covariance structure reverts to Identity
There are alternate ways to fit this model with lower dimension
The trick is to realize that all effects are nested within the data as a
whole
R. Gutierrez (StataCorp)
27 / 30
Some notes
all tells xtmixed to treat the whole data as one big panel
R.varname is the random-effects analog of xi. It creates an
(overparameterized) set of indicator variables, but unlike xi, does this
behind the scenes
When you use R.varname, covariance structure reverts to Identity
There are alternate ways to fit this model with lower dimension
The trick is to realize that all effects are nested within the data as a
whole
R. Gutierrez (StataCorp)
27 / 30
Some notes
all tells xtmixed to treat the whole data as one big panel
R.varname is the random-effects analog of xi. It creates an
(overparameterized) set of indicator variables, but unlike xi, does this
behind the scenes
When you use R.varname, covariance structure reverts to Identity
There are alternate ways to fit this model with lower dimension
The trick is to realize that all effects are nested within the data as a
whole
R. Gutierrez (StataCorp)
27 / 30
Group Variable
No. of
Groups
_all
week
1
9
Coef.
week
_cons
6.209896
19.35561
432
=
=
11516.16
0.0000
432.0
48.0
Std. Err.
.0578669
.6493996
Random-effects Parameters
432
48
Wald chi2(1)
Prob > chi2
Number of obs
z
107.31
29.81
P>|z|
0.000
0.000
6.096479
18.08281
6.323313
20.62841
Estimate
Std. Err.
sd(R.id)
3.892648
.4141707
3.15994
4.795252
sd(_cons)
.3337581
.1611824
.1295268
.8600112
sd(Residual)
2.072917
.0755915
1.929931
2.226496
_all: Identity
week: Identity
28 / 30
Group Variable
No. of
Groups
_all
id
1
48
Coef.
week
_cons
6.209896
19.35561
432
=
=
11516.16
0.0000
432.0
9.0
Std. Err.
.0578669
.6493996
Random-effects Parameters
432
9
Wald chi2(1)
Prob > chi2
Number of obs
z
107.31
29.81
P>|z|
0.000
0.000
6.096479
18.08281
6.323313
20.62841
Estimate
Std. Err.
sd(R.week)
.3337581
.1611824
.1295268
.8600112
sd(_cons)
3.892648
.4141707
3.15994
4.795252
sd(Residual)
2.072917
.0755915
1.929931
2.226496
_all: Identity
id: Identity
29 / 30
30 / 30
30 / 30
30 / 30
30 / 30
30 / 30
30 / 30