📄 sts.ado
字号:
per(real 1.0) ///
YLOg ///
ATRisk ///
Kernel(string) ///
width(string) ///
CIHazard ///
* ///
]
local gropts `"`options'"'
_get_gropts , graphopts(`options') ///
grbyable ///
getallowed( ///
CIOPts ///
ATRISKOPts ///
LOSTOPts ///
plot ///
addplot ///
YSCale ///
) ///
getbyallowed(LEGend TItle)
local by `s(varlist)'
local byttl `"`s(by_title)'"'
local bylgnd `"`s(by_legend)'"'
local byopts `"`s(byopts)'"'
local options `"`s(graphopts)'"'
local ciopts `"`s(ciopts)'"'
local atopts `"`s(atriskopts)'"'
local lstopts `"`s(lostopts)'"'
local plot `"`s(plot)'"'
local addplot `"`s(addplot)'"'
local yscale `"`s(yscale)'"'
_check4gropts ciopts, opt(`ciopts')
if `"`atopts'"' != "" {
local atrisk atrisk
}
if `"`lstopts'"' != "" {
local lost lost
}
ChkYScale4Log , `ylog' `yscale'
local ylog `s(log)'
if "`s(options)'" != "" {
local options `"yscale(`s(options)') `options'"'
local yscale
}
if "`hazard'"!="" {
if `per' != 1.0 {
di in red "option hazard not allowed with per()"
exit 198
}
foreach x in na cna enter failure gwood ///
lost atrisk {
ForbidOpt ``x''
}
if `"`censored'"'!="" {
di in red "censored() not allowed with hazard plots"
exit 198
}
local origin noorigin
local conopt connect(l ...)
}
else {
local conopt connect(J ...)
local ylabopt ylabel(\`ylab', grid)
}
if "`cihazard'"!="" & "`hazard'"=="" {
di in red "cihazard may only be specified with hazard"
exit 198
}
local w : char _dta[st_w]
if "`enter'"!="" & "`lost'"=="" {
local lost="lost"
}
if "`cna'"!="" {
local na="na"
}
if `level'<10 | `level'>99 {
di in red "level() invalid"
exit 198
}
if `"`kernel'"' != "" {
if "`hazard'" == "" {
di in red "kernel() only allowed with hazard"
exit 198
}
}
if `"`width'"' != "" {
if "`hazard'" == "" {
di in red "width() only allowed with hazard"
exit 198
}
}
if "`na'"!="" & "`adjustf'"!="" {
di in red "cannot specify adjustfor() with na or cna options"
exit 198
}
if "`cihazard'"!="" & "`adjustf'"!="" {
di in red "cannot specify adjustfor() with cihazard"
exit 198
}
if "`na'"!="" & "`failure'"!="" {
di in red "failure invalid with na or cna options"
exit 198
}
if "`na'"!="" & "`gwood'"!="" {
di in red "gwood invalid with na or cna options"
exit 198
}
if "`na'"!="" & `per' != 1.0 {
di in red "option na not allowed with per()"
exit 198
}
/* if "`failure'"!="" & `per' != 1.0 {
di in red "option failure not allowed with per()"
exit 198
}
*/
if "`gwood'"!="" {
if "`_dta[st_wt]'"=="pweight" {
di in red "option gwood not allowed with pweighted data"
exit 198
}
if `per' != 1.0 {
di in red "option gwood not allowed with per()"
exit 198
}
}
if "`cna'"!="" {
if `per' != 1.0 {
di in red "option cna not allowed with per()"
exit 198
}
if "`_dta[st_wt]'"=="pweight" {
di in red "option cna not allowed with pweighted data"
exit 198
}
}
if `per' != 1.0 {
if "`atrisk'"!="" {
di in red "option atrisk not allowed with per()"
exit 198
}
if "`lost'"!="" {
di in red "options lost and enter not allowed with per()"
exit 198
}
}
if "`ylog'"!="" & `per' != 1.0 {
di in red "option ylog not allowed with per()"
exit 198
}
if "`na'"!="" {
local origin "noorigin"
local ttlpos 5
}
else local ttlpos 1
ByStAdj "`by'" "`strata'" "`adjustf'"
local sb "`s(sb)'"
if "`sb'" != "" {
local n : word count `sb'
if `n'>1 & ///
("`separat'"!="" | "`gwood'"!="" | ///
"`hazard'"!="" | "`cna'"!="" | "`cihazard'"!="" ) {
di in red "may not specify " _c
if "`separat'"!="" {
di in red "separate" _c
}
else if "`gwood'"!="" {
di in red "gwood" _c
}
else if "`hazard'"!="" {
di in red "hazard" _c
}
else di in red "cna" _c
di in red " with more than one by/strata variable;"
di in red /*
*/ "use " _quote "egen ... = group(`strata')" _quote /*
*/ " to make a single variable"
exit 198
}
}
if "`censored'"!="" & ("`lost'"!="" | "`enter'"!="" | "`atrisk'"!="") {
di in red /*
*/ "censored() not possible with lost, enter, or atrisk"
exit 198
}
if "`censored'"~="" {
local l = length("`censored'")
if substr("numbered",1,max(1,`l')) == "`censored'" {
local censt= "numbered"
}
else if substr("single",1,max(1,`l')) == "`censored'" {
local censt= "single"
}
else if substr("multiple",1,max(1,`l')) == "`censored'" {
local censt= "multiple"
}
else {
di in red "invalid option censored(`censored')"
exit 198
}
}
if "`enter'"!="" & "`atrisk'"!="" {
di in red /*
*/ "atrisk and enter not possible at the same time"
exit 198
}
if "`adjustf'"!="" {
if "`gwood'"!="" {
di in red "gwood not possible with adjustfor()"
exit 198
}
if "`lost'"!="" | "`enter'"!="" | "`atrisk'"!="" /*
*/ | "`censored'"!="" {
di in red /*
*/ "atrisk, censored, lost, or enter not possible with adjustfor()"
exit 198
}
}
st_show `show'
tempvar touse mark n d cens ent h Vh s lb ub aal uba lba
st_smpl `touse' `"`if'"' "`in'" "`sb'" "`adjustf'"
preserve
quietly {
keep if `touse'
if "`adjustf'"=="" {
if "`censored'"~="" {
tempvar mycens
st_ct "`by'" -> _t `n' _d "" `ent' `mycens'
local enter="enter"
local notreal="notreal"
gen double `cens'=`mycens'
drop `mycens'
}
else {
st_ct "`by'" -> _t `n' _d `cens' `ent'
}
if "`enter'"=="" & "`atrisk'"=="" {
replace `cens' = `cens' - `ent' if _t
drop if _d==0 & `cens'==0
replace `ent' = 0
}
if "`atrisk'"!="" {
replace `ent' = `n'[_n+1] if _d
if "`lost'"=="" {
replace `cens'=0
}
}
AddSurv "`by'" _t `n' _d `level' -> /*
*/ `h' `Vh' `s' "" "" `lb' `ub' `aal' "" `uba' `lba'
}
else {
DoAdjust "`by'" "`strata'" "`adjustf'" "" "" -> `h' `s'
if "`hazard'"=="" {
KeepDead "`sb'"
}
gen `Vh' = 1 // not used with adjustfor
gen `ub' = . // not used with adjustfor
gen `lb' = . // not used with adjustfor
}
if "`hazard'" != "" {
keep if _d
sort `sb' _t
by `sb' _t: keep if _n==1
tempvar ub1 lb1
SmoothHazard _t `h' `Vh' "`kernel'" `"`width'"' `tmin' ///
`tmax' `"`sb'"' `"`ub1'"' `"`lb1'"' `level'
sort `sb' _t
qui replace `s' = `h'
qui replace `ub' = `ub1'
qui replace `lb' = `lb1'
}
label var `s' "Survivor function"
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'"
}
label var `s' "Failure function"
}
if "`hazard'"!="" {
label var `s' "Smoothed hazard function"
}
if "`na'" != "" {
replace `s'=`aal'
replace `lb'=`lba'
replace `ub'=`uba'
drop `aal' `lba' `uba'
label var `s' "Cumulative hazard"
}
cap confirm var `lb'
if !_rc {
label var `lb' `"`=strsubdp("`level'")'% CI"'
label var `ub' `"`=strsubdp("`level'")'% CI"'
}
if `tmin' != -1 {
drop if _t<`tmin'
}
if `tmax' != -1 {
drop if _t>`tmax'
}
qui count
if r(N)<1 {
di in red "no observations"
exit 2000
}
if "`lost'"=="" & "`atrisk'"=="" {
if "`adjustf'"=="" {
drop _d
}
}
else {
summ `s'
local eps = max( (r(max)-r(min))/30, .0)
tempvar dj tnext s2 s3 newt
sort `sb' _t _d
if "`sb'"!="" {
local byst "by `sb':"
}
`byst' replace _d=1 if _d==0 & (_n==1 | _n==_N)
`byst' gen long `dj' = _n if _d
`byst' replace `dj'=`dj'[_n-1] if `dj'>=.
sort `sb' `dj' _d
by `sb' `dj': replace `cens'=sum(`cens')
by `sb' `dj': replace `ent' =sum(`ent')
by `sb' `dj': keep if _n==_N
drop `dj'
sort `sb' _t
`byst' gen `tnext'=_t[_n+1]
`byst' replace `tnext'=_t if _n==_N
expand 3
sort `sb' _t
by `sb' _t: gen `newt' = cond(_n==1, _t, /*
*/ (_t+`tnext')/2)
drop `tnext'
by `sb' _t: gen `s2' = `s'+`eps' if _n==2
by `sb' _t: gen `s3' = `s'-`eps' if _n==3
by `sb' _t: replace `s'=. if _n>1
by `sb' _t: replace `cens' = 0 /*
*/ if _n==1 | _n==3
by `sb' _t: replace `ent' = 0 /*
*/ if _n==1 | _n==2
by `sb' _t: keep if _n==1 | /*
*/ (_n==2 & `cens') | (_n==3 & `ent')
local lbl : var label _t
drop _t
rename `newt' _t
label var _t `"`lbl'"'
local lbl
sort `sb' _t
sum `s2', mean
if r(N) > 0 {
label var `s2' "Number lost"
local s2l `cens'
}
else local s2
sum `s3', mean
if r(N) > 0 {
label var `s3' "Number entered"
local s3l `ent'
}
else local s3
if `"`s2'"' != "" {
local mgraph ///
(scatter `s2' _t, ///
sort ///
connect(none) ///
msymbol(none) ///
mlabel(`s2l') ///
mlabposition(0) ///
pstyle(p1) ///
\`ysca' /// yet to exist
`lstopts' ///
) ///
// blank
}
if `"`s3'"' != "" {
local mgraph ///
`mgraph' ///
(scatter `s3' _t, ///
sort ///
connect(none) ///
msymbol(none) ///
mlabel(`s3l') ///
mlabposition(0) ///
pstyle(p1) ///
\`ysca' /// yet to exist
`atopts' ///
) ///
// blank
}
}
} // quietly
if "`sb'"=="" | "`gwood'"!="" | "`cna'"!="" | "`cihazard'"!="" {
local separat "separate"
}
if "`separat'"=="" & `"`byopts'"' != "" {
local 0 , `byopts'
syntax [, ZZ_OPTION_TO_GET_ERROR_MESSAGE ]
error 198 // should not get here
}
if "`separat'"=="separate" {
local svars "`s'"
if "`gwood'" != "" | "`cna'"!="" | "`cihazard'"!="" {
local cigraph ///
(rline `lb' `ub' _t, ///
sort ///
connect(J) ///
pstyle(ci) ///
\`ysca' /// yet to exist
`ciopts' ///
) ///
// blank
}
}
else {
quietly {
tempvar grp id
by `sb': gen int `grp' = 1 if _n==1
replace `grp' = sum(`grp')
gen `id' = _n
local ng = `grp'[_N]
local i 1
local j 1
while `i' <= `ng' {
tempvar x
gen float `x'=`s'*`per' if `grp'==`i'
local svars "`svars' `x'"
GetGroupLabel `sb' if `grp'==`i', id(`id')
label var `x' `"`r(label)'"'
local i = `i' + 1
}
} // quietly
}
if "`na'" == "" & "`hazard'" == "" { // survival
if "`ylog'"!="" {
local ylab ".01 .05 .10 .25 .50 .75 1"
}
else {
local yfact= .25*`per'
local ylab "0(`yfact')`per'"
}
}
// format the first var to be graphed, this makes the yaxis lables
// look nice
if "`hazard'"=="" {
local fvar : word 1 of `svars'
format `fvar' %9.2f
}
if "`na'"!="" {
local ttl "Nelson-Aalen cumulative hazard estimate"
}
else if "`hazard'"!="" {
local ttl "Smoothed hazard estimate"
if "`cihazard'"!="" {
local cigraph ///
(rline `lb' `ub' _t, ///
sort ///
`conopt' ///
pstyle(ci) ///
\`ysca' /// yet to exist
`ciopts' ///
) ///
// blank
}
}
else if "`failure'"=="" {
if "`adjustf'"!="" {
local ttl "Survivor function"
}
else{
local ttl "Kaplan-Meier survival estimate"
}
}
else {
if "`adjustf'"!="" {
local ttl "Failure function"
}
else {
local ttl "Kaplan-Meier failure estimate"
}
}
if "`sb'"!="" {
if `per' != 1 {
local ttl `"`ttl's per `per', by `sb'"'
}
else local ttl `"`ttl's, by `sb'"'
}
else {
if `per' != 1 {
local ttl `"`ttl' per `per'"'
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -