# st: re: Linear regression using Mata optimize

 From Christopher Baum To statalist@hsphsun2.harvard.edu Subject st: re: Linear regression using Mata optimize Date Tue, 10 Feb 2009 13:03:31 -0500

```<>
```
Robby G. suggested a d-f adjustment (for n-k rather than n) was the issue. It isn't, as the optimize() matrix differs from OLS vce by a very large factor. What you need to do to get OLS (small-sample) standard errors is multiply the optimize() covariance matrix by [2 s^2]:
```
-----------
/* Linear regression using Mata optimize */

sysuse auto, clear

local xlist mpg cons

mata

st_view(x=0,.,("mpg"))
st_view(y=0,.,("price"))
x=x,J(rows(x),1,1)

void olsest(todo, b, y, x, lf, g, H)
{
e = (y-x*b')
lf = -(e'*e)
}

S = optimize_init()
optimize_init_evaluator(S, &olsest())
optimize_init_evaluatortype(S, "v0")
optimize_init_argument(S, 1, y)
optimize_init_argument(S, 2, x)
optimize_init_params(S, J(1,cols(x),0))

optimize_init_which(S,"max")

betahat = optimize(S)
vhat = optimize_result_V(S)

s2 = -1 * optimize_result_value(S) / (rows(y) - cols(x))
vhat = vhat * 2 * s2
st_matrix("b", betahat)
st_matrix("V", vhat)

end

matrix colnames b = `xlist'
matrix colnames V = `xlist'
matrix rownames V = `xlist'

mat list V
ereturn post b V , dep(price)
ereturn di

reg price mpg
mat list e(V)
--------------

```
Presumably if you divided by n rather than n-k in computing s^2, you would then agree with -xtmixed, mle- or -ivreg2 price mpg- standard error estimates.
```

Kit Baum, Boston College Economics and DIW Berlin
http://ideas.repec.org/e/pba1.html
An Introduction to Modern Econometrics Using Stata:
http://www.stata-press.com/books/imeus.html

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