📄 zts_5.ado
字号:
*! version 6.1.5 15sep2004
program define zts_5
version 5.0, missing
di in gr "(you are running sts from Stata version 5)"
zt_is_5
if substr("`1'",length("`1'"),1)=="," {
local 2 ", `2'"
local 1 = substr("`1'",1,length("`1'")-1)
}
local cmd "`1'"
mac shift
if "`cmd'"=="" {
local cmd "graph"
}
local l = length("`cmd'")
if substr("list",1,`l')=="`cmd'" {
List `*'
}
else if substr("graph",1,`l')=="`cmd'" {
Graph `*'
}
else if substr("generate",1,`l')=="`cmd'" {
Gen `*'
}
else if substr("test",1,`l')=="`cmd'" {
Test `*'
}
else if "`cmd'"=="if" | "`cmd'"=="in" {
Graph `*'
}
else {
di in red "unknown sts subcommand `cmd'"
exit 198
}
end
program define Test
local varlist "req ex"
local if "opt"
local in "opt"
local options "BY(string) Detail noSHow *"
local wt : char _dta[st_wt]
if "`wt'"=="pweight" {
local options "`options' Cox"
}
else local options "`options' Breslow Cox Wilcoxon"
parse "`*'"
if "`by'"!="" {
di in red "by() not allowed
exit 198
}
local by "by(`varlist')"
local n1 = ("`breslow'"!="")|("`wilcoxo'"!="")
if `n1'+("`cox'"!="")+("`logrank'"!="")>1 {
di in red "options logrank, wilcoxon, and cox are alternatives"
di in red "they may not be specified together"
exit 198
}
if `n1' {
local cmd "wilc_st"
}
else if "`cox'"!="" | "`wt'"=="pweight" {
local cmd "ctst_5"
}
else local cmd "logrank"
zt_sho_5 `show'
if "`cmd'"=="ctst_5" {
tempname oldest
capture {
capture estimate hold `oldest'
noisily `cmd' `varlist' `if' `in', `options'
}
local rc = _rc
capture estimate unhold `oldest'
exit `rc'
}
tempvar touse
zt_smp_5 `touse' "`if'" "`in'"
local t : char _dta[st_t]
local t0 : char _dta[st_t0]
local d : char _dta[st_d]
local id : char _dta[st_id]
local w : char _dta[st_w]
if "`t0'" != "" {
local t0 "t0(`t0')"
}
if "`id'" != "" {
local id "id(`id')"
}
`cmd' `t' `d' `w' if `touse', `t0' `id' `by' `options' `detail'
end
/*
gen var=thing [var=thing] ...
*/
program define Gen
parse "`*'", parse(" =,")
if "`2'" != "=" { error 198 }
while "`2'" == "=" {
confirm new var `1'
local 3 = lower("`3'")
if "`3'" == "s" {
local Surv `1'
}
else if "`3'"=="se(s)" {
NotPw "se(s)"
local Se "`1'"
local notcox "`1'=`3'"
}
else if "`3'"=="h" {
local Haz "`1'"
}
else if "`3'"=="se(lls)" {
NotPw "se(lls)"
local sllS "`1'"
local notcox "`1'=`3'"
}
else if "`3'"=="ub(s)" | "`3'"=="ub" {
NotPw "ub(s)"
local ub "`1'"
local notcox "`1'=`3'"
}
else if "`3'"=="lb(s)" | "`3'"=="lb" {
NotPw "lb(s)"
local lb "`1'"
local notcox "`1'=`3'"
}
else if "`3'"=="n" {
local Pop "`1'"
local notcox "`1'=`3'"
}
else if "`3'"=="d" {
local Die "`1'"
local notcox "`1'==`3'"
}
else {
di in red "`3' unknown function"
exit 198
}
mac shift 3
}
local if "opt"
local in "opt"
local options "Adjustfor(string) BY(string) Level(integer $S_level) noSHow STrata(string)"
parse "`*'"
ByStAdj "`by'" "`strata'" "`adjustf'"
local by "$S_1"
local strata "$S_2"
local adjustf "$S_3"
* zt_sho_5 `show'
if "`adjustf'" != "" {
if "`notcox'" != "" {
di in red "cannot calculate `notcox' with adjustfor()"
exit 198
}
qui DoAdjust "`by'" "`strata'" "`adjustf'" "`if'" "`in'" /*
*/ -> "`Haz'" "`Surv'"
if "`Surv'"!="" {
label var `Surv' "S(t+0), adjusted"
}
if "`Haz'"!="" {
label var `Haz' "h(t), adjusted"
}
exit
}
if "`Pop'"=="" { tempvar Pop }
if "`Die'"=="" { tempvar Die }
tempvar touse
zt_smp_5 `touse' "`if'" "`in'" "`by'" ""
preserve
quietly {
keep if `touse'
local t : char _dta[st_t]
qui zt_ct_5 "`by'" -> `t' `Pop' `Die'
keep if `Die'
AddSurv "`by'" `t' `Pop' `Die' `level' -> /*
*/ "`Haz'" "`Surv'" "`Se'" "`sllS'" "`lb'" "`ub'"
keep `by' `t' `Haz' `Surv' `Se' `sllS' `lb' `ub' `Pop' `Die'
tempfile one
save "`one'"
restore, preserve
sort `by' `t'
merge `by' `t' using "`one'"
keep if _merge==1 | _merge==3
drop _merge
sort `by' `t'
if "`by'" != "" {
local byp "by `by':"
}
if "`Surv'" != "" {
`byp' replace `Surv' = /*
*/ cond(_n==1,1,`Surv'[_n-1]) if `Surv'>=.
replace `Surv' = . if `touse'==0
label var `Surv' "S(t+0)"
}
if "`Se'" != "" {
`byp' replace `Se' = `Se'[_n-1] if `Se'>=.
label var `Se' "se(S) (Greenwood)"
}
if "`sllS'" != "" {
`byp' replace `sllS' = `sllS'[_n-1] if `sllS'>=.
label var `sllS' "se(-ln ln S)"
}
if "`lb'" != "" {
`byp' replace `lb' = `lb'[_n-1] if `lb'>=.
label var `lb' "S() `level'% lower bound"
}
if "`ub'" != "" {
`byp' replace `ub' = `ub'[_n-1] if `ub'>=.
label var `ub' "S() `level'% upper bound"
}
if "`Haz'" != "" {
label var `Haz' "h(t)"
}
label var `Pop' "N, entering population"
label var `Die' "d, number of failures"
}
restore, not
end
program define NotPw /* text */
local w : char _dta[st_wt]
if "`w'"=="pweight" {
di in red "`*' not possible with pweighted data"
exit 404
}
end
program define AddSurv /* by t Pop Die lvl -> Haz Surv Se sllS lb ub */
local by "`1'" /* 1 2 3 4 5 7 8 9 10 11 12 */
local t "`2'"
local N "`3'"
local D "`4'"
local lvl "`5'"
local h "`7'"
local S "`8'"
local Se "`9'"
local sllS "`10'"
local lb "`11'"
local ub "`12'"
if "`h'"=="" {
tempvar h
}
gen double `h' = cond(`N'==0,0,`D'/`N')
sort `by' `t'
if "`by'" != "" {
local byp "by `by':"
}
if "`Se'"!="" | "`lb'"!="" | "`ub'"!="" {
if "`S'"=="" { tempvar S }
if "`lb'"!="" | "`ub'"!="" {
if "`sllS'"=="" { tempvar sllS }
}
}
if "`S'" != "" {
`byp' gen double `S' = 1-`h'
`byp' replace `S' = `S'[_n-1]*(1-`h') if _n>1
}
if "`Se'" != "" {
`byp' gen double `Se' = /*
*/ `S'*sqrt(sum(`D'/(`N'*(`N'-`D')))) if `S'!=0
replace `Se' = . if `S'==1
}
if "`sllS'" != "" {
`byp' gen double `sllS' = sqrt( /*
*/ sum(`D'/(`N'*(`N'-`D'))) / (sum(ln((`N'-`D')/`N'))^2) )
}
if "`lb'"!="" | "`ub'"!="" {
local z = invnorm(1-(1-`lvl'/100)/2)
if "`lb'" != "" {
gen double `lb'=(`S')^(exp(`z'*`sllS')) if `S'!=0
}
if "`ub'"!="" {
gen double `ub'=(`S')^(exp(-`z'*`sllS')) if `S'!=0
}
}
end
program define List
local if "opt"
local in "opt"
local options "ADjustfor(string) AT(string) BY(string) Compare Enter Failure Level(integer $S_level) noSHow STrata(string)"
parse "`*'"
/* begin unload.h */
local t : char _dta[st_t]
local t0 : char _dta[st_t0]
local d : char _dta[st_d]
local id : char _dta[st_id]
local w : char _dta[st_w]
local wt : char _dta[st_wt]
/* end unload.h */
if `level'<10 | `level'>99 {
di in red "level() invalid"
exit 198
}
ByStAdj "`by'" "`strata'" "`adjustf'"
local by "$S_1"
local strata "$S_2"
local adjustf "$S_3"
local sb "$S_4"
if "`compare'" != "" {
if "`at'" == "" {
local at "10"
}
if "`sb'"=="" {
di in red "compare requires by() or strata()"
exit 198
}
}
if "`at'" != "" {
Procat `at'
local at "$S_1"
}
zt_sho_5 `show'
tempvar touse mark n d cens ent s se lb ub
zt_smp_5 `touse' "`if'" "`in'" "`sb'" "`adjustf'"
preserve
quietly {
keep if `touse'
if "`adjustf'"=="" {
zt_ct_5 "`by'" -> `t' `n' `d' `cens' `ent'
if "`enter'"=="" {
replace `cens' = `cens' - `ent'
drop if `d'==0 & `cens'==0
replace `ent' = 0
}
AddSurv "`by'" `t' `n' `d' `level' -> /*
*/ "" `s' `se' "" `lb' `ub'
}
else {
DoAdjust "`by'" "`strata'" "`adjustf'" "" "" -> "" `s'
KeepDead "`sb'"
}
if "`failure'" != "" {
replace `s'=1-`s'
if "`adjustf'"=="" {
replace `lb'=1-`lb'
replace `ub'=1-`ub'
local hold "`lb'"
local lb "`ub'"
local ub "`hold'"
}
}
}
if "`sb'"!="" {
quietly {
tempvar grp
by `sb': gen `grp'=1 if _n==1
replace `grp' = sum(`grp')
}
}
if "`compare'"!="" {
Reat `t' `at'
if "$S_1"!="" {
local at "$S_1"
}
if "`adjustf'"=="" {
drop `n' `d' `cens' `ent' `se' `lb' `ub'
}
Licomp "" "`sb'" "`grp'" `t' `s' "`at'" "`failure'" "`adjustf'"
exit
}
if "`failure'"!="" {
local ttl "Failure"
local blnk " "
local attl " Adjusted Failure Function"
}
else {
local ttl "Survivor"
local attl "Adjusted Survivor Function"
}
if "`at'"!="" {
Reat `t' `at'
if "$S_1"!="" {
local at "$S_1"
}
if "`adjustf'"=="" {
Listat "`sb'" "`grp'" `t' `n' `d' `cens' `ent' /*
*/ `s' `se' `lb' `ub' "`at'" "`level'" /*
*/ "`ttl'"
}
else Listata "`sb'" "`grp'" `t' `s' "`at'" "`ttl'" "`blnk'" "`adjustf'"
exit
}
if "`adjustf'"=="" {
if "`enter'"!="" {
local ettl "Enter"
local liste 1
}
else {
qui drop if `t'==0
local ettl " "
local liste 0
local net "Net"
}
if "`wt'"!="pweight" {
di in gr _n _col(12) "Beg." _col(26) "`net'" /*
*/ _col(41) "`ttl'" _col(55) "Std." _n /*
*/ " Time Total Fail Lost `ettl'" /*
*/ _col(41) /*
*/ "Function Error [`level'% Conf. Int.]"
local dupcnt 79
}
else {
di in gr _n _col(12) "Beg." _col(26) "`net'" /*
*/ _col(41) "`ttl'" _n /*
*/ " Time Total Fail Lost `ettl'" /*
*/ _col(41) "Function"
local dupcnt 48
}
}
else {
di in gr _n " Adjusted" /*
*/ _n " Time `blnk'`ttl' Function"
local dupcnt 27
}
di in gr _dup(`dupcnt') "-"
local i 1
while `i' <= _N {
if "`sb'" != "" {
if `grp'[`i'] != `grp'[`i'-1] {
sts_sh `grp' "`grp'[`i']" "`sb'"
di in gr "$S_3"
}
}
if "`adjustf'"=="" {
di in gr %6.0g `t'[`i'] " " in ye /*
*/ %8.0g `n'[`i'] " " /*
*/ %6.0g `d'[`i'] " " /*
*/ %6.0g `cens'[`i'] " " _c
if `liste' {
di in ye %6.0g `ent'[`i'] _c
}
else di _skip(6) _c
di in ye " " /*
*/ %11.4f `s'[`i'] " " _c
if "`wt'"!="pweight" {
di in ye /*
*/ %9.4f `se'[`i'] /* standard error */ " " /*
*/ %10.4f `lb'[`i'] /* lower cb */ " " /*
*/ %9.4f `ub'[`i'] /* upper cb */
}
else di
}
else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -