Professional Documents
Culture Documents
using Stata
Source: http://www.princeton.edu/~otorres/Stata/
Intro
Panel data models uses repeated measures of entities over time.
Panel data looks like this
country year Y X1 X2 X3 1 2000 6.0 7.8 5.8 1.3
1 2001 4.6 0.6 7.9 7.8
1 2002 9.4 2.1 5.4 1.1
2 2000 9.1 1.3 6.7 4.1
2 2001 8.3 0.9 6.6 5.0
2 2002 0.6 9.8 0.4 7.2
3 2000 9.1 0.2 2.6 6.4
3 2001 4.8 5.9 3.2 6.4
3 2002 9.1 5.2 6.
Causation: Since repeated measures are used, we can estimate causal relationship
rather than mere correlation.
Panel data allows you to control for
factors that are time invariant.
Inference: correct standard errors.
This is because each additional
observation is not independent of
previous observation of the same
entity.
Short Panel: data on many individuals
and few time periods.
PU/DSS/OTR
Setting panel data: xt set
The Stata command to run fixed/random effecst is xt r eg.
Before using xt r eg you need to set Stata to handle panel data by using the command
xt set . type:
xt set country year
del t a: 1 uni t
t i me var i abl e: year , 1990 t o 1999
panel var i abl e: count r y ( st r ongl y bal anced)
. xt set count r y year
In this case count r y represents the entities or panels (i) and year represents the time
variable (t).
The note ( st r ongl y bal anced) refers to the fact that all countries have data for all
years. If, for example, one country does not have data for one year then the data is
unbalanced. Ideally you would want to have a balanced dataset but this is not always the
case, however you can still run the model.
NOTE: If you get the following error after using xt set :
You need to convert count r y to numeric, type:
encode count r y, gen( count r y1)
Use count r y1 instead of count r y in the xt set command
5
var l i st : count r y: st r i ng var i abl e not al l owed
2.4 Stata linear panel commands
Panel summary xtset; xtdescribe; xtsum; xtdata;
xtline; xttab; xttran
Pooled OLS regress
Feasible GLS xtgee, family(gaussian)
xtgls; xtpcse
Random eects xtreg, re; xtregar, re
Fixed eects xtreg, fe; xtregar, fe
Random slopes xtmixed; quadchk; xtrc
First dierences regress (with dierenced data)
Static IV xtivreg; xthtaylor
Dynamic IV xtabond; xtdpdsys; xtdpd
PU/DSS/OTR
Exploring panel data
xt l i ne y
6
-
1
.
0
0
0
e
+
1
0
-
5
.
0
0
0
e
+
0
9
0
5
.
0
0
0
e
+
0
9
1
.
0
0
0
e
+
1
0
-
1
.
0
0
0
e
+
1
0
-
5
.
0
0
0
e
+
0
9
0
5
.
0
0
0
e
+
0
9
1
.
0
0
0
e
+
1
0
-
1
.
0
0
0
e
+
1
0
-
5
.
0
0
0
e
+
0
9
0
5
.
0
0
0
e
+
0
9
1
.
0
0
0
e
+
1
0
1990 1995 20001990 1995 2000
1990 1995 2000
A B C
D E F
G
y
year
Graphs by country
PU/DSS/OTR
Exploring panel data
xt l i ne y, over l ay
7
-
1
.
0
0
0
e
+
1
0
-
5
.
0
0
0
e
+
0
9
0
5
.
0
0
0
e
+
0
9
1
.
0
0
0
e
+
1
y
1990 1992 1994 1996 1998 2000
year
A B
C D
E F
G
Pooled model (or population-averaged)
y
it
= +x
0
it
+u
it
. (1)
Two-way eects model allows intercept to vary over i and t
y
it
=
i
+
t
+x
0
it
+
it
. (2)
Individual-specic eects model
y
it
=
i
+x
0
it
+
it
, (3)
for short panels where time-eects are included as dummies in x
it
.
Between estimator: OLS of y
i
on x
i
.
Random eects estimator: FGLS in RE model.
Equals OLS of (y
it
i
y
i
) on (x
it
i
x
i
);
i
= 1
p
/(T
i
+
2
).
Within estimator or FE estimator: OLS of (y
it
y
i
) on (x
it
x
i
).
First dierence estimator: OLS of (y
it
y
i ,t1
) on (x
it
x
i ,t1
).
PU/DSS/OTR
Fixed effects: Heterogeneity across countries (or entities)
bysor t count r y: egen y_mean=mean( y)
t woway scat t er y count r y, msymbol ( ci r cl e_hol l ow) | | connect ed y_mean count r y,
msymbol ( di amond) | | , xl abel ( 1 "A" 2 "B" 3 "C" 4 "D" 5 "E" 6 "F" 7 "G")
13
-
1
.
0
0
0
e
+
1
0
-
5
.
0
0
0
e
+
0
9
0
5
.
0
0
0
e
+
0
9
1
.
0
0
0
e
+
1
A B C D E F G
country
y y_mean
Heterogeneity: unobserved variables that do not change over time
PU/DSS/OTR
Fixed effects: Heterogeneity across years
bysor t year : egen y_mean1=mean( y)
t woway scat t er y year , msymbol ( ci r cl e_hol l ow) | | connect ed y_mean1 year ,
msymbol ( di amond) | | , xl abel ( 1990( 1) 1999)
14
-
1
.
0
0
0
e
+
1
0
-
5
.
0
0
0
e
+
0
9
0
5
.
0
0
0
e
+
0
9
1
.
0
0
0
e
+
1
1990 1991 1992 1993 1994 1995 1996 1997 1998 1999
year
y y_mean1
Heterogeneity: unobserved variables that do not change over time
3RROHGOLS
15
_cons 1. 52e+09 6. 21e+08 2. 45 0. 017 2. 85e+08 2. 76e+09
x1 4. 95e+08 7. 79e+08 0. 64 0. 527 - 1. 06e+09 2. 05e+09
y Coef . St d. Er r . t P>| t | [ 95%Conf . I nt er val ]
Tot al 6. 2729e+20 69 9. 0912e+18 Root MSE = 3. 0e+09
Adj R- squar ed = - 0. 0087
Resi dual 6. 2359e+20 68 9. 1705e+18 R- squar ed = 0. 0059
Model 3. 7039e+18 1 3. 7039e+18 Pr ob > F = 0. 5272
F( 1, 68) = 0. 40
Sour ce SS df MS Number of obs = 70
. r egr ess y x1
A
A
A
A
A
A
A A
A
A
B
B
B
B
B
B
B
B
B
B
C
C
C
C
C
C
C
C C
C
D
D
D
D
D
D
D
D
D
D
E
E
E
E
E
E
E
E
E
E
F
F
F
F
F
F
F
F
F
F
G
G
G
G
G
G
G
G
G
G
-
1
.
0
0
0
e
+
1
0
-
5
.
0
0
0
e
+
0
9
0
5
.
0
0
0
e
+
0
9
1
.
0
0
0
e
+
1
-.5 0 .5 1 1.5
x1
y Fitted values
t woway scat t er y x1,
ml abel ( count r y) | | l f i t y x1,
cl st yl e( p2)
Fixed Effects using least
squares dummy variable
model (LSDV)
.
16
_cons 8. 81e+08 9. 62e+08 0. 92 0. 363 - 1. 04e+09 2. 80e+09
_I count r y_7 - 1. 87e+09 1. 50e+09 - 1. 25 0. 218 - 4. 86e+09 1. 13e+09
_I count r y_6 1. 13e+09 1. 29e+09 0. 88 0. 384 - 1. 45e+09 3. 71e+09
_I count r y_5 - 1. 48e+09 1. 27e+09 - 1. 17 0. 247 - 4. 02e+09 1. 05e+09
_I count r y_4 2. 28e+09 1. 26e+09 1. 81 0. 075 - 2. 39e+08 4. 80e+09
_I count r y_3 - 2. 60e+09 1. 60e+09 - 1. 63 0. 108 - 5. 79e+09 5. 87e+08
_I count r y_2 - 1. 94e+09 1. 26e+09 - 1. 53 0. 130 - 4. 47e+09 5. 89e+08
x1 2. 48e+09 1. 11e+09 2. 24 0. 029 2. 63e+08 4. 69e+09
y Coef . St d. Er r . t P>| t | [ 95%Conf . I nt er val ]
Tot al 6. 2729e+20 69 9. 0912e+18 Root MSE = 2. 8e+09
Adj R- squar ed = 0. 1404
Resi dual 4. 8454e+20 62 7. 8151e+18 R- squar ed = 0. 2276
Model 1. 4276e+20 7 2. 0394e+19 Pr ob > F = 0. 0199
F( 7, 62) = 2. 61
Sour ce SS df MS Number of obs = 70
i . count r y _I count r y_1- 7 ( nat ur al l y coded; _I count r y_1 omi t t ed)
. xi : r egr ess y x1 i . count r y
-
2
.
0
0
e
+
0
9
0
2
.
0
0
e
+
0
9
4
.
0
0
e
+
0
9
6
.
0
0
e
+
0
9
-.5 0 .5 1 1.5
x1
yhat, country == A yhat, country == B
yhat, country == C yhat, country == D
yhat, country == E yhat, country == F
yhat, country == G Fitted values
xi : r egr ess y x1 i . count r y
pr edi ct yhat
separ at e y, by( count r y)
separ at e yhat , by( count r y)
t woway connect ed yhat 1- yhat 7
x1, msymbol ( none
di amond_hol l ow t r i angl e_hol l ow
squar e_hol l ow + ci r cl e_hol l ow
x) msi ze( medi um) mcol or ( bl ack
bl ack bl ack bl ack bl ack bl ack
bl ack) | | l f i t y x1,
cl wi dt h( t hi ck) cl col or ( bl ack)
OLS regression
PU/DSS/OTR
Fixed effects
The least square dummy variable model (LSDV) provides a good way to understand fixed
effects.
The effect of x1 is mediated by the differences across countries.
By adding the dummy for each country we are estimating the pure effect of x1 (by
controlling for the unobserved heterogeneity).
Each dummy is absorbing the effects particular to each country.
17
r egr ess y x1
est i mat es st or e ol s
xi : r egr ess y x1 i . count r y
est i mat es st or e ol s_dum
est i mat es t abl e ol s ol s_dum, st ar st at s( N)
l egend: * p<0. 05; ** p<0. 01; *** p<0. 001
N 70 70
_cons 1. 524e+09* 8. 805e+08
_I count r y_7 - 1. 865e+09
_I count r y_6 1. 130e+09
_I count r y_5 - 1. 483e+09
_I count r y_4 2. 282e+09
_I count r y_3 - 2. 603e+09
_I count r y_2 - 1. 938e+09
x1 4. 950e+08 2. 476e+09*
Var i abl e ol s ol s_dum
. est i mat es t abl e ol s ol s_dum, st ar st at s( N)
PU/DSS/OTR
Fixed effects: n entity-specific intercepts using xtreg
Comparing the fixed effects using dummies with xt r eg we get the same results.
18
r ho . 29726926 ( f r act i on of var i ance due t o u_i )
si gma_e 2. 796e+09
si gma_u 1. 818e+09
_cons 2. 41e+08 7. 91e+08 0. 30 0. 762 - 1. 34e+09 1. 82e+09
x1 2. 48e+09 1. 11e+09 2. 24 0. 029 2. 63e+08 4. 69e+09
y Coef . St d. Er r . t P>| t | [ 95%Conf . I nt er val ]
cor r ( u_i , Xb) = - 0. 5468 Pr ob > F = 0. 0289
F( 1, 62) = 5. 00
over al l = 0. 0059 max = 10
bet ween = 0. 0763 avg = 10. 0
R- sq: wi t hi n = 0. 0747 Obs per gr oup: mi n = 10
Gr oup var i abl e: count r y Number of gr oups = 7
Fi xed- ef f ect s ( wi t hi n) r egr essi on Number of obs = 70
. xt r eg y x1, f e
_cons 8. 81e+08 9. 62e+08 0. 92 0. 363 - 1. 04e+09 2. 80e+09
_I count r y_7 - 1. 87e+09 1. 50e+09 - 1. 25 0. 218 - 4. 86e+09 1. 13e+09
_I count r y_6 1. 13e+09 1. 29e+09 0. 88 0. 384 - 1. 45e+09 3. 71e+09
_I count r y_5 - 1. 48e+09 1. 27e+09 - 1. 17 0. 247 - 4. 02e+09 1. 05e+09
_I count r y_4 2. 28e+09 1. 26e+09 1. 81 0. 075 - 2. 39e+08 4. 80e+09
_I count r y_3 - 2. 60e+09 1. 60e+09 - 1. 63 0. 108 - 5. 79e+09 5. 87e+08
_I count r y_2 - 1. 94e+09 1. 26e+09 - 1. 53 0. 130 - 4. 47e+09 5. 89e+08
x1 2. 48e+09 1. 11e+09 2. 24 0. 029 2. 63e+08 4. 69e+09
y Coef . St d. Er r . t P>| t | [ 95%Conf . I nt er val ]
Tot al 6. 2729e+20 69 9. 0912e+18 Root MSE = 2. 8e+09
Adj R- squar ed = 0. 1404
Resi dual 4. 8454e+20 62 7. 8151e+18 R- squar ed = 0. 2276
Model 1. 4276e+20 7 2. 0394e+19 Pr ob > F = 0. 0199
F( 7, 62) = 2. 61
Sour ce SS df MS Number of obs = 70
i . count r y _I count r y_1- 7 ( nat ur al l y coded; _I count r y_1 omi t t ed)
. xi : r egr ess y x1 i . count r y
OLS regression
Using xt r eg
PU/DSS/OTR
Fixed effects option
r ho . 29726926 ( f r act i on of var i ance due t o u_i )
si gma_e 2. 796e+09
si gma_u 1. 818e+09
_cons 2. 41e+08 7. 91e+08 0. 30 0. 762 - 1. 34e+09 1. 82e+09
x1 2. 48e+09 1. 11e+09 2. 24 0. 029 2. 63e+08 4. 69e+09
y Coef . St d. Er r . t P>| t | [ 95%Conf . I nt er val ]
cor r ( u_i , Xb) = - 0. 5468 Pr ob > F = 0. 0289
F( 1, 62) = 5. 00
over al l = 0. 0059 max = 10
bet ween = 0. 0763 avg = 10. 0
R- sq: wi t hi n = 0. 0747 Obs per gr oup: mi n = 10
Gr oup var i abl e: count r y Number of gr oups = 7
Fi xed- ef f ect s ( wi t hi n) r egr essi on Number of obs = 70
. xt r eg y x1, f e
Fixed effects: n entity-specific intercepts (using xt r eg)
Outcome
variable
Predictor
variable(s)
Y
it
=
1
X
it
++
k
X
kt
+
i
+ e
it
[see eq.1]
Total number of cases (rows)
Total number of groups
(entities)
If this number is < 0.05 then
your model is ok. This is a
test (F) to see whether all the
coefficients in the model are
different than zero.
Two-tail p-values test the
hypothesis that each
coefficient is different from 0.
To reject this, the p-value has
to be lower than 0.05 (95%,
you could choose also an
alpha of 0.10), if this is the
case then you can say that the
variable has a significant
influence on your dependent
variable (y)
t-values test the hypothesis that each coefficient is
different from 0. To reject this, the t-value has to
be higher than 1.96 (for a 95% confidence). If this
is the case then you can say that the variable has
a significant influence on your dependent variable
(y). The higher the t-value the higher the
relevance of the variable.
Coefficients of the
regressors. Indicate how
much Y changes when X
increases by one unit.
29.7% of the variance is
due to differences
across panels.
rho is known as the
intraclass correlation
The errors u
i
are correlated
with the
regressors in
the fixed effects
model
2 2
2
) _ ( ) _ (
) _ (
e sigma u sigma
u sigma
rho