Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


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

st: problem initial values ml with lf0 vs. lf


From   Olivier Francois <[email protected]>
To   "[email protected]" <[email protected]>
Subject   st: problem initial values ml with lf0 vs. lf
Date   Wed, 28 Nov 2012 17:13:01 +0000

Hi,

I have coded a likelihood function I need to maximize in stata.
I first coded it with lf, but it was having problems converging. So I decided to try lf1.
In the process I was having trouble, so decided to first go to using lf0.

I am having a problem with lf0, however, that stata doesn't find feasible initial values for the function.
However, it is finding feasible values and manages to evaluate the likelihood function with lf.

I tried to find info online but couldn't find anything. Anybody would have an idea of what is going on? Is there a mistake in how I coded the lf0 function?

Below the code, first for the lf version and then for the lf0.

Thank you very much for the help,


//Generating the data
clear all
set more off
webuse sysdsn1, clear
gen cens = 0.1*age + 0.5*male + rnormal()
sum cens, detail
gen heck = cens <= `r(p75)'
clonevar y = insure
replace y = . if heck == 0

global R = 50

gen rep0 = heck == 0
gen rep1 = y==1 if y <. & heck == 1 // base outcome
gen rep2 = y==2 if y <. & heck == 1
gen rep3 = y==3 if y <. & heck == 1

global x0 "rep0"
global y1 "rep1"
global y2 "rep2"
global y3 "rep3"

//random elements:
matrix p = (7)
mdraws, neq(1) draws($R) prefix(ed) primes(p) burn(10)
forval i = 1/$R {
        global ed_`i' "ed1_`i'"
}

********************************
//LIKELIHOOD FUNCTION, lf
capture program drop my_ll
program define my_ll
        version 11.2
        args lnf xgam tau xb1 xb2 sig1 sig2
        tempvar tt1 tt2 tt3 sp0 sp1 sp2 sp3
        gen double `tt1' = 0
        gen double `tt2' = 0
        gen double `tt3' = 0
        gen double `sp0' = 0
        gen double `sp1' = 0
        gen double `sp2' = 0
        gen double `sp3' = 0

        quietly {
        forval i = 1/$R {
                replace `tt1' = exp(`xb1' + `sig1'*$ed_`i')
                replace `tt2' = exp(`xb2' + `sig2'*$ed_`i')
                replace `tt3' = `xgam' + `tau'*$ed_`i'

                replace `sp0' = `sp0' + normal(-`xgam' - `tau'*$ed_`i')
                replace `sp1' = `sp1' + 1/(1 + `tt1' + `tt2')*normal(`tt3')
                replace `sp2' = `sp2' + `tt1'/(1 + `tt1' + `tt2')*normal(`tt3')
                replace `sp3' = `sp3' + `tt2'/(1 + `tt1' + `tt2')*normal(`tt3')
        }
        replace `lnf' = $x0*ln(`sp0'/$R) + $y1*ln(`sp1'/$R) + $y2*ln(`sp2'/$R) + ///
                        $y3*ln(`sp3'/$R)
        }
end

xi: ml model lf my_ll (xgam: heck = age i.male) /tau ///
                                (xb1: y = age i.male nonwhite i.site) (xb2: y = age i.male nonwhite i.site) ///
                                /sig1 /sig2 ///
                                , technique(nr)

********************************
//LIKELIHOOD FUNCTION, lf0
capture program drop my_ll1
program define my_ll1
        version 11.2
        args todo b lnf

        //Generating the variables
        tempvar xgam tau xb1 xb2 sig1 sig2
        mleval `xgam' = `b', eq(1)
        mleval `tau' = `b', eq(2) scalar
        mleval `xb1' = `b', eq(3)
        mleval `xb2' = `b', eq(4)
        mleval `sig1' = `b', eq(5) scalar
        mleval `sig2' = `b', eq(6) scalar

        quietly {

        //Generating the likelihood function
        tempvar tt1 tt2 tt3 sp0 sp1 sp2 sp3
        gen double `tt1' = 0
        gen double `tt2' = 0
        gen double `tt3' = 0
        gen double `sp0' = 0
        gen double `sp1' = 0
        gen double `sp2' = 0
        gen double `sp3' = 0

        forval i = 1/$R {
                replace `tt1' = exp(`xb1' + `sig1'*$ed_`i')
                replace `tt2' = exp(`xb2' + `sig2'*$ed_`i')
                replace `tt3' = `xgam' + `tau'*$ed_`i'

                replace `sp0' = `sp0' + normal(-`xgam' - `tau'*$ed_`i')
                replace `sp1' = `sp1' + 1/(1 + `tt1' + `tt2')*normal(`tt3')
                replace `sp2' = `sp2' + `tt1'/(1 + `tt1' + `tt2')*normal(`tt3')
                replace `sp3' = `sp3' + `tt2'/(1 + `tt1' + `tt2')*normal(`tt3')
        }
        mlsum `lnf' = $x0*ln(`sp0'/$R) + $y1*ln(`sp1'/$R) + $y2*ln(`sp2'/$R) + ///
                        $y3*ln(`sp3'/$R)
        }
end

xi: ml model lf0 my_ll1 (heck = age i.male) () ///
                                (y = age i.male nonwhite i.site) (y = age i.male nonwhite i.site) ///
                                () () ///
                                , technique(nr)

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/faqs/resources/statalist-faq/
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index