*
*  Manual Example 5.8
*  Example ROBUST.PRG
*  Based upon Example 16.6 from Greene's Econometric Analysis, 5th ed
*
open data zellner.prn
data(format=prn,org=columns) 1 25 valueadd capital labor nfirm
*
set logy = log(valueadd)
set logk = log(capital)
set logl = log(labor)
*
* Compute linear regression, and the standardized residuals
*
linreg logy / resids
# constant logk logl
prj(xvx=px)
set stdresids = resids/sqrt(%seesq*(1-px))
*
* Rerun the regression, omitting the outliers (defined here as
* observations with standardized residuals greater than 2.5 in
* absolute value.
*
linreg(smpl=abs(stdresids)<=2.5) logy
# constant logk logl
*
* Now do LAD estimator
*
rreg logy
# constant logk logl
*
* Iterated WLS M-estimator
*
compute c = sqrt(%seesq)
dec vector beta0
*
*  Start with least squares (spread = constant)
*  Do 10 iterations or until convergence
*
*
*  Start with least squares (spread = constant)
*  Do 10 iterations or until convergence
*
set spread = 1.0
do iters=1,10
   compute beta0=%beta
   set spread = sqrt(c**2+resids**2)
   linreg(noprint,spread=spread) logy / resids
   # constant logk logl
   if %testdiff(beta0,%beta)<.0001
      break
end do iters
disp 'Iterations Taken' iters
*
*  Compute the sandwich estimator for the covariance matrix
*
set f      = resids/spread
set fprime = c**2/spread**3
mcov(matrix=b,lastreg) / f
mcov(matrix=a,lastreg,nosquare) / fprime
compute %xx = %mqform(b,inv(a))
linreg(create,noscale,lastreg,title='Iterated Weighted Least Squares')


