📄 bstat_8.ado
字号:
cap _pctile `x' if `touse', p(`p1', `p2')
if _rc {
local bc1 `"."'
local bc2 `"."'
}
else {
local bc1 = r(r1)
local bc2 = r(r2)
}
// bias-corrected and accelerated
if "`vai'" != "" {
if ! missing(`vai') {
scalar `zz' = `z0'-`zalpha'
local p1 = 100*normprob(`z0'+`zz'/(1-`vai'*`zz'))
scalar `zz' = `z0'+`zalpha'
local p2 = 100*normprob(`z0'+`zz'/(1-`vai'*`zz'))
capture _pctile `x' if `touse', p(`p1', `p2')
if _rc {
local bca1 `"."'
local bca2 `"."'
}
else {
local bca1 = r(r1)
local bca2 = r(r2)
}
}
else local vai
}
if "`bca1'`bca2'" == "" {
local bca1 = `"."'
local bca2 = `"."'
}
}
else {
scalar `z0' = .
local bc1 `"."'
local bc2 `"."'
local bca1 `"."'
local bca2 `"."'
}
// Compute percentiles
local p1 = (100 - `level')/2
local p2 = (100 + `level')/2
_pctile `x' if `touse', p(`p1', `p2')
local p1 = r(r1)
local p2 = r(r2)
// Compute normal CI
scalar `zalpha' = invttail(`n'-1, (100-`level')/200)
local n1 = `ebi' - `zalpha'*`sd'
local n2 = `ebi' + `zalpha'*`sd'
} // quietly
// Print results
local full `x' `n' `ebi' `bias' `sd'
if `"`sep'"' != "" {
TableSep
}
if `"`normal'"' == "" {
TableEntry `full' `n1' `n2' "(N)"
local full
}
if `"`percentile'"' == "" {
TableEntry `full' `p1' `p2' "(P)"
local full
}
if `"`bc'"' == "" {
TableEntry `full' `bc1' `bc2' "(BC)"
local full
}
if "`bca'" != "" {
TableEntry `full' `bca1' `bca2' "(BCa)"
}
else local vai `"."'
// Save results
tempname tmat
return scalar z0 = `z0'
return scalar accel = `vai'
matrix `tmat' = (`bca1' \ `bca2')
return matrix ci_bca `tmat'
matrix `tmat' = (`bc1' \ `bc2')
return matrix ci_bc `tmat'
matrix `tmat' = (`p1' \ `p2')
return matrix ci_percentile `tmat'
matrix `tmat' = (`n1' \ `n2')
return matrix ci_normal `tmat'
return scalar se = `sd'
return scalar bias = `bias'
return scalar stat = `ebi'
return scalar reps = `n'
end
program Estimate, eclass
syntax [varlist] , ///
b(name) ///
[ ///
accel(name) ///
nrep(integer -1) ///
NOBS(integer -1) ///
nclu(integer -1) ///
nstr(integer -1) ///
* ///
]
capture confirm matrix `b'
if _rc {
di as error `"b() invalid"'
exit 198
}
if `nobs' > 0 {
local obsopt obs(`nobs')
}
if `nrep' < 2 {
di as error `"insufficient observations in bootstrap dataset"'
exit 198
}
capture confirm matrix e(accel)
if `"`e(accel)'"' != "" {
tempname eaccel
matrix `eaccel' = e(accel)
}
local nvars : word count `varlist'
tempname eb
matrix `eb' = J(1,`nvars',0)
matrix colnames `eb' = `varlist'
tokenize `varlist'
// line up the columns for posting
forvalues i = 1/`nvars' {
matrix `eb'[1,`i'] = `b'[1,colnumb(`b',"``i''")]
}
SumAccum `varlist'
if `"`r(rc)'"' != "" {
di
di in smcl as error ///
"{p 0 0 2}insufficient observations to" ///
" compute bootstrap standard errors{break}" ///
"no results will be saved{break}"
exit `r(rc)'
}
tempname bs_b bs_v
mat `bs_b' = r(bs_b)
matrix rownames `bs_b' = y1
mat `bs_v' = r(V)
local n = r(n)
eret post `eb' `bs_v', `obsopt'
if `"`accel'"' != "" {
eret matrix accel `accel'
}
else if `"`eaccel'"' != "" {
eret matrix accel `eaccel'
}
local nalt : word count `options'
if mod(`nalt',2) != 0 {
di as error `"invalid extra options"'
exit 198
}
tokenize `options'
while `"`1'"' != "" {
local name `1'
local mat `2'
mac shift 2
eret matrix `name' `mat'
}
eret matrix bs_b `bs_b'
// scalars
if `nclu' > 0 {
eret scalar N_clust = `nclu'
}
if `nstr' > 0 {
eret scalar N_strata = `nstr'
}
// macros
eret local predict _no_predict
eret local cmd bootstrap
end
program SumAccum, rclass
syntax [varlist] [if] [in]
marksample touse
tempname b v
local K : word count `varlist'
qui count if `touse'
if r(N)<=1 {
return local rc 2000
exit
}
qui matrix accum `v' = `varlist' if `touse', deviations
local n = r(N)
local df = `n'-1
local k1 = `K'+1
matrix `b' = `v'[`k1',1..`K']/`n'
matrix `v' = `v'[1..`K',1..`K']/`df'
return matrix bs_b `b'
return matrix V `v'
return local n `n'
end
program TableHead
args title level nobs nstr nclu reps
if `"`title'"' == "" {
local title "Bootstrap statistics"
}
di
di in smcl as txt `"`title'"' _c
if `"`nobs'"' == "" {
local nobs .
}
di as txt _col(51) "Number of obs =" as res %10.0f `nobs'
if `"`nstr'"' != "" {
di as txt _col(51) "Number of strata =" as res %10.0f `nstr'
}
if `"`nclu'"' != "" {
di as txt _col(51) "N of clusters =" as res %10.0f `nclu'
}
di as txt _col(51) "Replications =" as res %10.0f `reps'
di
di in smcl as txt "{hline 13}{c TT}{hline 64}"
local cil `=string(`level')'
local cil `=length("`cil'")'
if `cil' == 2 {
local cititle "Conf. Interval"
}
else {
local cititle "Conf. Int."
}
di in smcl as txt %-12s "Variable" " {c |}" ///
%6s "Reps" ///
%10s "Observed" ///
%10s "Bias" ///
%10s "Std. Err" "." ///
%21s `"[`=strsubdp("`level'")'% `cititle']"'
TableSep
end
program TableSep
di in smcl as txt "{hline 13}{c +}{hline 64}"
end
program TableEntry
args name reps ebi bias se ll ul comment
capture confirm name `name'
if _rc == 0 {
di in smcl as txt %12s abbrev(`"`name'"',12) " {c |}" ///
as res ///
%6.0g `reps' _s(1) ///
%9.0g `ebi' _s(1) ///
%9.0g `bias' _s(1) ///
%9.0g `se' _s(2) ///
%9.0g `ll' _s(2) ///
%9.0g `ul' _s(1) ///
as txt %5s `"`comment'"'
}
else {
args ll ul comment
di in smcl as txt _col(13) " {c |}" ///
as res _col(53) ///
%9.0g `ll' _s(2) ///
%9.0g `ul' _s(1) ///
as txt %5s `"`comment'"'
}
end
program TableFoot
syntax [, nonormal nopercentile nobc bca]
if `"`normal'"' == "" {
local desc _col(8) "N = normal"
local break _n
}
if `"`percentile'"' == "" {
local desc `desc' `break' _col(8) "P = percentile"
local break _n
}
if `"`bc'"' == "" {
local desc `desc' `break' _col(8) "BC = bias-corrected"
local break _n
}
if "`bca'" != "" {
local desc `desc' `break' _col(8) ///
"BCa = bias-corrected and accelerated"
}
di in smcl as txt "{hline 13}{c BT}{hline 64}"
di "Note:" _c
di `desc'
end
exit
NOTES:
-bstat- is very dependent upon the dataset generated by -bootstrap-, thus
certain conditions must be set in order for -bstat- to behave consistently.
The -Check- subroutine checks certain conditions of the data in memory. These
conditions determine the version of the bootstrap dataset, as well as strict
conditions that are required by that version in order to proceed. These
conditions are based on characteristics (-char-) of the data and the variables.
In the following <var> is the name of a variable in the dataset.
Version 1: _dta[bs_version] == ""
char <var>[bstrap] is a number
NOTE: this characteristic will be copied over to
<var>[observed]
Version 2: _dta[bs_version] == 2
char <var>[observed] is a number
char <var>[acceleration] is a number if it exists
Output ruler:
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
Logit estimates Number of obs = 100
LR chi2(1) = 0.83
Prob > chi2 = 0.3610
Log likelihood = -5.1830091 Pseudo R2 = 0.0745
------------------------------------------------------------------------------
y | Coef. Std. Err. z P>|z| [95% Conf. Interval]
-------------+----------------------------------------------------------------
x | -3.712523 4.739402 -0.78 0.433 -13.00158 5.576535
_cons | -3.374606 1.441799 -2.34 0.019 -6.200481 -.5487309
------------------------------------------------------------------------------
----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8
Bootstrap statistics Number of obs = 74
Number of strata = 1
Replications = 50
------------------------------------------------------------------------------
Variable | Reps Observed Bias Std. Err. [95% Conf. Interval]
-------------+----------------------------------------------------------------
_bs_1 | 50 21.2973 .178109 .6358634 20.01948 22.57511 (N)
| 20.33784 22.93243 (P)
| 20.09459 22.24324 (BC)
| 20.09459 22.27027 (BCa)
------------------------------------------------------------------------------
N = normal
P = percentile
BC = bias-corrected
BCa = bias-corrected and accelerated
<end>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -