Here is another way to do it.
Before your likelihood code, initialise a variable to save the likelihood
gen ml_save = .
Within your likelihood code, follow each update of `lnf' by
replace ml_save = `lnf'
After running your likelihood code, investigate the saved variable.
This pivots on the simple idea that a permanent variable remains in memory until -drop-ped explicity. Naturally, use a name that does not clash with others, and beware that use of e.g. -preserve- might frustrate this. This will add a bit to execution time.
Nick
[email protected]
Maarten buis
--- On Sun, 7/2/10, Stephane Mahuteau wrote:
> Could anyone let me know how I can save the log likelihood
> for every observation in my dataset and keep it as a
> variable?
You can recreate the likelihood relatively simply with the
-predictnl- command, as in the example below. Whether that
is efficient depends on how you want to use that variable.
*--------------------- begin example -----------------------------------
program drop _all
program define mynorm_lf
args lnf xb ln_sigma
qui replace `lnf' = lnnormalden($ML_y1, `xb', exp(`ln_sigma'))
end
sysuse auto, clear
ml model lf mynorm_lf (mu: mpg = foreign rep78 displacement) /ln_sigma
ml check
ml search
ml maximize
predictnl double lnf = lnnormalden(mpg,xb(#1),exp(xb(#2))) if e(sample)
// check if this is the correct variable
sum lnf, meanonly
di r(sum)
*----------------------- end example -----------------------------------
( For more on how to use examples I sent to statalist see:
http://www.maartenbuis.nl/stata/exampleFAQ.html )
*
* 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/