📄 sw_8.ado
字号:
program define CheckEst, rclass /* */
capture version $HSW_call, missing: $HSW_cmd $HSW_dv $HSW_base $HSW_cur $HSW_cmdX
if _rc {
di in red "could not estimate full model"
error _rc
}
tokenize $HSW_cur
local deletes 0
local i 1
while "``i''" != "" {
local x .
capture local x = _se[``i'']
local rc = _rc
if `x'>=. | `x'==0 {
DelVar ``i''
local deletes 1
}
local i = `i'+1
}
capture assert e(sample) if $HSW_tu
if _rc {
quietly replace $HSW_tu = 0 if e(sample)==0
ret scalar IsChange = 1
exit /* we will be called again */
}
if `deletes' {
MakeList $HSW_n
quietly version $HSW_call, missing: $HSW_cmd $HSW_dv $HSW_base /*
*/ $HSW_cur $HSW_cmdX
}
ret scalar IsChange = 0
end
program define BI
* quietly version $HSW_call: $HSW_cmd
global `1' = e(ll)
global `2' = e(df_m)
end
program define ADO
global `1' $S_E_ll
global `2' $S_E_mdf
end
program define tswap /* j k */
local hold "${HSW_`1'}"
global HSW_`1' `"${HSW_`2'}"'
global HSW_`2' `"`hold'"'
end
/*
MakeList #
set HSW_cur, HSW_k to reflect first # terms
*/
program define MakeList /* k */
global HSW_k `1'
global HSW_cur
local i 1
while `i' <= $HSW_k {
global HSW_cur "$HSW_cur ${HSW_`i'}"
local i = `i' + 1
}
end
/*
SetCmd <cmdname>
Preprocessor, calls <cmd>_sw to set command macros.
See notes at eof
*/
program define SetCmd /* cmdname */
global HSW_Pcmd
global HSW_cons
global HSW_op1 "Level(integer $S_level)"
/* built-in abbreviations */
if "`1'"=="reg" {
local 1 "regress"
}
/* set */
local cmd = substr("`1'",1,5)+"_sw"
capture `cmd'
if _rc {
local rc = _rc
capture confirm var `1'
if _rc == 0 {
di in red "syntax is sw EST-COMMAND termlist ..."
exit 198
}
if `rc'==199 {
di in red "estimation command `1' not supported by sw"
exit 199
}
di in red "error in `1' sw-interface `cmd':"
error `rc'
}
if `"$HSW_res"'=="" {
global HSW_res BI
}
end
program define regre_sw
global HSW_cmd "regress"
global HSW_typ "reg"
global HSW_op0 "CLuster(string) HC2 HC3 Robust"
global HSW_op1 "$HSW_op1 Beta"
end
program define fit_sw
global HSW_cmd "regress"
global HSW_Pcmd "fit"
global HSW_typ "reg"
end
program define logit_sw
global HSW_cmd "logit"
global HSW_op0 "CLuster(string) Robust"
global HSW_op1 "$HSW_op1 OR"
global HSW_typ "ml"
global HSW_cons "chkcons"
end
program define logis_sw
global HSW_cmd "logit"
global HSW_Pcmd "logistic"
global HSW_op0 "CLuster(string) Robust"
global HSW_typ "ml"
global HSW_cons "chkcons"
end
program define probi_sw
global HSW_cmd "probit"
global HSW_op0 "CLuster(string) Robust"
global HSW_typ "ml"
global HSW_cons "chkcons"
end
program define DelTerm /* i */
local i `1'
local j `i'
while `j' < $HSW_n {
local k = `j'+1
global HSW_`j' "${HSW_`k'}"
local j = `j'+1
}
global HSW_`j'
global HSW_n = $HSW_n - 1
end
program define DelVar /* varname */
local vn "`1'"
di in blu "(`vn' dropped due to estimability)"
local i 1
while `i' <= $HSW_n {
tokenize ${HSW_`i'}
local j 1
while "``j''" != "" {
if "``j''"=="`vn'" {
local `j' " "
local n : word count `*'
if _n==0 {
DelTerm `i'
exit
}
tokenize `*'
global HSW_`i' "`*'"
exit
}
local j=`j'+1
}
local i=`i'+1
}
di in red "sw failure: could not find variable `vn'"
exit 9498
end
program define chkcons
capture local x = _se[_cons]
if _rc {
local x .
}
if `x'==0 | `x'>=. {
di in red "could not estimate _cons"
exit 498
}
end
/*
CmdOpts [, <remaining options>]
Converts predefined option masks $HSW_op0 and $HSW_op1
to actual option lists.
*/
program define CmdOpts /*, <remaining options> */
syntax [, $HSW_op0 $HSW_op1 * ]
if `"`options'"' != "" {
di in red `"`options' not allowed"'
exit 198
}
local h0 `"$HSW_op0"'
local h1 `"$HSW_op1"'
syntax [, `h1' *]
global HSW_op0 `"`options'"'
syntax [, `h0' *]
global HSW_op1 `"`options'"'
end
/*
AddT
possibly add another term and leave it defined as current model.
works for both ml and reg models.
returns HSW_AddT==1 if term added, else HSW_AddT==0.
*/
program define AddT /* first */
local first `1'
if $HSW_k == $HSW_n {
global HSW_AddT 0
exit
}
if "$HSW_typ" == "ml" {
$HSW_res HSW_ll0 HSW_d0
}
estimates hold HSW_last
local k = $HSW_k + 1
local p1 2 /* find min sig */
local kept 0
while `k' <= cond($HSW_hier, $HSW_k+1, $HSW_n) {
quietly version $HSW_call, missing: $HSW_cmd $HSW_dv $HSW_base /*
*/ $HSW_cur ${HSW_`k'} $HSW_cmdX
if "$HSW_typ" == "ml" {
$HSW_res HSW_ll1 HSW_d1
local p = chiprob($HSW_d1-$HSW_d0, /*
*/ -2*($HSW_ll0-$HSW_ll1))
}
else {
quietly test ${HSW_`k'}
local p = cond(r(df_r)>=., /*
*/ chiprob(r(df),r(chi2)), /*
*/ fprob(r(df),r(df_r),r(F)) )
}
if `p'< `p1' {
local k1 `k'
local p1 `p'
if `p' < $HSW_Pe {
local kept 1
capture estimates drop HSW_last
capture estimates hold HSW_last
}
}
local k = `k'+1
}
estimates unhold HSW_last
if `kept' {
global HSW_cur "$HSW_cur ${HSW_`k1'}"
di in gr "p = " in ye %5.4f `p1' _c
di in gr " < " %5.4f $HSW_Pe " adding " in ye "${HSW_`k1'}"
global HSW_k = $HSW_k + 1
tswap $HSW_k `k1'
global HSW_AddT 1
}
else {
if $HSW_hier {
di in gr "p = " in ye %5.4f `p1' in gr /*
*/ " >= " %5.4f $HSW_Pe " testing " /*
*/ in ye "${HSW_`k1'}"
}
else if `first' {
di in gr "p >=" %5.4f $HSW_Pe _col(23) /*
*/ "for all terms in model"
}
global HSW_AddT 0
}
end
program define RmTreg
local first `1'
if $HSW_k == 0 {
global HSW_RmT 0
exit
}
local k = cond($HSW_hier,$HSW_k,1)
local k1 .
local p1 -1
while `k' <= $HSW_k {
quietly test ${HSW_`k'}
local p = cond(r(df_r)>=., /*
*/ chiprob(r(df),r(chi2)), /*
*/ fprob(r(df),r(df_r),r(F)) )
if `p' > `p1' {
local k1 `k'
local p1 `p'
}
local k = `k' + 1
}
if `p1' < $HSW_Pr {
if $HSW_hier {
di in gr "p = " in ye %5.4f `p1' _c
di in gr " < " %5.4f $HSW_Pr " keeping " in ye /*
*/ "${HSW_`k1'}"
}
else if `first' {
di in gr "p < " %5.4f $HSW_Pr _col(23) /*
*/ "for all terms in model"
}
global HSW_RmT 0
exit
}
di in gr "p = " in ye %5.4f `p1' _c
di in gr " >= " %5.4f $HSW_Pr " removing " in ye "${HSW_`k1'}"
tswap `k1' $HSW_k
global HSW_k = $HSW_k - 1
MakeList $HSW_k
quietly version $HSW_call, missing: $HSW_cmd $HSW_dv $HSW_base $HSW_cur $HSW_cmdX
global HSW_RmT 1
end
program define RmTml
local first `1'
if $HSW_k == 0 {
global HSW_RmT 0
exit
}
$HSW_res HSW_ll1 HSW_d1
local oldcur "$HSW_cur"
estimates hold HSW_last
local k = cond($HSW_hier, $HSW_k, 1)
local k1 .
local p1 -1
local kept 0
local truk $HSW_k
local modk = $HSW_k - 1
while `k' <= `truk' {
tswap `k' `truk'
MakeList `modk'
quietly version $HSW_call, missing: $HSW_cmd $HSW_dv $HSW_base /*
*/ $HSW_cur $HSW_cmdX
$HSW_res HSW_ll0 HSW_d0
local p = chiprob($HSW_d1-$HSW_d0, -2*($HSW_ll0-$HSW_ll1))
if `p' > `p1' {
local p1 `p'
local k1 `k'
if `p' >= $HSW_Pr {
capture estimates drop HSW_last
estimates hold HSW_last
local oldcur "$HSW_cur"
local kept 1
}
}
tswap `k' `truk'
local k = `k'+1
}
global HSW_k `truk'
if `kept' {
di in gr "p = " in ye %5.4f `p1' _c
di in gr " >= " %5.4f $HSW_Pr " removing " in ye "${HSW_`k1'}"
tswap `k1' $HSW_k
global HSW_k = $HSW_k - 1
global HSW_RmT 1
}
else {
if $HSW_hier {
di in gr "p = " in ye %5.4f `p1' _c
di in gr " < " %5.4f $HSW_Pr " keeping " in ye /*
*/ "${HSW_`k1'}"
}
else if `first' {
di in gr "p < " %5.4f $HSW_Pr _col(23) /*
*/ "for all terms in model"
}
global HSW_RmT 0
}
estimates unhold HSW_last
global HSW_cur "`oldcur'"
end
exit
Command macros set by <cmd>_sw:
global macro contents
-----------------------------------------------------------------
HSW_cmd command to perform estimation
HSW_Pcmd postestimation command (optional)
HSW_typ "reg" | "ml"
"reg" means always use -test- to test terms
"ml" means use -test- or likelihood-ratio test
HSW_res to be set by version<6 programs only!
to be set only if "$HSW_typ"=="ml".
If not set, e(ll) and e(df_m) are assumed to be the
log-likelihood value and model dof. Otherwise:
name of program to unload ll and dof. May use
"BI": ll in e(ll) and dof in e(df_m)
"ADO": ll in $S_E_ll and dof in $S_E_mdf
any other program: however you write it
HSW_cons "chkcons" | ""
check that _se[_cons] defined, refuse if not.
HSW_op0 option mask for estimation-time $HSW_cmd
default: ""
HSW_op1 option mask for reply $HSW_cmd
default: "Level(integer $S_level)"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -