*! version 1.0.1 Jeremy Freese  17 June 2002  (SJ2-3: st0022)
capture program drop leastlikely
program define leastlikely

    syntax [varlist(default=none)] [if] [in] [, n(integer 5) Generate(varname) *]

    if "`e(cmd)'"=="clogit" | "`e(cmd)'"=="nlogit" | "`e(cmd)'"=="xtlogit" | /*
    */ "`e(cmd)'"=="blogit" | "`e(cmd)'"=="bprobit" | "`e(cmd)'"=="glogit" | /*
    */ "`e(cmd)'"=="gprobit" {
        di _n as err "leastlikely not intended for use after `e(cmd)'"
        exit 198
    }

    local erase "no"
    tempname values prob touse
    local depvar = "`e(depvar)'"
    local depvarlabel : value label `depvar'
    if "`generate'"=="" {
        local generate "Prob"
        local erase "yes"
    }
    quietly {
        gen `touse' = e(sample) `if' `in'
        gen `prob' = .
        tabulate `depvar' if e(sample)==1, matrow(`values')
        local temp : rownames `values'
        local numcats : word count `temp'
        forvalues i = 1(1)`numcats' {
            local value`i' = `values'[`i', 1]
        }
        local isbin "no"
        if `numcats'==2 & `value1'==0 & `value2'==1 { local isbin "yes" }
        if "`isbin'" == "yes" {
            tempname temp
            predict `temp', p
            replace `prob' = `temp' if `depvar'==1 & `touse'==1
            replace `prob' = (1-`temp') if `depvar'==0 & `touse'==1
        }
        if "`isbin'" == "no" {
            forvalues i = 1(1)`numcats' {
                tempname temp
                predict `temp', outcome(`value`i'')
                replace `prob' = `temp' if `depvar'==`value`i'' & `touse'==1
            }
        }
        gen `generate' = `prob'
    }

    forvalues i = 1(1)`numcats' {
        local vallabel ""
        if "`depvarlabel'"!="" {
            local vallabel : label `depvarlabel' `value`i''
            if "`vallabel'"!="" { local vallabel = "(`vallabel')" }
        }
        di _n as txt "Outcome: " as res `value`i'' " `vallabel'"

        tempname temp
        quietly egen `temp' = rank(`generate') if `depvar'==`value`i'' & `touse'==1, track
        list `generate' `varlist' if `temp'<=`n' , `options'
    }

    if "`erase'"=="yes" { drop `generate' }

end