From Colin Cameron
http://cameron.econ.ucdavis.edu/stata/stata.html
* This program does
* (1) Poisson regression using Stata MATA matrix commands
* (2) Poisson regression using Stata command poisson as check
* using number of bids data
* Invoke MATA
mata
// Create y vector as well as X matrix from Stat data set using st_view command
st_view(y=., ., “numbids”)
st_view(X=., ., (”leglrest”, “whtknght”, “bidprem”, “cons”))
Xnames = (”leglrest”, “whtknght”, “bidprem”, “cons”) // used to label output
// Obtain starting values from ols regression of lny on x
b = invsym(X’X)*X’(ln(y+(y:==0)*0.1))
n = rows(X)
// Newton-Raphson iterations to computer Poisson MLE
// Loop until parameter modification less than 10^-16
cha = 1
iter = 1
do {
mu = exp(X*b)
sco = (X’(y-mu))/n
hes = -(X’(X:*mu))/n
bold = b
b = bold + invsym(-hes)*sco
cha = (bold-b)’(bold-b)/(b’b)
iter = iter + 1
} while (cha > 1e-16)
iter
bmle = b
// Compute standard errors using A^-1 * B * A^-1
k = cols(X)
gradmatrix = X:*(y-mu)
Vmle = invsym(-n*hes)*(gradmatrix’gradmatrix)*invsym(-n*hes)*(n/(n-k))
semle = sqrt(diagonal(Vmle))
tmle = bmle:/semle
pmle = 2*ttail(n-k, abs(tmle))
// Print results
(bmle, semle, tmle, pmle)
// Easier to read output includes regressor names
(Xnames’, strofreal(bmle), strofreal(semle), strofreal(tmle), strofreal(pmle))
end
|