[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: re: Linear regression using Mata optimize

From   Christopher Baum <>
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



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


   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)


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
An Introduction to Modern Econometrics Using Stata:

*   For searches and help try:

© Copyright 1996–2015 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index