*! version 1.2.0 Jesper Sorensen 072898
program define zinb
    version 5.0
    local options "Level(integer $S_level) LOgit(string) nolog TRace nobase"
    if substr("`*'",1,1)=="," | "`*'"=="" {
		if "$S_E_cmd"!="zinb" { error 301 }
		parse "`*'"
	}
    else {
        local varlist "req ex"
        local if "opt"
        local in "opt"
        parse "`*'"
        if "`log'"=="nolog" {
            local pre1 "qui"
            }
        else local pre1 "noi"
        parse "`varlist'", parse(" ")
        local depv "`1'"
        mac shift 1
        local indv "`*'"
        tempvar b f V f0 mysamp
        /* constant only model */
        if "`base'" ~="nobase" {
            ml begin
            ml function zinb_ll
            ml method lf
            eq nb : `depv'
            eq logit :
            eq lnalpha :
            ml model `b'= nb logit lnalpha , depv(100)
            ml sample `mysamp' `if' `in'
            `pre1' ml maximize `f0' `V'
        /* estimate requested model */
        }
        ml begin
        ml function zinb_ll
        ml method lf
        eq nb : `depv' `indv'
        if "`logit'" ~= "" {
            eq logit : `logit'
        }
        else {
            eq logit : `indv'
        }
        eq lnalpha :
        ml model `b'= nb logit lnalpha , depv(100)
        ml sample `mysamp' `if' `in'
        `pre1' ml maximize f V, `trace'
        if "`base'"~="nobase" {
            ml post zinb, title("Zero Inflated Negative Binomial") lf0(`f0')
        }
        else {
            ml post zinb, title("Zero Inflated Negative Binomial")
        }
        global S_E_cmd "zinb"
    }
    ml mlout zinb, level(`level')
end