DEFINE hcreg (const !tokens(1) !default(1)
/method !tokens(1) !default(3)
/format !tokens(1) !default(f8.4)
/dv !tokens(1)
/iv !cmdend).
set mxloop = 1000000000.
MATRIX.
GET x/file = */variables = !dv !iv/names = dv/missing = omit.
compute y=x(:,1).
compute x=x(:,2:ncol(x)).
compute n = nrow(x).
do if (!const = 0).
compute iv = t(dv(1,2:ncol(dv))).
print /title '* Regression throught the origin.'.
else.
compute iv = t({"Constant", dv(1,2:ncol(dv))}).
compute con = make(n,1,1).
compute x={con,x}.
end if.
compute dv=dv(1,1).
compute invXtX = inv(t(x)*x).
compute b = invXtX*t(x)*y.
compute k = ncol(x).
compute h = sqrt(make(n,1,n/(n-k))).
do if (!method ne 1).
loop i=1 to n.
compute h(i)= x(i,:)*invXtX*t(x(i,:)).
end loop.
end if.
compute resid = y-x*b.
do if (!method = 0).
loop i=1 to k.
compute x(:,i) = resid&*x(:,i).
end loop.
print/title = "Method = HC0".
else if (!method = 1).
loop i=1 to k.
compute x(:,i) = (resid&*h)&*x(:,i).
end loop.
print/title = "Method = HC1".
else if (!method = 2).
loop i=1 to k.
compute x(:,i) = (resid&/((1-h)&**(1/2)))&*x(:,i).
end loop.
print/title = "Method = HC2".
else if (!method = 3).
loop i=1 to k.
compute x(:,i) = (resid&/(1-h))&*x(:,i).
end loop.
print/title = "Method = HC3".
else if (!method = 4).
compute delta = h&*(n/k).
compute lim = make(n,1,4).
compute temp1 = delta gt lim.
compute temp2 = delta le lim.
compute delta = (temp1&*4+temp2&*delta)&/2.
loop i=1 to k.
compute x(:,i) = (resid&/(1-h)&**delta)&*x(:,i).
end loop.
print/title = "Method = HC4".
end if.
compute hc = invXtX*t(x)*x*invXtX.
print dv/title = "Criterion variable is:"/format A8.
print n/title = "Sample size is:".
compute sebhc = sqrt(diag(hc)).
compute te = b&/sebhc.
compute p = 2*(1-tcdf(abs(te), n-nrow(b))).
compute oput = {b,sebhc, te, p}.
print oput/title = 'Heteroscedasticity-Consistent Regression Results'/clabels
= "B" "S.E." "t" "P>|t|"/rnames = iv/format !format.
compute iv2 = t(iv).
print hc/title = 'Heteroscedasticity-Consistent Covariance Matrix'/cnames =
iv/rnames = iv2/format !format.
END MATRIX.
!END DEFINE.