* ********************************************** * Summary Note by Jing Li and Junsoo Lee * panel_1.do * * Commands: xt, xtdata, xtdes, xtsum, xttab, * xtgls, xtreg, stegar, xtivreg, xtabond * * September 2003 * *********************************************** clear cd "c:\upcd1\work\stata" log using panel_1.log, replace set mem 200m set more off set matsize 800 ***************************************** * xt * ***************************************** use abdata.dta, clear * use http://www.stata-press.com/data/r8/abdata, clear * use http://www.stata-press.com/data/r8/nlswork, clear * use http://www.stata-press.com/data/r8/union, clear * tsset id year ** Some commands such as "xtabond" require tsset. * iis id, clear * tis year, clear ** iis and tis are alternatives to i() and t() option. ** These override previous setting specified by iis or tis. ** describe pattern of the panel-data list in 1/6, separator(0) divider xtdes, patterns(15) i(id) t(year) ***************************************** * xtdata * ***************************************** use nlswork.dta, clear * use http://www.stata-press.com/data/r8/nlswork, clear generate age2 = age^2 generate ttl_exp2 = ttl_exp^2 generate byte black = race==2 xtdata ln_w grade age* ttl_exp* tenure* black not_smsa south, be clear i(id) ** xtdata converts the data into a form suitable for between estimation. regress ln_w grade age* ttl_exp* tenure* black not_smsa south ** Thus, this gives the be estimator. * xtdata ln_w grade age* ttl_exp* tenure* black not_smsa south, fe clear i(id) * regress ln_w grade age* ttl_exp* tenure* black not_smsa south ***************************************** * xtdes * ***************************************** use nlswork.dta, clear * use http://www.stata-press.com/data/r8/nlswork, clear xtdes, patterns(15) i(id) t(year) ***************************************** * xtsum * ***************************************** xtsum wks_work xtsum birth_yr ** As this is time invariant, its within std dev is zero. ***************************************** * xttab * ***************************************** xttab wks_work xttab birth_yr ** As this is time invariant, its within percentage is 100. ***************************************** * xtgls * ***************************************** ** xtgls fits "Cross-sectional time series" linear models using feasible GLS (not panel estimation). use abdata.dta, clear * use http://www.stata-press.com/data/r8/abdata, clear ** estimate the model using GLS * Dep var = n (log of employment in firm i and time t) * Regressors = w (log of wage) k (log of capital stock) ys (log of industry output) xtgls n w k ys, i(id) t(year) nmk ** Estimating the model using default options (homosekdasticity, no autocorrelation) ** xtgls n w k ys, i(id) t(year) igls panels(correlated) ** MLE estimation of by specifying the igls option, which iterates the GLS estimates. ** The above does not work, since the panel should be balanced. ** We now use a different data set, which is a balanced panel. use invest2.dta, clear * use http://www.stata-press.com/data/r8/invest2, clear xtgls invest market stock, i(company) panels(iid) corr(independent) nmk ** same as regress (iid, homoskedasticity, no autocorrelation) ** nmk specifies std error to be normalized by n-k. xtgls invest market stock, i(company) panels(hetero) ** iid, heteroskedasticity, no autocorrelation xtgls invest market stock, i(company) t(time) panels(correlated) ** correlated, heteroskedasticity, no autocorrelation xtgls invest market stock, i(company) t(time) panels(correlated) igls nolog ** correlated, heteroskedasticity, no autocorrelation ** MLE estimation by iterative GLS (1046 iterations for this case.) xtgls invest market stock, i(company) panels(hetero) corr(ar1) ** iid, heteroskedasticity, common ar1 autocorrelation xtgls invest market stock, i(company) panels(hetero) corr(psar1) ** iid, heteroskedasticity, hetero ar1 autocorrelation xtgls invest market stock, i(company) t(time) panels(correlated) corr(psar1) ** correlated, heteroskedasticity, hetero ar1 autocorrelation matrix list e(Sigma) ** Estimated cross-sectional covariances predict new_inv1, xb list new_inv1 ***************************************** * xtreg * ***************************************** use abdata.dta, clear * use http://www.stata-press.com/data/r8/abdata, clear ** estimate GLS random-effects model xtreg n w k ys, re i(id) theta xttest0 ** Breusch and Pagan LM test for random effects, modified by Baltagi and Li (1990; see manual, p. 210) xthausman ** Performs the Hausman specification test for RE versus FE. xtreg n w k ys, re i(id) ** RE GLS xtreg n w k ys, mle i(id) nolog ** estimate ML RE model (supressing iterations with nolog) xtreg n w k ys, re i(id) sa ** RE: using the small-sample Swamy-Arora estimator by Baltagi and Chang (1994; see manual, p. 209) xtreg n w k ys, pa i(id) nolog ** GEE population-averaged model; equivalent to the RE ** also equivalent to the following xtgee xtgee n w k ys, family(gaussian) link(id) corr(exchangeable) xtreg n w k ys, re i(id) ** Between estimator xtreg n w k ys, be i(id) wls ** Between estimator ** (wls is used for unbalanced panel, and a stabilized variance is used.) xtreg n w k ys, fe i(id) ** Estimating the Fixed-effects model ***************************************** * xtregar * ***************************************** ** FE and RE with AR(1) error use grunfeld.dta, clear * use http://www.stata-press.com/data/r8/grunfeld, clear tsset * tsset company year xtregar invest mvalue kstock, fe ** Estimating the Fixed-effects model with ar(1) error xtregar invest mvalue kstock, re ** Estimating the Fixed-effects model with ar(1) error ***************************************** * xtivreg * ***************************************** ** Estimating instrumental variable panel data models use abdata.dta, clear * use http://www.stata-press.com/data/r8/abdata, clear tsset id year xtivreg n l2.n l(0/1).w l(0/2).(k ys) yr1977-yr1984 (l.n = l3.n), i(id) fd ** FD model ** dep = n ** ind = ** l.n = n(t-1) ... endogenous and instrumented ** l2.n = n(t-2) .. L2D ** l(0/1).w = w(t), w(t-1) .. D1 (level), LD (lagged) ** l(0.2).(k ys) = k(t), k(t-1), k(t-2); ys(t), ys(t-1), ys(t-2) .. D1, LD, L2D ** iv = l3.n = n(t-3) & all other exogenous variables xtivreg n l2.n l(0/1).w l(0/2).(k ys) yr1977-yr1984 (l.n = l3.n), i(id) fd first small xtivreg n w yr1977-yr1984 (k = ys), fe i(id) ** Fixed-effects model xtivreg n w yr1977-yr1984 (k = ys), fe i(id) first ** Fixed-effects model, reporting the first stage result. xtivreg n w (k = ys), be i(id) first ** Between-effects model xtivreg n w (k = ys), re nosa i(id) first theta ** GLS Random-effects model xtivreg n w (k = ys), re ec2sls i(id) first theta ** EC2SLS Random-effects model ***************************************** * xtabond * ***************************************** ** Arellano-Bond estimator use abdata.dta, clear * use http://www.stata-press.com/data/r8/abdata, clear xtabond n l(0/1).w l(0/2).(k ys) yr1977-yr1984, lag(2) ** One step estimator ** Sargan's test of over-identification restriction test >> p-value < 0.001. ** Sargan's test assumes homoskedasticity. xtabond n l(0/1).w l(0/2).(k ys) yr1977-yr1984, lag(1) robust ** Still, one step estimator but reporting robust std error. ** The absence of AR(1) error is rejected but no AR(2) error is not rejected. ** The AR(1) error does not mean the one-step estimator is inconsistent. ** But, if the null of no AR(2) error is not rejected, the one step estimator is inconsistent, which is not the case here. xtabond n l(0/1).w l(0/2).(k ys) yr1977-yr1984, lag(2) small ** request t-stat and F-stat be reported instead of Z-stat and chi-square stat. xtabond n l(0/1).w l(0/2).(k ys) yr1977-yr1984, lag(2) twostep ** The std errors of the two-step estimator tend to be biased in small samples. ** Thus, the one-step estimator is recommended for inference, and the Sargan test from the two step estimator is used for model specification. xtabond n l(0/1).w l(0/2).(k ys) yr1977-yr1984, lag(2) twostep pre(w, lag(1,.)) pre(k, lag(2,.)) ** predetermined regressors xtabond n l(0/1).w l(0/2).(k ys) yr1977-yr1984, lag(2) twostep pre(w, lag(1,.) endog) pre(k, lag(2,.) endog) ** predetermined plus contemporaneously correlated with error ***************************************** * More examples by Jing * ***************************************** **** Note: try xt series of commands on "invest2.dta" use invest2.dta,clear * use http://www.stata-press.com/data/r8/invest2, clear iis company tis time ** describe pattern of the panel-data xtdes, patterns(20) ** estimate the model using GLS * Dep variable = invest * Regressors = market stock xtgls invest market stock, nmk panels(iid) corr(independent) xtgls invest market stock, panels(hetero) xtgls invest market stock, panels(correlated) corr(ar1) xtgls invest market stock, panels(correlated) corr(psar1) xtgls invest market stock, igls gen lninvest = log(invest) /*try GLS with the log-level data*/ xtgls lninvest market stock **** Note: try xt series of commands on "nlswork.dta" use nlswork.dta,clear * use http://www.stata-press.com/data/r8/nlswork, clear iis idcode tis year ** describe the patterns of the data xtdes, patterns(30) ** estimate the model using 'xtreg' * Dep variable = ln_wage * Regressors = grade race age ttl_exp tenure not_smsa south * And the square terms of age ttl_exp tenure are also included gen age2 = age^2 gen ttl_exp2 = ttl_exp^2 gen tenure2 = tenure^2 * between-effects model xtreg ln_wage grade race age age2 ttl_exp ttl_exp2 tenure tenure2 not_smsa south, be wls * fixed-effects model xtreg ln_wage grade race age age2 ttl_exp ttl_exp2 tenure tenure2 not_smsa south, fe * GLS Random-effects model xtreg ln_wage grade race age age2 ttl_exp ttl_exp2 tenure tenure2 not_smsa south, re sa theta estimates store est1 xtreg ln_wage grade race age ttl_exp tenure not_smsa south, re sa theta estimates store est2 hausman est1 est2 ** instrumental variable and 2SLS estimation of the data * GLS Random-effects model xtivreg ln_wage age* not_smsa race (tenure = union south race), re theta first xtivreg ln_wage age* not_smsa race (tenure = union south race), ec2sls theta small