Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
st: programming weighted two stage least squares
From
Andrew Barnes <[email protected]>
To
[email protected]
Subject
st: programming weighted two stage least squares
Date
Wed, 30 Mar 2011 16:50:43 -0700
Hello all -
I programmed up this weighted two stage least squares but I'm not sure if
it's correct. I love some input on it. Thanks, Andrew
I have a model y2=y1 + x + e where y1 is endogenous (as is y2 obviously), x
is some exogenous determinant of y2, and the error terms (e) are
heteroscedastic. In the single equation model, I'm using weighted least
squares a la Wooldridge
reg y2 y1 x
predict double y2hat, xb
replace y2hat=0.999 if y2hat>=1
replace y2hat=0.001 if y2hat<=0
gen WTy2hat=1/(y2hat*(1-y2hat))
reg y2 y1 x [w=WTy2hat]
Now, to resolve the endogeneity, I want to run a weighted 2SLS model using
instruments z (so the reduced form first stage is y1=x+z+n, where n is an
error term)
I have done the following:
***W2SLS BY HAND
***weighted first stage
reg y1 x z
predict double y1hat, xb
replace y1hat=0.999 if y1hat>=1
replace y1hat=0.001 if y1hat<=0
drop y1hat
gen WTy1hat=1/(y1hat*(1-y1hat))
reg y1 x z [w=WTy1hat]
**get predicted y1hat from weighted first stage
predict double y1hat, xb
***weighted second stage
reg y2 y1hat x
predict double y2hat, xb
replace y2hat=0.999 if y2hat>=1
replace y2hat=0.001 if y2hat<=0
gen WTy2hat=1/(y2hat*(1-y2hat))
reg y2 y1hat x [w=WTy2hat]
***calculate correct VCE
*(from http://www.stata.com/statalist/archive/2010-02/msg00307.html)
scalar rmsebyhand=e(rmse)
*wrong VCE
mat vbyhand=e(V)
scalar dfk=e(df_r)
**correct resids
gen double eps2=(y2- _b[y1hat]*y1 - _b[x]*x - _b[_cons])^2
su eps2
***correct rmse
scalar rmsecorr=sqrt(r(sum)/dfk)
***correct VCE
mat vcorr=(rmsecorr/rmsebyhand)^2*vbyhand
mat li vcorr
****display b vector and correct standard errors
qui reg y2 y1hat x [w=WTy2hat]
matrix b=e(b)
matrix list b
matrix colnames vcorr= y1hat x _cons
matrix rownames vcorr = y1hat x _cons
ereturn post b vcorr
ereturn display
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/