Post e (b) vector from user program to Stata
I wrote a program that calculates a weighted regression and now I want the estimation results to be stored as a vector e (b) so that bootstrap can easily access the results, but I keep getting the error. My program looks like this:
capture program drop mytest
program mytest, eclass
version 13
syntax varlist [if]
marksample touse
// mata subroutine creates matrix `b', such as mata: bla("`varlist'", "`touse'")
tempname b
matrix `b' = (1\2\3)
ereturn post `b'
end
mytest town_id
ereturn list
But I keep getting conformability error r(503);
when I run the script. When I put a normal matrix like this instead ereturn matrix x = b
, everything works fine, but I would like my coefficients to be stored "correctly" in the vector e(b)
.
I checked the Stata documentation but couldn't figure out why this doesn't work. Their advice is to code
tempname b V
// produce coefficient vector `b' and variance–covariance matrix `V'
ereturn post `b' `V', obs(`nobs') depname(`depn') esample(`touse')
The ereturn message options are optional. Can anyone tell me what I am missing here? Thank!
source to share
Use the "row" vector instead of the "column" vector. If you check the saved results, for example, regress
you can see that this is what is expected.
capture program drop mytest
program mytest, eclass
version 13
syntax varlist [if]
marksample touse
// mata subroutine creates matrix `b', such as mata: bla("`varlist'", "`touse'")
tempname b
matrix `b' = (1,2,3)
ereturn post `b'
end
*----- tests -----
clear
sysuse auto
// mytest test
mytest mpg weight
ereturn list
matrix list e(b)
// regress example
regress price weight mpg
ereturn list
matrix list e(b)
source to share