# Re: st: Linear regression using Mata optimize

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

```Thank you very much Bobby and Kit,

I got the point and the answer to my question.

```
Below is the syntax to estimate MLEs using Mata -optimize()- in linear regression.
```
Nicola

/* Linear regression using Mata optimize */

sysuse auto, clear
local xlist mpg _cons

mata
mata clear
st_view(X=0,.,("mpg"))
st_view(y=0,.,("price"))
X=X,J(rows(X),1,1)

void mlreg(todo, p, y, X, lf, g, H)
{
b = p[1,(1::cols(X)) ]
s=  p[1,(cols(X)+1)]
lf = lnnormalden( (y-X*b'):/s ) :- ln(s)
}

S = optimize_init()
optimize_init_evaluator(S, &mlreg())
optimize_init_evaluatortype(S, "v0")
optimize_init_params(S, J(1,cols(X)+1,3))
optimize_init_argument(S, 1, y)
optimize_init_argument(S, 2, X)
optimize_init_which(S,"max")

p = optimize(S)
vhat = optimize_result_V(S)

st_matrix("b", p[1,(1::cols(X))])
st_matrix("V", vhat[(1::cols(X)),(1::cols(X))])

end

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

ereturn post b V , dep(price)
ereturn di

xtmixed price mpg, ml

exit

On Feb 10, 2009, at 3:52 PM, Roberto G. Gutierrez, StataCorp wrote:

```
```Nicola Orsini <Nicola.Orsini@ki.se> asks:

```
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?
```
```
```
I haven't looked at the Mata code closely, but I see that Nicola is comparing his program to -regress-. -regress- gives OLS standard errors, which are _not_ maximum likelihood (ML). They are REML. The difference is one of dividing by n instead of (n-p), but it is enough to throw off the standard
```errors.

```
If Nicola wants to test his optimize() code, he should try comparing with
```-xtmixed, mle-, which fits ML linear regression as a degenerate case.

--Bobby
rgutierrez@stata.com
*
*   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/
```