Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: fe & robust using mata


From   AbdelRahmen El Lahga <[email protected]>
To   [email protected]
Subject   st: fe & robust using mata
Date   Thu, 23 Feb 2006 17:49:39 +0100

I try to write a code estimating fe model for unbalanced data using mata.in order to practice my mata!! (see the code below)
I find the same coefficient vector as the command xtreg
but my programmation of the robust se vector is certainly wrong it give se like 1.37e+15..;
(see the last 10 lines of the code below)
any suggestions
Abdelrahmen
version 9.1
mata:
mata clear

void fe () {
stata ("drop _all") stata ("use c:\projets\nicolas\sas\table")
stata("sort idfamt")
y_init=x_init=s_init=.
st_view(y_init, ., "f_week_hours")
st_view(x_init, ., ("f_lnwage", "m_lnwage", "assetinc" , "kids01" , "kids2_4", "kids5p" , ///
"pchange" , "puls_1" , "puls0" , "puls1" , "f_age"))
st_view(s_init, ., "s")
tmax=21 // tmax periode maximale d'observation
n=rows(x_init)/tmax // nombre d'individus
nt=rows(x_init) //nombre total des lignes
k=cols(x_init) // nombre de regresseurs
k
y=J(nt,1,0)
x=J(nt,k,0)
s=s_init
for (i=1;i<=nt;i++) {
if (s[i]==1) {
y[i,.]= y_init[i,.] x[i,.]= x_init[i,.]
}
}
y_within=J(nt,1,0)
x_within=J(nt,k,0)
compt=0
for (i=1;i<=n;i++) {
compt=compt+tmax
rec1=compt-tmax+1 // numero 1ere ligne d'apparition
rec2=compt // numero derniere ligne d'apparition, la boucle marche jusqu'ici
si=s[|rec1 \ rec2|]
Ti=sum(si)
yi=y[rec1..rec2,.]
xi=x[rec1..rec2,.]
y_mean=colsum(yi)/Ti
x_mean=colsum(xi)/Ti
y_within[rec1..rec2,.]=si:*(yi:-y_mean)
x_within[rec1..rec2,.]=si:*(xi:-x_mean)

xi=si:*x_within[rec1..rec2,.]
yi=si:*y_within[rec1..rec2,.]
x[rec1..rec2,.]=xi
y[rec1..rec2,.]=yi
}
xx=cross(x,x)
xy=cross(x,y)
betaFE=invsym(xx)*xy
betaFE // tres bien Abdel!! courage
/* Matrice de varcoc robuste a l'heteroscedasdicite et l'autocorrelation */
uhat=y_within-x_within*betaFE // Wooldridge page 275 et page 581
xuux=x'*uhat*uhat'*x
variance=invsym(xx)*xuux*invsym(xx)
ee=diagonal(variance)
ee
se=sqrt(ee)
se
tstat=betaFE:/se
tstat
}

fe ()
end

*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/




© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index