# Re: st: Re: Linear regression using Mata optimize

 From Nicola Orsini To statalist@hsphsun2.harvard.edu Subject Re: st: Re: Linear regression using Mata optimize Date Tue, 10 Feb 2009 09:07:57 +0100

```Martin,

of course I know many other ways I could fit a linear regression model.
```
However, my question was on the use of optmize(). I don't want to avoid it, I just want to better understand how it works.
```
Nicola

Martin Weiss wrote:
```
```<>

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