clear
version 14.1
set more off
set rmsg on
cd "c:/users/bchavez2/desktop"
cap log close
log using "week3_lecture.txt", replace
use AApredation, clear
twoway (line NJfare period if destin=="MCI", sort lcolor(blue) lwidth(medthick)cmissing(n)) ///
(line AAfare period if destin=="MCI", sort lcolor(red) lwidth(medthick)) ///
(line DLfare period if destin=="MCI", sort lcolor(green) lwidth(medthick) lpattern(longdash)cmissing(n)), ///
ytitle(Fare price) xtitle(Time) title(DFW/Kansas City Market(MCI)) legend(size(small))
encode dest, gen(edest)
/*Fixed effects: Heterogeneity across entities */
bysort edest: egen aamean=mean(AAfare)
twoway scatter AAfare edest, msymbol(circle_hollow) || connected aamean edest, //
msymbol(diamond)
/*Fixed effects: Heterogeneity across time */
bysort period: egen aamean_p=mean(AAfare)
twoway scatter AAfare period, msymbol(circle_hollow) || connected aamean_p period, //
msymbol(diamond)
/*OLS regression */
reg AAfare dist
twoway scatter AAfare dist, mlabel(dest) || lfit AAfare dist, clstyle(p2)
/*OLS regression with destination dummies */
reg AAfare dist i.edest
predict yhat
separate AAfare, by(edest)
separate yhat, by(edest)
twoway connected yhat1-yhat88 dist || lfit AAfare dist, clwidth(thick) clcolor(black)
/*Saving estimation results */
regress AAfare dist
estimates store ols
reg AAfare dist i.edest
estimates store ols_dum
estimates table ols ols_dum, star stats(N) //making an estimation table to compare results
/*Declare panel dataset*/
xtset edest period
/*xt commands for graphs */
xtline AAfare
xtline AAfare, overlay
/*summarize using xt*/
xtsum
/*Using time periods and running joint test to see if fixed effects necessary*/
xtreg AAfare dist i.period
testparm i.period
//prob>F is <.05, so we reject the null that all coefficients are jointly equal to zero.
//Therefore time Fixed effects are needed.
/*Fixed effects regression*/
/* Fixed effects: controls for anything not time-varying (sex, race/ethnicity/
sometimes education) is treated as fixed within an individual.
These are captured through individual specific dummy variables.
An advantage of fixed effects is that we can use it to try to "soak up"
unobserved factors (omitted variables which maybe unmeasurable)*/
xtreg AAfare dist, fe //note same pvalue as the ols with dummies
est store fixed
//intercept interpretation is tricky: effect of distance over AAfare when distance
//changes across periods and between destinations by one unit.
areg AAfare dist, absorb(edest) //another way to estimate fixed effects.
est store areg
est table fixed ols_dum areg, star stats(N r2 r2_a) //same results
/*Create a variable for total flights flown per row entry*/
ds *flights*
egen tot_flights=rowtotal(`r(varlist)')
bysort year: egen year_tot_flights=total(tot_flights)
/*Random Effects Regresion*/
/* Random effects: rather than indiv specific dummy variable, we assume there is
a common factor for an indiv which has mean 0. These are captured in the
error term. Non-time varying factors (for indiv: sex/race/ethnicity) do not
drop out of the model -- these are captured through individual specific
dummy variables. The variation across entities is assumed to be random and
uncorrelated with the predictor or independent variables included in the
model. If you have reason to believe that differences across entities have
some influence on your dependent variable then you should use random effects.
Random effects assume that the entity’s error term is not correlated with the
predictors which allows for time-invariant variables to play a role as explanatory
variables
*/
xtreg AAfare distance, re
est store random
/*We can can test whether we should use random vs fixed effects empirically
(what test? Hausman) */
hausman fixed random, sigmamore
/*Hausman test: preferred is random versus fixed. The hypothesis is that the individual-
level effects are adequately modeled by a random-effects model.
It basically tests whether the unique errors (ui) are correlated with the
regressors, the null hypothesis is they are not.)
If P<.05, we reject the null and must use fixed effects, otherwise fail to reject
the null and random effects is the appropriate model specification*/
//In this example we fail to reject the null. Random Effects is appropriate for this model
/************************** Additional Tests ********************************/
/*Running a Breusch and Pagan Lagrangian mult test for random effects */
xtreg AAfare dist, re
xttest0
// Prob>Chi2 is <0.05. Reject the null. Should not use OLS.
/* Pasaran CD (cross-sectional dependence) test is used to test whether the residuals are
correlated across entities. Under the null, u(it) is assumed to be iid over periods and
across cross-sectional units. Under the alternative, u(it) may be correclated across cross-
sections.*/
ssc install xtcsd
ssc install xtscc
xtreg AAfare dist, fe
xtcsd , pesaran abs
//CD test strongly rejects the null hypothesis of no cross-sectional dependence.
//Therefore there is cross-sectional dependence here.
/*Heteroskedasticity test for Fixed effects is available: xttest3:
This is a modified Wald test for groupwise heteroskedasticity in fe model.
The null hypothesis is that there is constant variance (homoskedasticity).*/
ssc install xttest3
xtreg AAfare dist, fe
xttest3
/* Reject the null. There is presence of heteroskedasticity. So we need to use robust to obtain
heteroskedasticity robust std. errors: */
xtreg AAfare dist, fe robust
log close