*! svrologit_p *! Prediction for svrmodel with cmd(ologit) or cmd(oprobit) *! Nicholas Winter version 1.0.0 31mar2004 * Modified from official Stata's ologit_p, version 1.1.2 21oct2003 program define svrologit_p version 6 /* Parse. */ syntax newvarlist [if] [in] [, Outcome(string) /* */ Index XB STDP Pr noOFFset ] /* Check syntax. */ local nopt : word count `index' `xb' `stdp' `pr' if `nopt' > 1 { di in red "only one of p, xb, or stdp can be specified" exit 198 } local type "`index'`xb'`stdp'`pr'" if ("`type'"=="index" | "`type'"=="xb" | "`type'"=="stdp") /* */ & `"`outcome'"'!="" { di in red "outcome() option cannot be " /* */ "specified with `type' option" exit 198 } /* Index, XB, or STDP. */ if "`type'"=="index" | "`type'"=="xb" | "`type'"=="stdp" { Onevar `type' `varlist' if e(df_m) != 0 | ("`e(offset)'"!="" & "`offset'"=="") { tempname bb bb2 VV VV2 hold /* This lops off the cut points from */ tempvar h_esamp /* the estimation results, in order */ mat `bb' = e(b) /* to exclude them from the prediction */ mat `VV' = e(V) /* of xb and stdp */ local ncoef = e(df_m) mat `bb2' = `bb'[1,1..`ncoef'] mat `VV2' = `VV'[1..`ncoef',1..`ncoef'] estimates hold `hold' , restore varname(`h_esamp') est post `bb2' `VV2' _predict `typlist' `varlist' `if' `in', `type' `offset' estimates unhold `hold' /* put back the real estimates */ } else gen `typlist' `varlist' = . `if' `in' if "`type'"=="index" | "`type'"=="xb" { label var `varlist' /* */ "Linear prediction (cutpoints excluded)" } else { /* stdp */ label var `varlist' /* */ "S.E. of linear prediction (cutpoints excluded)" } exit } /* If here we compute probabilities. Do general preliminaries. */ local cut "_cut" /* _b[_cut1] */ if "`e(model)'"=="Ordered Logit" { local func "1/(1+exp(" local funcn "1-1/(1+exp(" } else { local func "normprob(-(" local funcn "normprob((" } tempvar touse mark `touse' `if' `in' if e(df_m) != 0 | ("`e(offset)'"!="" & "`offset'"=="") { tempname bb bb2 VV VV2 hold /* This lops off the cut points from */ tempvar h_esamp /* the estimation results, in order */ mat `bb' = e(b) /* to exclude them from the prediction */ mat `VV' = e(V) /* of xb and stdp */ local ncoef = e(df_m) mat `bb2' = `bb'[1,1..`ncoef'] mat `VV2' = `VV'[1..`ncoef',1..`ncoef'] estimates hold `hold' , restore varname(`h_esamp') est post `bb2' `VV2' tempvar xb qui _predict double `xb' if `touse', xb `offset' estimates unhold `hold' /* put back the real estimates */ } else local xb 0 /* Probability with outcome() specified: create one variable. */ if ("`type'"=="pr" | "`type'"=="") & `"`outcome'"'!="" { if "`type'"=="" { di in gr "(option p assumed; predicted probability)" } Onevar "p with outcome()" `varlist' Eq `outcome' local i `s(icat)' local im1 = `i' - 1 sret clear if `i' == 1 { gen `typlist' `varlist' = /* */ `func'`xb'-_b[`cut'1])) /* */ if `touse' } else if `i' < e(k_cat) { gen `typlist' `varlist' = /* */ `func'`xb'-_b[`cut'`i'])) /* */ - `func'`xb'-_b[`cut'`im1'])) /* */ if `touse' } else { gen `typlist' `varlist' = /* */ `funcn'`xb'-_b[`cut'`im1'])) /* */ if `touse' } local val = el(e(cat),1,`i') label var `varlist' "Pr(`e(depvar)'==`val')" exit } /* Probabilities with outcome() not specified: create e(k_cat) variables. */ if "`type'"=="" { di in gr "(option p assumed; predicted probabilities)" } local n : word count `varlist' if `n' != e(k_cat) { capture noisily error cond(`n'= 1 capture assert `out' <= e(k_cat) if _rc { di in red "there is no outcome #`out'" _n /* */ "there are only `e(k_cat)' categories" exit 111 } sret local icat `"`out'"' exit } Chk confirm number `out' local i 1 while `i' <= e(k_cat) { if `out' == el(e(cat),1,`i') { sret local icat `i' exit } local i = `i' + 1 } di in red `"outcome `out' not found"' Chk assert 0 /* return error */ end program define Chk capture `0' if _rc { di in red "outcome() must either be a value of `e(depvar)'," /* */ _n "or #1, #2, ..." exit 111 } end program define Onevar gettoken option 0 : 0 local n : word count `0' if `n'==1 { exit } di in red "option `option' requires that you specify 1 new variable" error cond(`n'==0,102,103) end