Ok, I finally got to the bottom of this problem. But I don't
understand it at all; I'd be grateful for explanations.
It appears that, as soon as there are missing values somewhere in the
variables of the model (even if these observations are excluded via
[if] or [in]), the likelihood will not be saved. The following
example, using the auto data, shows that in the first case (rep78
contains missing values) nothing is saved. In the second case (no
missing values) it works! In the third case (one observation with all
missings added but then excluded from the sample) it doesn't work once
again.
The -trace- reveals that during iterations, the values are saved in
all three cases. I was unable to find the piece of internal code that
reverts everything back in the case of missings.
This doesn't make sense to me at all. Any ideas?
Many thanks,
Eva
(The example is in d0 only because my own program is d0. I also paste
the output, for comparison.)
*************************************
capture program drop weird_d0
program weird_d0
version 9.2
args todo b lnf
tempvar xb lnj
tempname lns sigma
mleval `xb' = `b', eq(1)
mleval `lns' = `b', eq(2) scalar
scalar `sigma' = exp(`lns')
// dependent variable:
local y $ML_y1
quietly {
/* standard regression model */
gen double `lnj' = log(normalden(`y',`xb',`sigma')) if $ML_samp==1
replace logLL = `lnj' if $ML_samp==1
mlsum `lnf' = `lnj' if $ML_samp==1
}
end
sysuse auto, clear
gen logLL = .
// First case: missing values in one variable (rep78)
ml model d0 weird_d0 (Model: price = rep78 foreign weight ) /lnsigma
ml maximize, nolog
sum logLL
// Second case: no missing values
ml model d0 weird_d0 (Model: price = mpg foreign weight ) /lnsigma
ml maximize, nolog
sum logLL
di r(sum)
// Third case: add an empty observation, and exclude it.
replace logLL = .
count
set obs 75
ml model d0 weird_d0 (Model: price = mpg foreign weight ) /lnsigma in 1/74
ml maximize, nolog
sum logLL
*************************************
. sysuse auto, clear
(1978 Automobile Data)
.
. gen logLL = .
(74 missing values generated)
.
. // First case: missing values in one variable (rep78)
. ml model d0 weird_d0 (Model: price = rep78 foreign weight ) /lnsigma
. ml maximize, nolog
initial: log likelihood = -<inf> (could not be evaluated)
feasible: log likelihood = -756.67855
rescale: log likelihood = -756.67855
rescale eq: log likelihood = -754.07605
Number of obs = 69
Wald chi2(3) = 67.96
Log likelihood = -624.14723 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
price | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
Model |
rep78 | 150.5707 312.1301 0.48 0.630 -461.1931 762.3345
foreign | 3444.848 800.2654 4.30 0.000 1876.357 5013.34
weight | 3.388606 .4113701 8.24 0.000 2.582335 4.194877
_cons | -5689.553 1724.021 -3.30 0.001 -9068.572 -2310.534
-------------+----------------------------------------------------------------
lnsigma |
_cons | 7.626673 .0851257 89.59 0.000 7.45983 7.793517
------------------------------------------------------------------------------
.
. sum logLL
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
logLL | 0
.
. // Second case: no missing values
. ml model d0 weird_d0 (Model: price = mpg foreign weight ) /lnsigma
. ml maximize, nolog
initial: log likelihood = -<inf> (could not be evaluated)
feasible: log likelihood = -811.54531
rescale: log likelihood = -811.54531
rescale eq: log likelihood = -808.73926
Number of obs = 74
Wald chi2(3) = 73.87
Log likelihood = -670.09901 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
price | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
Model |
mpg | 21.8536 72.18735 0.30 0.762 -119.631 163.3382
foreign | 3673.06 665.2357 5.52 0.000 2369.222 4976.898
weight | 3.464706 .6134653 5.65 0.000 2.262336 4.667076
_cons | -5853.696 3284.452 -1.78 0.075 -12291.1 583.7126
-------------+----------------------------------------------------------------
lnsigma |
_cons | 7.636454 .0821995 92.90 0.000 7.475346 7.797562
------------------------------------------------------------------------------
.
. sum logLL
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
logLL | 74 -9.055392 .9102058 -13.53397 -8.555032
. di r(sum)
-670.09904
.
.
. // Third case: add an empty observation, and exclude it.
. replace logLL = .
(74 real changes made, 74 to missing)
. count
74
. set obs 75
obs was 74, now 75
.
. ml model d0 weird_d0 (Model: price = mpg foreign weight ) /lnsigma in 1/74
. ml maximize, nolog
initial: log likelihood = -<inf> (could not be evaluated)
feasible: log likelihood = -811.54531
rescale: log likelihood = -811.54531
rescale eq: log likelihood = -808.73926
Number of obs = 74
Wald chi2(3) = 73.87
Log likelihood = -670.09901 Prob > chi2 = 0.0000
------------------------------------------------------------------------------
price | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
Model |
mpg | 21.8536 72.18735 0.30 0.762 -119.631 163.3382
foreign | 3673.06 665.2357 5.52 0.000 2369.222 4976.898
weight | 3.464706 .6134653 5.65 0.000 2.262336 4.667076
_cons | -5853.696 3284.452 -1.78 0.075 -12291.1 583.7126
-------------+----------------------------------------------------------------
lnsigma |
_cons | 7.636454 .0821995 92.90 0.000 7.475346 7.797562
------------------------------------------------------------------------------
.
. sum logLL
Variable | Obs Mean Std. Dev. Min Max
-------------+--------------------------------------------------------
logLL | 0
.
2008/10/10 Eva Poen <[email protected]>:
> This is what I'm trying to implement:
> In my d0 likelihood evaluator, I would like to save the likelihood
> contribution (`lnf') for each observation in a variable, for later
> use. However, the newly created variable doesn't seem to be touched by
> the d0 evaluator, but there is no error message either.
>
> Here is what's in my estimation program:
> /*----mm3w0w1.ado---*/
> ...
> // save likelihood?
> if "`llsave'" != "" {
> confirm new variable `llsave'
> tempvar likesave
> qui gen double `likesave' = .
> global LL_save "`likesave'"
> }
> ...
> ml model d0 ...
> ml maximize ...
> ...
> // save likelihood?
> if "`llsave'" != "" {
> qui gen double `llsave' = `likesave'
> }
> ...
> /*-------------------------*/
>
> where llsave() is an option to the program, which takes the new
> variable name. The idea is to save a (temporary, in case the program
> fails) variable name in $LL_save, and then process it in the
> likelihood evaluator. In the evaluator, I have:
>
> /*----mm3w0w1_d0.ado---*/
> ...
> ...
> gen double `lnj' = ...
>
> // save likelihood?
> if "$LL_save" != "" {
> replace $LL_save = `lnj'
> }
>
> mlsum `lnf' = `lnj'
> ...
> /*-------------------------*/
>
> The program runs fine (without error) and produces the expected
> results, apart from the new variable. This variable will still be what
> I set it to in mm3w0w1.ado in the first place. Can someone tell me
> where the mistake is? I have a feeling that I'm missing something very
> obvious.
>
>
> Thanks,
> Eva
> *
> * 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/
>
*
* 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/