📄 asmprobit_p.ado
字号:
*! version 1.0.4 03jun2005
/* predict for asmprobit */
program define asmprobit_p, sortpreserve
version 9
if "`e(cmd)'" != "asmprobit" {
di as error "{p}multinomial probit estimates from " ///
"{help asmprobit##|_new:asmprobit} not found{p_end}"
exit 301
}
syntax newvarname [if] [in] [, XB STDP Pr FORCE]
local predtype "`xb' `stdp' `pr'"
local nopt : word count `predtype'
if `nopt' > 1 {
di as error "only one of pr, xb, or stdp can be specified"
exit 198
}
local predtype = ltrim(rtrim("`predtype'"))
if "`predtype'" == "" {
di in gr "(option pr assumed; Pr(`e(altvar)')"
local predtype "pr"
}
marksample touse, novarlist
markout `touse' `e(indvars)' `e(ind2vars)'
if "`predtype'" == "pr" {
markout `touse' `e(altvar)' `e(casevar)', strok
}
tempvar altern
tempname alabel
local altvar `e(altvar)'
local alteqs `"`e(alteqs)'"'
local nalt : word count `alteqs'
cap qui egen `altern' = group(`altvar') if `touse', lname(`alabel')
if _rc > 0 {
/* should not happen */
di as error "failed to renumber alternative levels"
error _rc
}
qui label list `alabel'
local m = r(k)
sort `e(casevar)' `altvar'
if "`force'"=="" {
local bail = 0
local i = 1
while `i'<=`nalt' & `i'<=`m' & !`bail' {
local alti : label `alabel' `i'
local bail = (`"`e(alt`i')'"' != `"`alti'"')
local `++i'
}
if `bail' {
di as error "{p}the labels of the alternatives used " ///
"in the estimation model do not match those in " ///
"variable `altvar'; use the force option to ignore " ///
"labels{p_end}"
exit 322
}
}
if `m' > `nalt' {
di in gr "{p 0 7}Note: some cases have alternatives that are " ///
"not in the estimated model; predictions set to missing for " ///
"those cases{p_end}"
tempvar ni ni1
qui by `e(casevar)' : gen `ni' = sum(`touse')
local na1 = `nalt' + 1
forvalues i=`na1'/`m' {
qui replace `touse' = 0 if (`altern'==`i')
}
qui by `e(casevar)' : gen `ni1' = sum(`touse')
qui by `e(casevar)': replace `touse' = 0 if `ni1'[_N]!=`ni'[_N]
}
if "`predtype'" == "pr" {
/* get rid of singletons */
qui by `e(casevar)': replace `touse' = 0 if `ni1'[_N]==1
}
qui summarize `touse'
if r(sum) == 0 {
di as error "no cases remain after excluding invalid observations"
exit 2000
}
local neq : word count `e(ind2vars)'
local neq = `neq' + e(const)
tempvar result
if "`predtype'"=="xb" {
local caselvl `"`e(caselevels)'"'
tempvar vi
qui _predict `typlist' `result' if `touse', `predtype' equation(`altvar')
if `neq' > 0 {
forvalues i=1/`nalt' {
if (`i' == `e(i_base)') continue
local eq : word `i' of `alteqs'
qui _predict `vi' if `touse' & `altern'==`i', `predtype' ///
equation(`eq')
qui replace `result' = `result' + `vi' if `altern'==`i' & `touse'
drop `vi'
}
}
label var `result' `"xb for each alternative in variable `altvar'"'
}
else if "`predtype'"=="stdp" {
local neq : word count `e(ind2vars)'
local neq = `neq' + e(const)
if `neq' == 0 {
qui _predict `typlist' `result' if `touse', `predtype' equation(`altvar')
}
else {
cap confirm variable `e(depvar)'
local rmdep = (_rc > 0)
if `rmdep' {
/* predictnl requires depvar to exist in call to -tsrevar- */
qui gen `e(depvar)' = 0
}
local ibase `e(i_base)'
cap {
qui _predict `typlist' `result' if `touse' & `altern'==`ibase', ///
`predtype' equation(`altvar')
local i = 0
tempvar ti vi
foreach eq of local alteqs {
if (`++i' == `ibase') continue
qui predictnl `typlist' `ti'=xb(`altvar')+xb(`eq') ///
if `touse' & `altern'==`i', se(`vi')
qui replace `result' = `vi' if `touse' & `altern'==`i'
drop `vi' `ti'
}
} // end cap
local rc = _rc
if (`rmdep') drop `e(depvar)'
if (`rc' > 0) error `rc'
}
label var `result' `"Standard error of xb for each alternative in `altvar'"'
}
else /*if "`predtype'" == "pr"*/ {
macro drop MPROBIT_*
/* just in case there is an external panel info object, get rid of it */
cap mata : mata drop _mprobit_panel_info
global MPROBIT_TRACE 0
global MPROBIT_CHOLESKY = e(cholesky)
global MPROBIT_FULLCOV = e(fullcov)
global MPROBIT_NVAR = e(k_sigma)
global MPROBIT_NCOR = e(k_rho)
global MPROBIT_ALTERNATIVES `altern'
global MPROBIT_ID `e(casevar)'
global MPROBIT_ATTRIBS `e(indvars)'
global MPROBIT_NATTRIB = e(k_indvars)
global MPROBIT_NCASECOEF = `neq'
global MPROBIT_CASEVARS `e(ind2vars)'
global MPROBIT_CONST = e(const)
if e(k_alt) > 2 {
if ("`e(mc_seed)'" != "") global MPROBIT_SEED = e(mc_seed)
if ("`e(mc_burn)'" != "") global MPROBIT_BURN = e(mc_burn)
else global MPROBIT_BURN = 0
global MPROBIT_NPOINTS = e(mc_points)
global MPROBIT_ANTITHETICS = e(mc_antithetics)
local meth = substr("`e(mc_method)'",1,3)
if "`meth'" == "Hal" {
global MPROBIT_METHOD 1
}
else if "`meth'" == "Ham" {
global MPROBIT_METHOD 2
}
else {
global MPROBIT_METHOD 3
}
}
else {
global MPROBIT_BURN = 0
global MPROBIT_NPOINTS = 0
global MPROBIT_ANTITHETICS = 0
global MPROBIT_METHOD = 0
}
global MPROBIT_BASE = e(i_base)
global MPROBIT_NALT = `nalt'
global MPROBIT_BALANCED = 0
tempname corstruct corfixed order
matrix `corfixed' = e(corfixed)
matrix `corstruct' = e(corpattern)
matrix `order' = J(`nalt',1,0)
local indices : rownames `corstruct'
forvalues i=1/`nalt' {
local j : word `i' of `indices'
matrix `order'[`i',1] = `j'
matrix `corstruct'[`i',`i'] = el(e(stdpattern),1,`i')
matrix `corfixed'[`i',`i'] = el(e(stdfixed),1,`i')
}
global MPROBIT_CORSTRUCT `corstruct'
global MPROBIT_CORFIXED `corfixed'
global MPROBIT_ORDER `order'
global MPROBIT_TOUSE `touse'
tempname b negH lnf g
scalar `lnf' = 0.0
matrix `b' = e(b)
cap noi {
qui gen `typlist' `result' = .
asmprobit_lf -1 `b' `lnf' `g' `negH' `result'
label var `result' `"Probability of each alternative in variable `altvar'"'
}
local rc = _rc
cap mata : mata drop _mprobit_panel_info
macro drop MPROBIT_*
if (`rc' > 0) exit `rc'
}
rename `result' `varlist'
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -