*
* This is from Pagan and Ullah, Nonparametric Econometrics, Cambridge, 1999,
* pp 250-251.
*
open data housing.csv
data(format=prn,org=columns) 1 59 price age aircon baths bedrms cond corner culd dish fence firepl floors garage irreg lajolla lndry patio pool rooms sprink sqft view yard
set lsqft = log(sqft)/log(10)
set lyard = log(yard)/log(10)
*
* Estimate linear regression with White standard errors
*
linreg(robusterrors) price / resids
# sqft yard pool lajolla baths firepl irreg sprink view lsqft lyard constant
*
* Adaptive kernel estimator. For the regression equation y=Xb+u, if
* the density of u is the (unknown) f, the gradient of the log likelihood
* is - sum {x(t) f'(u(t))/f(u(t)}. The adaptive kernel estimator is
* a two-step estimator starting with OLS and using kernel estimates of
* psi=f'/f.
*
* We need to compute the density at the given data points, so grid=input is
* used with the resids providing the evaluation points.
*
density(type=gauss,derives=f1,grid=input) resids / resids fu
set psi = f1/fu
*
* Compute the sample mean of psi**2 and the X'X matrix as the pieces
* required for the information matrix. (u and X are assumed to be
* independent).
*
compute psisq=%normsqr(psi)/%nobs
cmom(lastreg,nodepvar,matrix=ibxx)
compute ibxx=ibxx*psisq
*
* The mcov of psi with the regressors is needed to compute the
* covariance matrix.
*
mcov(lastreg,matrix=ib) / psi
*
* Compute the gradient. Note the use of nodepvar, since cmom(lastreg) will
* by default add the dependent variable to the list.
*
cmom(lastreg,nodepvar,zxmatrix=d)
# psi
*
* Compute the adjusted beta and the sandwich estimator of the covariance
* matrix.
*
compute beta=%beta-inv(ibxx)*d
compute %xx=inv(ibxx)*ib*inv(ibxx)
linreg(create,lastreg,coeffs=beta,noscale,title='Adaptive Kernel Estimator')

