UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

Using xtreg

clear
input id y80 y81 y82 x80 x81 x82
1  5  6  7  8  9 10
2 10 11 12  5  6  7
3 15 16 17  1  2  3
4  3  4  5  6  8 10
end

* 1. Reshape the data
reshape long x y, i(id) j(year)

* 2. identify i and t
iis id
tis year

save xt, replace

* 3. describe xt data
xtdes
xtsum
xttab x
xttrans x

* 4. normal OLS
***************************************************
regress y x

* normal OLS, clustering and robust std errors
regress y x, cluster(id) robust



* 5. illustrate between regression
*****************************************************************
*    associates mean of x (across id) with mean y (across id)
* between regression 1
* could collapse across id, generate mean of x and y
sort id
collapse (mean) meany=y meanx=x, by(id)
regress meany  meanx

* between regression 2
use xt, clear
egen xbar = mean(x), by(id)
regress y xbar

* between regression via xtreg 3
xtreg y x, be

* 6. illustrate within regression
****************************************************************
*    associates within x within id with y within id
* within regression 1
use xt, clear
sort id
by id: regress y x

* within regression 2
* params are right, ses are wrong
use xt, clear
egen xbar = mean(x), by(id)
generate xwith = x - xbar
egen ybar = mean(y), by(id)
generate ywith = y - ybar
regress ywith xwith

* within regression 3
* separate intercepts for each group
* (making intercepts random in SAS?, like re in stata?)
tabulate id, generate(id)
regress y x id1 id2 id3 id4, nocons

* within regression via xtreg 3
use xt, clear
xtreg y x, fe


* 7. xtreg with random effects
***********************************************************
xtreg y x, re theta
* check fit of re model
xthaus

* try mle
xtreg y x, mle


* 8. xtgls
***********************************************************
* only use when time points >= groups
use xt, clear

* default
xtgls y x, panels(iid) corr(independent)

* autocorrelated errors
xtgls y x, panels(iid) corr(ar1)

* panel specific autocorrelated errors
xtgls y x, panels(iid) corr(psar1)

* heteroschedastic error structure, with autocorrelation
xtgls y x, panels(hetero) corr(ar1)

* heteroschedastic error structure correleted between panels, with autocorrelation
xtgls y x, panels(correlated) corr(ar1)

9. xtgee
xtgee y x, family(gaussian) link(id) corr(exchangeable)
* same as
xtreg y x, pa



* how would this compare to 
* proc mixed as repeated measures with time varying covariates
* sas proc glm as repeated measure with time varying covariates


* see also
*   xtgee
*   xtgls
*   xtpois
*   xtprobit



log close

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California