 Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.

# st: programming a weighted two stage least squares model

 From Andrew Barnes To statalist@hsphsun2.harvard.edu Subject st: programming a weighted two stage least squares model Date Fri, 1 Apr 2011 08:38:43 -0700

```Hi folks,

I programmed up this weighted two stage least squares (W2SLS) but I'm
not sure if
it's correct. Also, I don't know if the canned post estimation
commands for ivregress (e.g., estat firststage, estat endog, estat
overid) will be valid for a W2SLS model.   Any thoughts or comments
are appreciated.  Cheers, 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/

*
*   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/
```