See discussions, stats, and author profiles for this publication at: https://www.researchgate.
net/publication/342184332
The Stata command all commands concerning fixed and random effect
Method · June 2020
DOI: 10.13140/RG.2.2.27371.16162
CITATIONS READS
0 3,090
1 author:
Mohamed Nachid Boussiala
Algiers University
41 PUBLICATIONS 79 CITATIONS
SEE PROFILE
Some of the authors of this publication are also working on these related projects:
simulation View project
diagnostics for multiple regression View project
All content following this page was uploaded by Mohamed Nachid Boussiala on 15 June 2020.
The user has requested enhancement of the downloaded file.
Collected By Mohamed Nachid Boussiala
[email protected]
[email protected]
The Stata command to run fixed/random effecst is xtreg.
Before using xtreg you need to set Stata to handle panel data by using the command
xtset. type: xtset industry Time, yearly
In this case “industry” represents the entities or panels (i) and “Time” represents the time
variable (t).
/*we have a string variable “Industry” that Stata cannot identify; we have to generate a
corresponding numerical variable by typing*/
encode Industry, gen(ind)
egen ind = group(Industry)
label define ind 1 "GE" 2 "GM" 3 "US" 4 "WEST", replace
*We can see this new variable “ind” by typing
list Industry ind in 1/80, nolabel sepby(Industry)
*Next we have to declare the data set to be a panel data
xtset ind Time, yearly
encode industry, gen(ind)
Use ‘ind’ instead of ‘industry’ in the xtset command
xtset ind Time, yearly
Xtline dep.var ======== to get graph
xtline dep.var , overlay ======== to get one graph
bysort country: egen dep.var_mean=mean(dep.var)
twoway scatter dep.var country, msymbol(circle_hollow) || connected dep.var_mean country,
msymbol(diamond) || , xlabel(, labels)
bysort Time: egen dep.var_mean1=mean(dep.var)
twoway scatter dep.var Time, msymbol(circle_hollow) || connected dep.var _mean1 Time,
msymbol(diamond) || ,xlabel(, labels)
/*1. The Constant Coefficients (Pooled Regression) Model
I(it) = α + β1*F(it-1) + β2*K(it-1) + uit ; uit IIN(0, σ²); i = 1, 2, 3, 4; t = 1, 2, …, 20*/
Run this command
regress I F K
or this reg I F K
to test the significance of β1, β2 then run
testparm F K
/*2. The Fixed Effects Model
*2.1 The Fixed Effects (Least Squares Dummy Variable) Model:
I(it) = α + β1*F(it-1) + β2*K(it-1) + uit ////////
uit = µi + λt + vit , or, uit = µi + vit , or, uit = λt + vit
(i) Slope coefficients constant but intercept varies over companies.
Iit =µ1*D1 +µ2*D2 +µ3*D3 +µ4*D4 + β1*Fit-1 + β2*Kit-1 + vit */
tabulate ind, generate(D)
regress I F K D2 D3 D4 or
regress I F K D2-D4 or
reg I F K i.ind
/*Poolability Test (between Pooled Regression and FE Model)*/
testparm i.ind
*Random Coefficient models (Another Poolability test)
xtrc I F K
/*The test included with the random-coefficients model also indicates that the assumption of
parameter constancy is not valid for these data. if we cannot accepte H0*/
/*(ii) Slope coefficients constant but intercept varies over time.
Iit =λ1*d1 + λ2*d2 + …. + λ19*d19 + λ20*d20+ β1*Fit-1 + β2*Kit-1 + vit */
tabulate Time, generate(d) or
regress I F K d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14 d15 d16 d17 d18 d19 d20 or
regress I F K d2-d20 or
reg I F K i.Time
Now Stata gives the poolability test result after the regression with the factor variable i.Time:
testparm i.Time
/*(iii) Slope coefficients constant but intercept varies over companies and time.
Iit = α +µ2*D2 +µ3*D3 +µ4*D4 + λ2*d2 +… + λ20*d20 + β1*Fit-1 + β2*Kit-1 + vit */
regress I F K D2 D3 D4 d2 d3 d4 d5 d6 d7 d8 d9 d10 d11 d12 d13 d14 d15 d16 d17 d18 d19 d20 or
regress I F K D2-D4 d2-d20 or
reg I F K i.ind i.Time
In this case in Stata, we can do the poolability test in three ways. First we test the null of zero
cross section and temporal effects:
testparm i.ind i.Time
testparm i.ind
testparm i.Time
/*(iv) All coefficients (intercept and slope) vary over companies.
Iit = µ + µ2*D2 + µ3*D3 + µ4*D4 + β1*Fit-1 + β2*Kit-1 + γ1*(D2*Fit-1)+ γ2*(D2*Kit-1)
+ γ3*(D3*Fit-1)+ γ4*(D3*Kit-1) + γ5*(D4*Fit-1)+ γ6*(D4*Kit-1)+ vit */,
reg I F K i.ind i.ind#c.F i.ind#c.K
To get the results of F-tests:
testparm i.ind
testparm i.ind#c.F
testparm i.ind#c.K
testparm c.F c.K
/*(v) All coefficients (intercept and slope) vary over time.
* Iit = λ + λ2*d2 + λ3*d3 + … + λ20*d20 + β1*Fit-1 + β2*Kit-1 + γ1*(d2*F it-1)+ γ2*(d2*Kit-
1) + γ3*(d3*Fit-1)+ γ4*(d3*Kit-1) + … + γ37*(d*20*Fit-1 )+ γ38*(d20*K it-1)+ vit */ ,
reg I F K i.Time i.Time#c.F i.Time#c.K
/*(vi) All coefficients (intercept and slope) vary over companies and time.
Iit = α +µ2*D2+µ3*D3+µ4*D4 +β1*Fit-1+β2*Kit-1+γ1*(D2*Fit-1)+γ2*(D2*Kit-1) +
γ3*(D3*Fit-1)+γ4*(D3*Kit-1)+γ5*(D4*Fit-1)+γ6*(D4*Kit-1)+λ2*d2+λ3*d3+ … +λ20*d20+β1*Fit-
1+β2*Kit-1+δ1*(d2*Fit-1) +δ2*(d2*Kit-1)+δ3*(d3*Fit-1)+δ4*(d3*Kit-1)+ … +δ37*(d20*Fit-
1)+δ38*(d20*Kit-1 )+v it */
reg I F K i.ind i.Time i.ind#c.F i.ind#c.K i.Time#c.F i.Time#c.K
**2.2 The Fixed Effects (Within-groups Regression) Model
xtreg I F K, fe
*We can have the estimates of the individual (cross section or panel) effects
predict IE, u
This will generate the individual effects (IE) that can be viewed in the Data Editor (Edit). Note
that we can give any name , instead of IE, for example, the command
predict pe, u
will give the same series with name pe (panel effects).
**2.2 The Random Effects Model
xtreg I F K, re
/* ρ = σ²µ/(σ²µ+σ²v) the interpretation of rho : X% of the variance is due to differences across
panels.
θ = 1 − sqrt((σ²v /(σ²v+T*σ²µ))*/
xtreg I F K, re theta
/* Estimate the FE model by typing */
xtreg I F K, fe
/* And store the result as fe by typing: statistics > postestimation >
manage estimaion > store current estimation > name FE */
estimates store fe
/*Then estimate the RE model by typing*/
xtreg I F K, re
/*And do the Hausman test by typing ,,: statistics > postestimation >
manage estimaion > specification ,diagnostic and goodness of fit > hausman test*/
hausman fe
/*The test fails to reject the null, as the p-value (Prob>chi2) is greater than 5%. Note that the
Hausman test is a test of
H 0 : random effects would be consistent and efficient, versus
H 1 : random effects would be inconsistent.*/
===========================================================
/*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.*/
regress I F K
estimates store ols
xi: regress I F K i.ind
estimates store ols_dum
estimates table ols ols_dum, star stats(N)
/*Another way to estimate fixed effects: n entity-specific intercepts
(using areg)
“Although its output is less informative than regression with explicit dummy variables,
areg does have two advantages. It speeds up exploratory work, providing quick feedback about
whether a dummy variable approach is worthwhile.
Secondly, when the variable of interest has many values, creating dummies for each of
them could lead to too many variables or too large a model ….” (Hamilton, 2006, p.180)*/
areg I F K , absorb(ind)
areg I F K , absorb(Time)
/*Another way to estimate fixed effects: common intercept
and n-1 binary regressors (using dummies and regress)*/
xi: regress I F K i.ind
/*To compare the previous methods type “estimates store [name]” after running each regression, at
the end use the command “estimates table…”:*/
xtreg I F K, fe
estimates store fixed
xi: regress I F K i.ind
estimates store ols
areg I F K, absorb(ind)
estimates store areg
estimates table fixed ols areg, star stats(N r2 r2_a)
OTHER TESTS/ DIAGNOSTICS
/*Testing for time-fixed effects
To see if time fixed effects are needed when running a FE model use the
command testparm. It is a joint test to see if the dummies for all years are equal
to 0, if they are then no time fixed effects are needed (type help testparm for
more details)*/
xtreg I F K i.Time, fe
/*After running the fixed effect model, type:*/
testparm i.Time
/*Testing for random effects: Breusch-Pagan Lagrange multiplier (LM)
The LM test helps you decide between a random effects regression and a simple
OLS regression.
The null hypothesis in the LM test is that variances across entities is zero. This is,
no significant difference across units (i.e. no panel effect).*/
xtreg I F K , re
/*The command in Stata is*/
xttset0
/*Breusch and Pagan Lagrangian multiplier test for random effects
y[country,t] = Xb + u[country] + e[country,t]
Test: Var(u) = 0
Testing for cross-sectional dependence/contemporaneous correlation: using Breusch-Pagan LM
test of independence
According to Baltagi, cross-sectional dependence is a problem in macro panels with long time
series (over 20-30 years). This is not much of a problem in micro panels (few years and large
number of cases).
The null hypothesis in the B-P/LM test of independence is that residuals across entities are not
correlated. The command to run this test is xttest2 (Type xttest2 for more info. If not available try
installing it by typing ssc install xttest2):run it after xtreg I F K , fe */
xttest2
/* if P-value > 0.05 => No cross-sectional dependence*/
/*Testing for cross-sectional dependence/contemporaneous correlation:Using Pasaran CD test
As mentioned in the previous slide, cross-sectional dependence is more of an issue in macro
panels with long time series (over 20-30 years) than in micro panels.
Pasaran CD (cross-sectional dependence) test is used to test whether the residuals are
correlated across entities. Cross-sectional dependence can lead to bias in tests results (also
called contemporaneous correlation). The null hypothesis is that residuals are not correlated.
The command for the test is xtcsd, you have to install it typing ssc install xtcsd
we run it after xtreg I F K , fe */
xtcsd, pesaran abs
/* if P-value > 0.05 => No cross-sectional dependence
Had cross-sectional dependence be present Hoechle suggests to use Driscoll and Kraay standard errors
using the command xtscc (install it by typing ssc install xtscc). */
Xtscc I F K ( the same results as reg I F K )
/* Testing for heteroskedasticity
A test for heteroskedasticiy is available for the fixed- effects model using the command
xttest3. This is a user-written program, to install it type: ssc install xttest3*/
xttest3 ( after run xtreg I F K , fe)
/*The null is homoskedasticity (or constant variance). If p-value< 0.05 we cannot accept the null and
conclude heteroskedasticity. Type help xttest3 for more details.
NOTE: Use the option ‘robust’ to obtain heteroskedasticity-robust standard errors (also known as
Huber/White or sandwich estimators).*/
/* Testing for serial correlation
Serial correlation tests apply to macro panels with long time series (over 20-30 years). Not a problem
in micro panels (with very few years). Serial correlation causes the standard errors of the coefficients
to be smaller than they actually are and higher R-squared .
A Lagram-Multiplier test for serial correlation is available using the command xtserial.
This is a user-written program, to install it type ssc install xtserial*/
xtserial I F K
/* The null is no serial correlation. Above we cannot accept the null and conclude the
data have first-order autocorrelation. Type help xtserial for more details.*/
/*Testing for unit roots/stationarity
/* “ xtunitroot performs a variety of tests for unit roots (or stationarity) in panel datasets. The Levin-
Lin-Chu (2002), Harris-Tzavalis (1999), Breitung (2000; Breitung and Das 2005), Im-Pesaran-Shin
(2003), and Fisher-type (Choi 2001) tests have as the null hypothesis that all the panels contain a unit
root. The Hadri (2000) Lagrange multiplier (LM) test has as the null hypothesis that all the panels are
(trend) stationary. The top of the output for each test makes explicit the null and alternative
hypotheses. Options allow you to include panel-specific means (fixed effects) and time trends in the
model of the data-generating process” [Source: http://www.stata.com/help.cgi?xtunitroot or type help
xtunitroot]
Syntax
Levin-Lin-Chu test
xtunitroot llc varname [if] [in] [, LLC_options]
Harris-Tzavalis test
xtunitroot ht varname [if] [in] [, HT_options]
Breitung test
xtunitroot breitung varname [if] [in] [, Breitung_options]
Im-Pesaran-Shin test
xtunitroot ips varname [if] [in] [, IPS_options]
Fisher-type tests (combining p-values)
xtunitroot fisher varname [if] [in], {dfuller | pperron} lags(#) [Fisher_options]
Hadri Lagrange multiplier stationarity test
xtunitroot hadri varname [if] [in] [, Hadri_options]
=======================================================
Table 1: Selection of Stata commands and options that produce robust standard error estimates for
linear panel models.
SE estimates are
Command Option robust to dis Notes
turbances being
reg, xtreg robust heteroscedastic
heteroscedastic and
reg, xtreg cluster()
autocorrelated
autocorrelated with
xtregar
AR(1)1
heteroscedastic and
newey autocorrelated of type
MA(q)2
heteroscedastic,
contemporaneously N < T required for
cross-sectionally feasibility; tends to
xtgls panels(),corr ()
correlated, and produce optimistic SE
autocorrelated of type estimates
AR(1)
heteroscedastic,
contemporaneously
large-scale panel
cross-sectionally
xtpcse Correlation () regressions with xtpcse
correlated, and
take a lot of time
autocorrelated of type
AR(1)
heteroscedastic,
autocorrelated
xtscc
with MA(q), and cross-
sectionally dependent
1 AR(1) refers to first-order autoregression
2 MA(q) denotes autocorrelation of the moving average type with lag length q.
View publication stats