📄 _mprobitestimator.ado
字号:
ltol(0.0001) tol(0.001) base(`base') `copt'
matrix `b' = e(b)
}
else {
/* must generate clogit coefficient names in case it drops variables due */
/* to collinearity */
foreach attrib of local attribs {
local initcf `"`initcf' `varlist':`attrib'"'
}
if `const' | `nccf'>0 {
/* TODO: */
assert `base' < .
forvalues i=1/`nalt' {
local ai = `altlevels'[`i',1]
if (`ai'==`base') continue
tempvar a`i'
gen byte `a`i'' = (`alternatives'==`ai')
if `nccf' > 0 {
foreach cv of local casevars {
tempvar `cv'`ai'
gen double ``cv'`ai'' = `a`i''*`cv'
local csvars `"`csvars' ``cv'`ai''"'
local initcf `"`initcf' `varlist':``cv'`ai''"'
}
}
if `const' {
local csvars `"`csvars' `a`i''"'
local initcf `"`initcf' `varlist':`a`i''"'
}
}
}
if `bconstr' > 0 {
matrix colnames `C' = `initcf'
local copt constraints(`C')
}
qui clogit `varlist' `attribs' `csvars' [`weight'`exp'], group(`case') ///
ltol(0.0001) tol(0.001) nrtol(0.01) `copt'
matrix `b' = e(b)
}
local ncoef : word count `cfnms'
if colsof(`b') < `ncoef' {
di in smcl "{p 0 7}Note: when computing the initial estimates one " ///
"or more variables were dropped due to collinearity{p_end}"
matrix `from' = J(1,`ncoef',0)
local ndrop = 0
forvalues i=1/`ncoef' {
local icf : word `i' of `initcf'
local k = colnumb(`b',`"`icf'"')
if (`k'<.) matrix `from'[1,`i'] = `b'[1,`k']
}
}
else matrix `from' = `b'
if (`nvcf' > 0) matrix `from' = (`from',J(1,`nvcf',0))
matrix colnames `from' = `cfnms' `vcfnms'
return local thetas `"`thetas'"'
return matrix from = `from'
return local altmodel `"`altmodel'"'
return local casemodels `"`casemodels'"'
return local caseeqs `"`caseeqs'"'
return local baselab `"`baselab'"'
return local ncasecf = `nccf'+`const'
if (`return(ncasecf)'>0) return matrix caselevels = `caselvls'
return local const = `const'
return local rcfnms `"`cfnms'"'
return local vconstr = `vconstr'
end
program MatrixName, rclass
args matspec type
local 0 `matspec'
cap qui syntax namelist(min=1 max=2) [, copy]
gettoken spec name : namelist
if substr("`spec'",1,3) == "pat" {
return local matspec pattern
}
else if substr("`spec'",1,3) == "fix" {
return local matspec fixed
}
else if "`type'" == "correlation" {
di as error "{p}correlation() must specify either independent, " ///
"exchangeable, unstructured (default), or a matrix " ///
"specifying the correlation pattern or fixed parameters{p_end}"
exit 198
}
else {
di as error "{p}stddev() must specify either heteroscedastic, " ///
"homoscedastic, or a matrix specifying the standard deviation " ///
"pattern or fixed parameters{p_end}"
exit 198
}
if "`name'" == "" {
di as error "{p}a matrix name must follow the matrix specification " ///
"`spec' in `type'(){p_end}"
exit 198
}
if _rc != 0 {
di as error "syntax error specifying `type'()"
exit _rc
}
cap qui confirm matrix `name'
if _rc != 0 {
di as error "matrix `name' specified in `type'() does not exist"
exit 111
}
return local name `name'
return local copy = ("`copy'" != "")
end
program VParStripe, rclass
args cholesky
local nalt = rowsof($MPROBIT_ALTLEVELS)
local nvcf = 0
local dl $MPROBIT_DELIMIT
if $MPROBIT_FULLCOV {
local alt : rownames $MPROBIT_CORSTRUCT
if `cholesky' {
local rii lntheta
local rij theta
}
else {
local rii lnsigma
local rij atanhr
}
forvalues j=1/`nalt' {
local cj = $MPROBIT_CORSTRUCT[`j',`j']
if `cj' < . {
local cj : word `j' of `alt'
local thetas `"`thetas' /`rii'`cj'`dl'`cj'"'
local cfnms `"`cfnms' `rii'`cj'`dl'`cj':_cons"'
local `++nvcf'
}
else {
local cj : word `j' of `alt'
}
local j1 = `j'+1
forvalues i=`j1'/`nalt' {
local ci = $MPROBIT_CORSTRUCT[`i',`j']
if `ci' < . {
local ci : word `i' of `alt'
if `ci' > `cj' {
local thetas `"`thetas' /`rij'`ci'`dl'`cj'"'
local cfnms `"`cfnms' `rij'`ci'`dl'`cj':_cons"'
}
else {
local thetas `"`thetas' /`rij'`cj'`dl'`ci'"'
local cfnms `"`cfnms' `rij'`cj'`dl'`ci':_cons"'
}
local `++nvcf'
}
}
}
}
else {
forvalues j=1/$MPROBIT_NVAR {
local thetas `"`thetas' /lnsigma`j'"'
local cfnms `"`cfnms' lnsigma`j':_cons"'
}
forvalues i=1/$MPROBIT_NCOR {
local thetas1 `"`thetas1' /atanhr`i'"'
local cfnms1 `"`cfnms1' atanhr`i':_cons"'
}
local nvcf = $MPROBIT_NVAR+$MPROBIT_NCOR
if `"`thetas1'"' != "" {
local thetas `"`thetas' `thetas1'"'
local cfnms `"`cfnms' `cfnms1'"'
}
}
return local nvcf `nvcf'
return local thetas `"`thetas'"'
return local cfnms `"`cfnms'"'
end
program Reparameterize, rclass
args b cholesky cfnms choices T
tempname R
local nalt = rowsof($MPROBIT_ALTLEVELS)
matrix `R' = I(`nalt')
if `cholesky' {
local D `R'
local rii lntheta
local rij theta
}
else {
tempname D
matrix `D' = I(`nalt')
local rii lnsigma
local rij atanhr
}
/* elements of MPROBIT_ALTLEVELS matrix is in the same order as the row */
/* stripe of MPROBIT_CORSTRUCT */
/* cholesky factor of covariances */
local dl $MPROBIT_DELIMIT
local rc = 0
forvalues j=1/`nalt' {
local cj = $MPROBIT_CORSTRUCT[`j',`j']
if `cj' < . {
local cj = $MPROBIT_ALTLEVELS[`j',1]
local k = colnumb("`b'","`rii'`cj'`dl'`cj':")
if `k' >= . {
di as error "could not find coefficient associated " ///
"with parameter `rii'`cj'`dl'`cj':_cons"
local rc = 497
continue, break
}
matrix `D'[`j',`j'] = exp(`b'[1,`k'])
}
else {
local cj = $MPROBIT_ALTLEVELS[`j',1]
}
local j1 = `j'+1
forvalues i=`j1'/`nalt' {
local ci = $MPROBIT_CORSTRUCT[`i',`j']
if (`ci' >=.) continue
local ci = $MPROBIT_ALTLEVELS[`i',1]
if (`ci'>`cj') local cij `ci'`dl'`cj'
else local cij `cj'`dl'`ci'
local k = colnumb("`b'","`rij'`cij':")
if `k' >= . {
local rc = 497
di as error "could not find coefficient associated " ///
"with parameter `rij'`cij':_cons"
continue, break
}
if `cholesky' {
matrix `R'[`i',`j'] = `b'[1,`k']
}
else {
matrix `R'[`i',`j'] = tanh(`b'[1,`k'])
matrix `R'[`j',`i'] = `R'[`i',`j']
}
}
if (`rc'>0) continue, break
}
if (`rc'>0) exit `rc'
if `cholesky' {
matrix `R' = `R'*`R''
local nreorder = 1
local j = 0
while `++j'<`nalt' & `nreorder' {
local nreorder = (`choices'[`j',1] == $MPROBIT_ALTLEVELS[`j',1])
}
if `nreorder' == 0 {
/* put variance-covariances back into the original alternative */
/* levels order before permuting for base and scale levels */
local names : rownames $MPROBIT_CORSTRUCT
matrix rownames `R' = `names'
ChoiceOrderC "$MPROBIT_CORSTRUCT" "`choices'" "internal" 1
ChoiceOrderC "$MPROBIT_CORFIXED" "`choices'" "internal" 1
ChoiceOrderC "`R'" "`choices'" "internal" 1
global MPROBIT_ALTLEVELS `choices'
GenTmat "$MPROBIT_NVAR" "$MPROBIT_NCOR" "$MPROBIT_CORSTRUCT" 1
matrix $MPROBIT_TMAT = r(T)
local kv = 0
local kc = 0
}
local rii lnsigma
local rij atanhr
}
else {
cap matrix `R' = cholesky(`D'*`R'*`D')
if _rc != 0 {
di as error "initial estimates generated a singular " ///
"variance-covariance matrix"
exit _rc
}
local rii lntheta
local rij theta
}
local k : word count `cfnms'
forvalues j=1/`nalt' {
local cj = $MPROBIT_CORSTRUCT[`j',`j']
if `cj' < . {
local cj = $MPROBIT_ALTLEVELS[`j',1]
if `cholesky' {
matrix `b'[1,`++k'] = log(sqrt(`R'[`j',`j']))
if `nreorder' == 0 {
matrix $MPROBIT_CORSTRUCT[`j',`j'] = `++kv'
}
}
else {
local k = colnumb("`b'","lnsigma`cj'`dl'`cj':")
matrix `b'[1,`k'] = log(`R'[`j',`j'])
}
local cfnms `"`cfnms' `rii'`cj'`dl'`cj':_cons"'
local thetas `"`thetas' /`rii'`cj'`dl'`cj'"'
}
else {
local cj = $MPROBIT_ALTLEVELS[`j',1]
}
local j1 = `j'+1
forvalues i=`j1'/`nalt' {
local ci = $MPROBIT_CORSTRUCT[`i',`j']
if (`ci' >=.) continue
local ci = $MPROBIT_ALTLEVELS[`i',1]
if `cholesky' {
matrix `b'[1,`++k'] = atanh(`R'[`i',`j']/sqrt(`R'[`i',`i']*`R'[`j',`j']))
if `nreorder' == 0 {
matrix $MPROBIT_CORSTRUCT[`i',`j'] = `++kc'
matrix $MPROBIT_CORSTRUCT[`j',`i'] = `kc'
}
}
else {
if (`ci'>`cj') local cij `ci'`dl'`cj'
else local cij `cj'`dl'`ci'
local k = colnumb("`b'","atanhr`cij':")
matrix `b'[1,`k'] = `R'[`i',`j']
}
if `ci' > `cj' {
local cfnms `"`cfnms' `rij'`ci'`dl'`cj':_cons"'
local thetas `"`thetas' /`rij'`ci'`dl'`cj'"'
}
else {
local cfnms `"`cfnms' `rij'`cj'`dl'`ci':_cons"'
local thetas `"`thetas' /`rij'`cj'`dl'`ci'"'
}
}
}
matrix colnames `b' = `cfnms'
return local thetas `"`thetas'"'
end
program ChoiceOrderC, rclass
args mat mchoices name setstripe
local nchoice = rowsof(`mchoices')
tempname mat1 ip
matrix `mat1' = J(`nchoice',`nchoice',.)
matrix `ip' = J(`nchoice',1,.)
local choices : rownames `mat'
forvalues i=1/`nchoice' {
local choi : word `i' of `choices'
if `setstripe' {
local ci = `mchoices'[`i',1]
local ochoices `"`ochoices' `ci'"'
}
local j = 0
local found = 0
while `found'==0 & `j'<`nchoice' {
cap local found = (`choi' == `mchoices'[`++j',1])
if _rc > 0 {
/* `choi' is not numeric */
di as error "{p}row stripe of matrix `name' must identify " ///
"the alternatives, as numeric values, associated with " ///
"each row of the matrix, or use the copy option{p_end}"
exit 113
}
}
if `found' == 0 {
di as error "alternative `choi' identified in the row " ///
"stripe of `name' is not one of the alternatives"
exit 112
}
local k = 0
local found = 0
while `found'==0 & `++k'<`i' {
local choj : word `k' of `choices'
local found = (`choi' == `choj')
}
if `found' {
di as error "alternative `choi' is identified more than " ///
"once in the row stripe of `name'"
exit 112
}
matrix `ip'[`i',1] = `j'
forvalues k=1/`nchoice' {
matrix `mat1'[`j',`k'] = `mat'[`i',`k']
}
}
matrix `mat' = `mat1'
forvalues i=1/`nchoice' {
local k = `ip'[`i',1]
forvalues j=1/`nchoice' {
matrix `mat1'[`j',`k'] = `mat'[`j',`i']
}
}
matrix `mat' = `mat1'
if `setstripe' {
matrix rownames `mat' = `ochoices'
matrix colnames `mat' = `ochoices'
}
end
program CopyVarStruct, rclass
args corstr corfix varstr varmat vartype varcopy base fixed
local choices : rownames `corstr'
local nchoice : word count `choices'
if (rowsof(`varstr')==`nchoice' & colsof(`varstr')==1) matrix `varstr' = `varstr''
if rowsof(`varstr')!=1 | colsof(`varstr')!=`nchoice' {
di as error "matrix specified in stddev() must be 1 by `nchoice'"
exit 198
}
if (`varcopy') matrix colnames `varstr' = `choices'
local vchoices : colnames `varstr'
local nvar = 0
if "`vartype'" == "pattern" {
tempname v
matrix `v' = J(`nchoice',1,0)
}
forvalues i=1/`nchoice' {
local choi : word `i' of `vchoices'
local found = 0
local j = 0
while `j'<`nchoice' & `found'==0 {
local choj : word `++j' of `choices'
cap local found = (`choi'==`choj')
if _rc > 0 {
di as error "{p}column stripe of matrix `varmat' must " ///
"identify the alternatives, as numeric values, associated " ///
"with each element of the matrix, or use the copy option{p_end}"
exit 113
}
}
if `found' == 0 {
di as error "{p}alternative `choi' identified in the column " ///
"stripe of `varmat' is not one of the alternatives{p_end}"
exit 112
}
local k = 0
local found = 0
while `found'==0 & `++k'<`i' {
local choj : word `k' of `vchoices'
local found = (`choi' == `choj')
}
if `found' {
di as error "alternative `choi' is identified more than " ///
"once in the row stripe of `varmat'"
exit 112
}
if "`vartype'" == "pattern" {
if (`varstr'[1,`i'] == 0) matrix `varstr'[1,`i'] = .
local k = `varstr'[1,`i']
matrix `corstr'[`j',`j'] = `k'
if `k' >= . {
/* fixed to 1 */
if ("`base'"=="" & "`fixed'"!="`choi'") local base = `choi'
else if ("`fixed'"=="" & "`base'"!="`choi'") local fixed = `choi'
matrix `corfix'[`j',`j'] = 1
}
else {
if `k'<1 | `k'>`nchoice' {
di as error "{p}variance parameters identified in " ///
"`varmat' must be consecutive starting from 1 " ///
"and cannot exceed `nchoice'{p_end}"
error 417
}
matrix `v'[`k',1] = `v'[`k',1] + 1
if (`k' > `nvar') local nvar = `k'
/* free parameter */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -