*! version 0.1.0 2013-12-06 | vanheuvelen long

* tpr: postestimation analyses of ologit

program define tpr_sim_OLMpostest

* ologit results

    scalar OLstatus = 1
    matrix table = r(table)
    matrix bvec = table[1,1...]
    matrix sevec = table[2,1...]

* ic stats

    $qui estat ic
    scalar OLaic = el(r(S),1,5)
    scalar OLbic = el(r(S),1,6)

* OLM summary

    matrix OLvec = OLtried, OLstatus, OLeN, OLconverged, OLrc, ///
        bvec, sevec, $samplesize, $isample, OLaic, OLbic

* predicted probs

    local prnms ""
    forvalues i = $y_catmin/$y_catmax {
        local prnms "`prnms' PROL`i'"
    }
    qui predict `prnms' if $insample

* oparallel for wolfe and score tests

	$qui oparallel, wolfe score
	matrix OPvec = r(chi2_s), r(p_s), r(df), r(chi2_wg), r(p_wg), r(df)

*brant test

    $qui capture brant
    scalar BRtried = 1
    $qui di _new "  + BR rc " _rc
    scalar BRrc = _rc
    scalar BRstatus = 1
    if (BRrc==9993) scalar BRstatus = .d // 999 binary fail
    if (BRrc==2000) scalar BRstatus = .k // unclear what 2000 means
    if r(chi2) < 0 scalar BRstatus = .e
    matrix BRvec = BRtried, BRstatus, r(df), r(chi2), r(p), BRrc

    * individual coefs
    local ivar = 0
    foreach nm of varlist $x_names {
        local ++ivar
        if BRrc == 0 {
            scalar chi = el(r(ivtests),`ivar',1)
            scalar pr = el(r(ivtests),`ivar',2)
            scalar BRindstatus = BRstatus
            if chi < 0 {
                scalar BRindstatus = .l
                scalar pr = .
            }
            if (chi>=.) scalar BRindstatus = .m
            matrix BR`nm'vec = BRindstatus, $ncuts, chi, pr, BRrc
        }
        else matrix BR`nm'vec = .j, ., ., ., BRrc
    }

end
exit