Hi,
I'm building a new likelihood estimation ado command in Stata and I've written the likelihood in Mata. However, I wanted to avoid using a library and include the Mata code at the bottom of the ado file (this is the second approach mentioned here: http://www.stata.com/statalist/archi.../msg00712.html).
Using this approach with ml seems to return an error where the mata evaluator is not found, but mata functions defined normally work as they should with the code included below the ado program.
I tried exploring this issue by testing whether stata is compiling the evaluator by running "mata: example_lf()" which spits out an "expected argument" error (as we would expect) which shows that it does indeed compile when the program is called.
I whipped up a quick simple example to highlight the issue (adapted from Maximum Likelihood Estimation with Stata, 4th Edition):
------------ foomata.ado ------------------------
program foomata
version 13.1
if replay() {
if (`"`e(cmd)'"' != "foomata") error 301
Replay `0'
}
else Estimate `0'
end
program Estimate, eclass sortpreserve
syntax varlist(fv) [if] [in] [, ///
vce(passthru) ///
noLOG ///
noCONStant ///
Level(cilevel) /// -Replay- option
* /// -mlopts-
]
// check syntax
mlopts mlopts, `options'
gettoken lhs rhs : varlist
// mark the estimation sample
marksample touse
// fit the full normal model
ml model lf probit_lf() ///
(`lhs' = `rhs'), `constant' ///
if `touse' ///
`vce' ///
`log' ///
`mlopts' ///
maximize
ereturn local cmd foomata
Replay, level(`level')
end
program Replay
syntax [, Level(cilevel)]
ml display, level(`level')
end
mata:
void probit_lf(transmorphic scalar ML, real rowvector b, real colvector lnfj)
{
real vector depvar, xb
depvar = moptimize_util_depvar(ML, 1)
xb = moptimize_util_xb(ML, b, 1)
lnfj = depvar:*ln(normal(xb)) + (1:-depvar):*ln(normal(-xb))
}
end
------------ foomata.ado ------------------------
------------ runcommand.do -------------------
set more off
capture program drop _all
sysuse cancer, clear
foomata died age
------------ runcommand.do -------------------
Running the do file will return the error "evaluator probit_lf() not found". I know that using a library or pre-compiling the mata code will make the command work, but I wanted to know if there was a fix to make this approach of including the mata code at the bottom of the ado file work with ml model.
Thanks in advance for the help, I look forward to hearing about any potential solutions.
Sarwar
I'm building a new likelihood estimation ado command in Stata and I've written the likelihood in Mata. However, I wanted to avoid using a library and include the Mata code at the bottom of the ado file (this is the second approach mentioned here: http://www.stata.com/statalist/archi.../msg00712.html).
Using this approach with ml seems to return an error where the mata evaluator is not found, but mata functions defined normally work as they should with the code included below the ado program.
I tried exploring this issue by testing whether stata is compiling the evaluator by running "mata: example_lf()" which spits out an "expected argument" error (as we would expect) which shows that it does indeed compile when the program is called.
I whipped up a quick simple example to highlight the issue (adapted from Maximum Likelihood Estimation with Stata, 4th Edition):
------------ foomata.ado ------------------------
program foomata
version 13.1
if replay() {
if (`"`e(cmd)'"' != "foomata") error 301
Replay `0'
}
else Estimate `0'
end
program Estimate, eclass sortpreserve
syntax varlist(fv) [if] [in] [, ///
vce(passthru) ///
noLOG ///
noCONStant ///
Level(cilevel) /// -Replay- option
* /// -mlopts-
]
// check syntax
mlopts mlopts, `options'
gettoken lhs rhs : varlist
// mark the estimation sample
marksample touse
// fit the full normal model
ml model lf probit_lf() ///
(`lhs' = `rhs'), `constant' ///
if `touse' ///
`vce' ///
`log' ///
`mlopts' ///
maximize
ereturn local cmd foomata
Replay, level(`level')
end
program Replay
syntax [, Level(cilevel)]
ml display, level(`level')
end
mata:
void probit_lf(transmorphic scalar ML, real rowvector b, real colvector lnfj)
{
real vector depvar, xb
depvar = moptimize_util_depvar(ML, 1)
xb = moptimize_util_xb(ML, b, 1)
lnfj = depvar:*ln(normal(xb)) + (1:-depvar):*ln(normal(-xb))
}
end
------------ foomata.ado ------------------------
------------ runcommand.do -------------------
set more off
capture program drop _all
sysuse cancer, clear
foomata died age
------------ runcommand.do -------------------
Running the do file will return the error "evaluator probit_lf() not found". I know that using a library or pre-compiling the mata code will make the command work, but I wanted to know if there was a fix to make this approach of including the mata code at the bottom of the ado file work with ml model.
Thanks in advance for the help, I look forward to hearing about any potential solutions.
Sarwar
Comment