rologit.ado
来自「是一个经济学管理应用软件 很难找的 但是经济学学生又必须用到」· ADO 代码 · 共 297 行
ADO
297 行
*! version 1.2.7 09jan2005
program rologit, byable(onecall)
version 8.0
if replay() {
if "`e(cmd)'" != "rologit" {
error 301
}
if _by() {
error 190
}
Display `0'
}
else {
if _by() {
by `_byvars'`_byrc0' : Estimate `0'
}
else Estimate `0'
}
end
program Estimate, eclass sortpreserve byable(recall)
syntax varlist(min=2 numeric) [if] [in] [fw pw iw] , ///
GRoup(varname) [ TIES(str) noTEstrhs REVerse ///
INComplete(int 0) Robust CLuster(varname) noLOg TRace ///
OFFset(varname) Level(passthru) * ]
local coxopt `options'
/*
if _by() {
_byoptnotallowed score() `"`score'"'
}
*/
if "`weight'" != "" {
local wght `"[`weight'`exp']"'
if "`weight'" == "pweight" {
local robust robust
}
}
if "`ties'" != "" {
TiesOption `ties'
local ties `r(ties)'
}
quietly {
tempvar fail markg g y
tempname ng gavg gmin gmax
marksample touse
markout `touse' `group' , strok
// offset()
if "`offset'" != "" {
markout `touse' `offset'
local offopt offset(`offset')
}
// cluster()
if "`cluster'" != "" {
markout `touse' `cluster' , strok
capt bys `touse' `group' : /*
*/ assert `cluster' == `cluster'[1] if `touse'
if _rc {
if (_rc==1) {
exit 1
}
noi di as err "cluster() should be constant" /*
*/ " within group()"
exit 198
}
local robust robust
local clopt "cluster(`cluster')"
}
else if "`robust'" != "" {
// beware: cluster() is necessary for robust!
local clopt "cluster(`group')"
}
// group structure (#groups, avg size etc)
count if `touse'
if r(N) == 0 {
noi error 2000
}
bys `touse' `group' : gen int `g' = _N if _n==1
summ `g' if `touse' , meanonly
scalar `ng' = r(N)
scalar `gmin' = r(min)
scalar `gavg' = r(mean)
scalar `gmax' = r(max)
drop `g'
/*
key variables for -cox-
cox rologit
-------------------
timevar == ranking 1: shortest time <==> most preferred,
2: 2nd shortest time <==> preferred second
etc
failure == whether preference was expressed;
if no pref is expressed ("waiting time incomplete")
it is interpreted as being less attractive than all
alternatives mentioned. The associated waiting time
is set to #alternatives + 1.
strata == rologit-group()
*/
gettoken lhs rhs : varlist
gen byte `fail' = `lhs' != `incomplete' if `touse'
count if !`fail'
if r(N) > 0 {
di as txt "incomplete rankings encountered"
local incomplete 1
}
summ `lhs' if `touse' & `fail', meanonly
if "`reverse'" == "" {
// higher value of lhs == "more attractive"
// == "shorter waiting time"
gen `y'=cond(`fail',r(max)-`lhs'+1,r(max)+2) if `touse'
}
else {
// low value of lhs == "more attractive"
// == "shorter waiting time"
gen `y'=cond(`fail',`lhs'-r(min)+1,r(max)+2) if `touse'
}
// tie handling via any of Cox method's
capt bys `touse' `group' `fail' `y' : /*
*/ assert _N==1 if `touse' & `fail'
if _rc {
if (_rc==1) {
exit 1
}
local hasties yes
if "`ties'" == "none" {
noi di as err /*
*/ "ties not allowed, specify option ties()"
exit 198
}
else if "`ties'" == "" {
local ties = cond("`robust'"=="", /*
*/ "exactm", "efron")
}
if "`ties'" == "exactm" {
if "`robust'" != "" {
noi di as err "robust not allowed "/*
*/"with exactm method for handling ties"
exit 198
}
if "`weight'" == "pweight" {
noi di as err "pweights not allowed with exactm"
exit 404
}
}
local coxties `ties'
}
// warning message if data are not 1,2,.. coded
// only checked if there are no ties
if "`hasties'" == "" {
capt by `touse' `group' `fail' (`y') : /*
*/ assert `y' == _n if `touse' & `fail'
if _rc {
noi di as txt "(preferences are not coded " /*
*/ "as sequential integers)"
}
}
// test that rhs variables vary between alternatives
if "`testrhs'" == "" {
foreach v of local rhs {
capt bys `touse' `group' (`v') : /*
*/ assert `v'==`v'[1] if `touse'
if (_rc==1) {
exit 1
}
if !_rc {
local cnsvar `cnsvar' `v'
}
else local newrhs `newrhs' `v'
}
if "`cnsvar'" != "" {
noi di as txt "`cnsvar' omitted due to " /*
*/ "no within-`group' variance"
local rhs `newrhs'
}
}
} /* quietly */
// estimate the exploded logit model via cox,
// displaying the log/trace, but no header or table
cox `y' `rhs' if `touse' `wght' , dead(`fail') strata(`group') ///
`robust' `clopt' `coxties' `log' `trace' `offopt' ///
`coxopt' nocoef noheader
// e() are inherited from -cox-
_repost "rologit" "`lhs'" // no longer a Cox estimator
ereturn local method
ereturn local strata
ereturn local old_cmd
ereturn scalar N_g = `ng' // N of groups
ereturn scalar g_min = `gmin' // min #obs within subject
ereturn scalar g_avg = `gavg' // mean #obs within subject
ereturn scalar g_max = `gmax' // max #obs within subject
ereturn scalar code_inc = `incomplete' // code for incomplete prefs
ereturn local reverse `reverse' // reverse ordering
ereturn local depvar `lhs'
ereturn local group `group'
ereturn local predict rologit_p
ereturn local ties `ties'
ereturn local title "Rank-ordered logistic regression"
ereturn local cmd rologit
Display, `level'
end
program Display
syntax [, Level(cilevel)]
local crtype = upper(substr(`"`e(crittype)'"',1,1)) + /*
*/ substr(`"`e(crittype)'"',2,.)
di _n as txt "`e(title)'" ///
_col(49) as txt "Number of obs =" as res %10.0f e(N)
di as txt "Group variable: " as res abbrev("`e(group)'",12) ///
_col(49) as txt "Number of groups =" as res %10.0f e(N_g) _n
if "`e(ties)'" != "none" & "`e(ties)'" != "" {
di as txt "ties handled via the `e(ties)' method" _c
}
else di as txt "no ties in data" _c
di _col(49) as txt "Obs per group: min =" as res %10.0f e(g_min)
di _col(49) as txt " avg =" as res %10.2f e(g_avg)
di _col(49) as txt " max =" as res %10.0f e(g_max) _n
di _col(49) as txt "`e(chi2type)' chi2(" as res "`e(df_m)'" as txt ")" ///
_col(68) "=" as res %10.2f e(chi2) _n ///
as txt "`crtype' = " as res %9.0g e(ll) ///
_col(49) as txt "Prob > chi2 =" ///
as res %10.4f chi2tail(e(df_m),e(chi2)) _n
ereturn display, level(`level')
end
program TiesOption, rclass
local narg : word count `0'
if `narg' != 1 {
di as err "invalid ties(): exactly one value expected"
exit 198
}
local 0 `", `0'"'
syntax , [EXactm exactp EFRon BREslow NONE]
if "`exactp'" != "" {
di as err "tie-handling method exactp is not allowed"
exit 198
}
return local ties `exactm' `efron' `breslow' `none'
end
exit
HISTORY
1.2.1 change in display formatting
1.2.0 Support for all tie-handling methods in Cox
rologit is no longer set up as a wrapper around cox--though it
still uses cox
predict command
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?