📄 bootstrap.ado
字号:
*! version 4.2.13 08apr2005
program bootstrap, eclass
version 9, missing
local version : di "version " string(_caller()) ", missing:"
// Stata 8 syntax
capture _on_colon_parse `0'
if c(rc) | `"`s(after)'"' == "" {
gettoken old : 0 , qed(qed)
if `qed' {
`version' bootstrap_8 `0'
exit
}
else if "`old'" != "" {
capture which `old'
if !c(rc) {
`version' bootstrap_8 `0'
exit
}
capture program list `old'
if !c(rc) {
`version' bootstrap_8 `0'
exit
}
}
// replay output
capture syntax [, * ]
if !c(rc) {
if _caller() < 9 {
`version' bootstrap_8 `0'
exit
}
if "`e(prefix)'" != "bootstrap" {
error 301
}
Display `0'
exit
}
else {
`version' bootstrap_8 `0'
exit
}
}
preserve
`version' BootStrap `0'
if `"`e(leave)'"' != "" {
quietly restore, not
}
end
program BootStrap, eclass
version 9
local version : di "version " string(_caller()) ":"
// <my_stuff> : <command>
_on_colon_parse `0'
local command `"`s(after)'"'
local 0 `"`s(before)'"'
// quick check for -force- and -nodrop- options
syntax [anything(name=exp_list equalok)] ///
[fw iw pw aw] [if] [in] [, ///
FORCE ///
noDROP ///
CLuster(passthru) ///
IDcluster(name) ///
group(varname) ///
NOECLUSTER ///
Level(passthru) ///
COEF ///
* /// other options
]
_check_eformopt, soptions eformopts(`options')
local options `"`s(options)'"'
local efopt = cond(`"`s(opt)'"'=="",`"`s(eform)'"',`"`s(opt)'"')
if "`weight'" != "" {
local wgt [`weight'`exp']
}
// parse the command and check for conflicts
`version' _prefix_command bootstrap `wgt' `if' `in' , `coef' ///
`efopt' `cluster' `level' checkvce checkcluster: `command'
if `"`r(vce)'"' != "" {
di as err "option vce() not allowed"
exit 198
}
if "`force'" == "" & `"`s(wgt)'"' != "" {
// bootstrap does not allow weights
local 0 `s(wgt)'
syntax [, NONOPTION ]
}
local version `"`s(version)'"'
local cmdname `"`s(cmdname)'"'
local cmdargs `"`s(anything)'"'
local wgt `"`s(wgt)'"'
local wtype `"`s(wtype)'"'
local wexp `"`s(wexp)'"'
local cmdopts `"`s(options)'"'
local cluster `"`s(cluster)'"'
local rest `"`s(rest)'"'
local efopt `"`s(efopt)'"'
local level `"`s(level)'"'
// command initially executed using entire dataset
local xcommand `"`s(command)'"'
if `"`efopt'"' != "" & !inlist(`"`:list retok exp_list'"', "", "_b") {
local efopt
}
local tsop 0
if "`cluster'" != "" {
capture tsset
if !c(rc) & "`r(panelvar)'" != "" {
local tsop 1
local panelvar `r(panelvar)'
local timevar `r(timevar)'
}
}
if !`tsop' {
local junk : subinstr local cmdargs "." "_", count(local hasdot)
if `hasdot' != 0 {
di as err "{p 0 0 2}" ///
"time-series operators are not allowed with bootstrap " ///
"without panels, see {help tsset##|_new:tsset}{p_end}"
exit 198
}
}
is_svysum `cmdname'
local is_sum = r(is_svysum)
if `"`cluster'"' == "" {
if `"`idcluster'"' != "" {
di as err ///
"idcluster() can only be specified with the cluster() option"
exit 198
}
}
if `"`idcluster'"' == "" {
if `"`group'"' != "" {
di as err ///
"group() can only be specified with the idcluster() option"
exit 198
}
}
if "`drop'" == "" {
local cmdif `"`s(if)'"'
local cmdin `"`s(in)'"'
}
local exclude bs bstrap bootstrap jknife jacknife jackknife statsby
if `:list cmdname in exclude' ///
| ("`force'" == "" & substr("`cmdname'",1,3) == "svy") {
di as err "`cmdname' is not supported by bootstrap"
exit 199
}
// now check the rest of the options
local 0 `", `options'"'
syntax [, ///
noDOTS ///
Reps(int 50) ///
SAving(string) ///
DOUBle /// not documented
MSE ///
SIze(string) /// bsample opts
STRata(varlist) ///
SEED(string) ///
NOIsily /// "prefix" options
TRace ///
REJECT(string asis) ///
nowarn ///
bca ///
notable /// -Display- opts
TItle(string asis) ///
noHeader ///
noLegend ///
Verbose ///
]
if "`trace'" != "" {
local noisily noisily
local traceon set trace on
local traceoff set trace off
}
if "`noisily'" != "" {
local dots nodots
}
local nodots `dots'
local dots = cond("`dots'" != "", "*", "_dots")
local noi = cond("`noisily'"=="", "*", "noisily")
// preliminary parse of <exp_list>
_prefix_explist `exp_list', stub(_bs_)
local eqlist `"`s(eqlist)'"'
local idlist `"`s(idlist)'"'
local explist `"`s(explist)'"'
local eexplist `"`s(eexplist)'"'
_prefix_note `cmdname', `nodots'
if "`noisily'" != "" {
di "bootstrap: First call to `cmdname' with data as is:" _n
di as inp `". `xcommand'"'
}
capture confirm new var `idcluster'
if "`idcluster'" != "" & !c(rc) {
quietly egen `idcluster' = group(`cluster')
}
tempvar touse
mark `touse' `cmdif' `cmdin'
// run the command using the entire dataset
preserve
_prefix_clear, e r
`traceon'
capture noisily quietly `noisily' `version' `xcommand'
`traceoff'
local rc = c(rc)
// error occurred while running on entire dataset
if `rc' {
_prefix_run_error `rc' bootstrap `cmdname'
}
// check for rejection of results from entire dataset
if `"`reject'"' != "" {
_prefix_reject bootstrap `cmdname' : `reject'
local reject `"`s(reject)'"'
}
if `"`cmdname'"' == "mlogit" {
_prefix_mlogit, `cmdopts'
local cmdopts `"`s(options)'"'
}
if "`drop'" == "" {
// check e(sample)
_prefix_check4esample bootstrap `cmdname'
local keepesample `"`s(keep)'"'
}
if "`warn'" == "" {
local diwarn `"`s(diwarn)'"'
}
// determine default <exp_list>, or generate an error message
if `"`exp_list'"' == "" {
_prefix_explist, stub(_bs_) edefault
local eqlist `"`s(eqlist)'"'
local idlist `"`s(idlist)'"'
local explist `"`s(explist)'"'
local eexplist `"`s(eexplist)'"'
}
// expand eexp's that may be in eexplist, and build a matrix of the
// computed values from all expressions
tempname b
_prefix_expand `b' `explist', ///
stub(_bs_) ///
eexp(`eexplist') ///
colna(`idlist') ///
coleq(`eqlist') ///
// blank
local k_eq `s(k_eq)'
local k_exp `s(k_exp)'
local k_eexp `s(k_eexp)'
local K = `k_exp' + `k_eexp'
local k_extra `s(k_extra)'
local names `"`s(enames)' `s(names)'"'
local eexpress `"`s(eexplist)'"'
local coleq `"`s(ecoleq)' `s(coleq)'"'
local colna `"`s(ecolna)' `s(colna)'"'
forval i = 1/`K' {
local exp`i' `"`s(exp`i')'"'
}
// setup list of missings
forvalues j = 1/`K' {
local mis `mis' (.)
if missing(`b'[1,`j']) {
di as err ///
`"'`exp`j''' evaluated to missing in full sample"'
exit 322
}
}
if "`drop'" != "" {
// command with [if] [in]
local command `"`xcommand'"'
}
else {
is_svysum `cmdname'
local is_sum = r(is_svysum)
if `is_sum' & `k_exp' == 0 & "`eexpress'" == "(_b)" {
// speed things up with the -novariance-
_prefix_checkopt NOVARiance, `cmdopts'
if `"`noisily'`s(novariance)'"' == "" {
local novar " novar"
}
}
// command without [if] [in]
local command `"`cmdname' `cmdargs' `wgt'"'
if `"`cmdopts'"' != "" {
local command `"`command', `cmdopts' `novar'`rest'"'
}
else if "`novar'" != "" {
local command `"`command', `novar'`rest'"'
}
else local command `"`command'`rest'"'
}
// check options
if `reps' < 2 {
di as err "reps() must be an integer greater than 1"
exit 198
}
if `"`saving'"'=="" {
tempfile saving
local filetmp "yes"
}
else {
_prefix_saving `saving'
local saving `"`s(filename)'"'
if "`double'" == "" {
local double `"`s(double)'"'
}
local every `"`s(every)'"'
local replace `"`s(replace)'"'
}
if `level' <= 0 | `level' >= 100 {
di as err "level() must be between 0 and 100"
exit 198
}
// set the seed
if "`seed'" != "" {
set seed `seed'
}
local seed `c(seed)'
local diopts `table' ///
`header' ///
`legend' ///
`efopt' ///
level(`level') ///
// blank
// display the resample warning
`diwarn'
// keep only the estimation sample
if `"`keepesample'"' != "" {
quietly `keepesample'
restore, not
}
else {
restore
if `"`cmdif'`cmdin'"' != "" {
quietly keep if `touse'
}
else local leave leave
}
if `"`strata'`cluster'"' != "" {
markout `touse' `strata' `cluster'
sort `touse' `strata' `cluster', stable
}
// get strata information
if `"`strata'"' != "" {
local ustrata `strata'
tempvar sflag
by `touse' `strata': gen `sflag' = _n==1
quietly replace `sflag' = sum(`sflag') if `touse'
local nstrata = `sflag'[_N]
local strataopt strata(`sflag')
local strata `sflag'
sort `touse' `strata' `cluster', stable
}
// bsample prefers call without arguments
if `"`size'"' == "_N" {
local size
}
// if clusters, count them
local obs = _N
if "`cluster'" != "" {
foreach clname of local cluster {
capture assert ! missing(`clname')
if c(rc) {
di as err ///
`"missing values in cluster variable `clname' not allowed"'
exit 459
}
}
if `"`strata'"' != "" {
local bystrata by `touse' `strata':
}
tempvar cflag
quietly by `touse' `strata' `cluster': ///
gen `cflag' = (_n==1) if `touse'
quietly count if `cflag'
// total number of clusters
local nclust = r(N)
quietly `bystrata' replace `cflag' = sum(`cflag')
// number of clusters per strata
quietly `bystrata' replace `cflag' = `cflag'[_N]
if "`size'" != "" {
capture assert `size' <= `cflag'
if c(rc) {
di as err ///
"size() must not be greater than number of clusters"
exit 498
}
}
local clustopts cluster(`cluster')
}
nobreak {
capture noisily break {
preserve
if "`eexpress'" == "(_b)" {
if `k_extra' == 0 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -