# st: Re: Linear regression using Mata optimize

 From "Martin Weiss" To Subject st: Re: Linear regression using Mata optimize Date Mon, 9 Feb 2009 20:12:33 +0100

```<>

Here`s a solution that avoids all the rocket science:
-help m1_interactive-

HTH
```
Martin _______________________ ----- Original Message ----- From: "Nicola Orsini" <Nicola.Orsini@ki.se>
```To: <statalist@hsphsun2.harvard.edu>
Sent: Monday, February 09, 2009 6:10 PM
Subject: st: Linear regression using Mata optimize

```
My question is why I'm not getting the correct variance-covariance matrix in the example below (simple linear regression) when using optimize_result_V(S).
```Any thoughts on this?

Nicola

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

st_matrix("b", betahat)
st_matrix("V", vhat)

end

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

ereturn post b V , dep(price)
ereturn di

reg price mpg

exit

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

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