Statalist


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

st: Linear regression using Mata optimize


From   Nicola Orsini <Nicola.Orsini@ki.se>
To   statalist@hsphsun2.harvard.edu
Subject   st: Linear regression using Mata optimize
Date   Mon, 9 Feb 2009 18:10:52 +0100

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/



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