|
1 | | -% Computes simple percentile confidence interval(s) directly from a vector (or |
2 | | -% row-major matrix) of bootstrap statistics. |
| 1 | +% Computes percentile confidence interval(s) directly from a vector (or row- |
| 2 | +% major matrix) of bootstrap statistics. |
3 | 3 | % |
4 | 4 | % -- Function File: CI = bootint (BOOTSTAT) |
5 | 5 | % -- Function File: CI = bootint (BOOTSTAT, PROB) |
| 6 | +% -- Function File: CI = bootint (BOOTSTAT, PROB, ORIGINAL) |
6 | 7 | % |
7 | | -% 'CI = bootint (BOOTSTAT)' computes 95% percentile confidence intervals |
8 | | -% directly from the vector, or rows* of the matrix in BOOTSTAT, where |
9 | | -% BOOTSTAT contains bootstrap statistics such as those generated using |
10 | | -% the `bootstrp` function. Depending on the application, bootstrap |
11 | | -% confidence intervals with better coverage and accuracy can be computed |
12 | | -% using the various dedicated bootstrap functions from the statistics- |
13 | | -% resampling package. |
| 8 | +% 'CI = bootint (BOOTSTAT)' computes simple 95% percentile confidence |
| 9 | +% intervals [1,2] directly from the vector, or rows* of the matrix in |
| 10 | +% BOOTSTAT, where BOOTSTAT contains bootstrap statistics such as those |
| 11 | +% generated using the `bootstrp` function. Depending on the application, |
| 12 | +% bootstrap confidence intervals with better coverage and accuracy can |
| 13 | +% be computed using the various dedicated bootstrap confidence interval |
| 14 | +% functions from the statistics-resampling package. |
14 | 15 | % |
15 | 16 | % * The matrix should have dimensions P * NBOOT, where P corresponds to |
16 | 17 | % the number of parameter estimates and NBOOT corresponds to the number |
|
28 | 29 | % The default value of PROB is the vector: [0.025, 0.975], for an |
29 | 30 | % equal-tailed 95% percentile confidence interval. |
30 | 31 | % |
31 | | -% bootint (version 2024.04.24) |
| 32 | +% 'CI = bootint (BOOTSTAT, PROB, ORIGINAL)' uses the ORIGINAL estimates |
| 33 | +% associated with BOOTSTAT to correct PROB and the resulting confidence |
| 34 | +% intervals (CI) for median bias. The confidence intervals returned in CI |
| 35 | +% therefore become bias-corrected percentile intervals [3,4]. |
| 36 | +% |
| 37 | +% BIBLIOGRAPHY: |
| 38 | +% [1] Efron (1979) Bootstrap Methods: Another look at the jackknife. |
| 39 | +% Annals Stat. 7,1-26 |
| 40 | +% [2] Efron, and Tibshirani (1993) An Introduction to the Bootstrap. |
| 41 | +% New York, NY: Chapman & Hall |
| 42 | +% [3] Efron (1981) Nonparametric Standard Errors and Confidence Intervals. |
| 43 | +% Can J Stat. 9(2):139-172 |
| 44 | +% [4] Efron (1982) The jackknife, the bootstrap, and other resampling plans. |
| 45 | +% SIAM-NSF, CBMS #38 |
| 46 | +% |
| 47 | +% bootint (version 2024.05.19) |
32 | 48 | % Author: Andrew Charles Penn |
33 | 49 | % https://www.researchgate.net/profile/Andrew_Penn/ |
34 | 50 | % |
|
46 | 62 | % You should have received a copy of the GNU General Public License |
47 | 63 | % along with this program. If not, see http://www.gnu.org/licenses/ |
48 | 64 |
|
49 | | -function CI = bootint (Y, PROB) |
| 65 | +function CI = bootint (Y, PROB, T0) |
50 | 66 |
|
51 | 67 | % Check input and output arguments |
52 | 68 | if (nargin < 2) |
53 | 69 | PROB = 0.95; |
54 | 70 | end |
55 | | - if (nargin > 2) |
| 71 | + if (nargin > 3) |
56 | 72 | error ('bootint: Too many input arguments.') |
57 | 73 | end |
58 | 74 | if (nargout > 1) |
|
67 | 83 | if (sz(2) == 1) |
68 | 84 | p = 1; |
69 | 85 | Y = Y.'; |
| 86 | + nboot = numel (Y); |
70 | 87 | else |
71 | 88 | p = sz(1); |
72 | 89 | nboot = sz(2); |
|
100 | 117 | PROB = arrayfun (@(c) (1 + c * PROB) / 2, [-1, 1]); |
101 | 118 | end |
102 | 119 | end |
| 120 | + |
| 121 | + % Apply median bias correction to PROB if T0 is provided |
| 122 | + if (nargin > 2) |
| 123 | + % Error checking for T0 |
| 124 | + if (~ isa (PROB, 'numeric')) |
| 125 | + error ('bootint: ORIGINAL must be numeric') |
| 126 | + end |
| 127 | + szT0 = size (T0); |
| 128 | + if ( (all (szT0 > 1)) || (szT0(2) > 1) ) |
| 129 | + error ('bootint: ORIGINAL must be a scalar or column vector') |
| 130 | + end |
| 131 | + if (p > 1) |
| 132 | + if (szT0(1) ~= p) |
| 133 | + error ('bootint: BOOTSTAT and ORIGINAL must have the same number of rows') |
| 134 | + end |
| 135 | + else |
| 136 | + if (szT0(1) > 1) |
| 137 | + error ('bootint: If BOOTSTAT is a vector, ORIGINAL must be a scalar') |
| 138 | + end |
| 139 | + end |
| 140 | + % Create distribution functions |
| 141 | + stdnormcdf = @(x) 0.5 * (1 + erf (x / sqrt (2))); |
| 142 | + stdnorminv = @(p) sqrt (2) * erfinv (2 * p - 1); |
| 143 | + % Calculate the median bias correction constant (z0) |
| 144 | + z0 = stdnorminv (sum (bsxfun (@lt, Y, T0), 2) / nboot); |
| 145 | + if (~ all (isfinite (z0))) |
| 146 | + % Revert to percentile bootstrap confidence intervals |
| 147 | + warning ('bootint:biasfail', ... |
| 148 | + cat (2, 'Unable to calculate the bias correction constant;', ... |
| 149 | + ' reverting to simple percentile intervals.\n')) |
| 150 | + z0 = zeros (p, 1); |
| 151 | + end |
| 152 | + % Calculate BC percentiles |
| 153 | + z = stdnorminv (PROB); |
| 154 | + PROB = stdnormcdf (bsxfun (@plus, 2 * z0, z)); |
| 155 | + end |
| 156 | + |
103 | 157 | % Compute confidence intervals |
104 | 158 | CI = nan (p, 2); |
105 | 159 | for j = 1:p |
|
117 | 171 |
|
118 | 172 | end |
119 | 173 |
|
| 174 | +%!demo |
| 175 | +%! |
| 176 | +%! % Law school data |
| 177 | +%! data = [576, 3.39; 635, 3.30; 558, 2.81; 578, 3.03; 666, 3.44; ... |
| 178 | +%! 580, 3.07; 555, 3.00; 661, 3.43; 661, 3.36; 605, 3.13; ... |
| 179 | +%! 653, 3.12; 575, 2.74; 545, 2.76; 572, 2.88; 594, 2.96]; |
| 180 | +%! x = data(:, 1); |
| 181 | +%! y = data(:, 2); |
| 182 | +%! r = cor (x, y); |
| 183 | +%! |
| 184 | +%! % 95% confidence interval for the mean |
| 185 | +%! bootstat = bootstrp (4999, @cor, x, y); |
| 186 | +%! CI_per = bootint (bootstat,0.95) % 95% simple percentile interval |
| 187 | +%! CI_cper = bootint (bootstat,0.95,r) % 95% bias-corrected percentile interval |
| 188 | +%! |
| 189 | +%! % Please be patient, the calculations will be completed soon... |
| 190 | + |
120 | 191 | %!test |
121 | 192 | %! |
122 | | -%! % Simulate (log-normal) data |
123 | | -%! randn ('seed', 1); |
124 | | -%! Y = exp (randn (5, 999)); |
| 193 | +%! % Law school data |
| 194 | +%! data = [576, 3.39; 635, 3.30; 558, 2.81; 578, 3.03; 666, 3.44; ... |
| 195 | +%! 580, 3.07; 555, 3.00; 661, 3.43; 661, 3.36; 605, 3.13; ... |
| 196 | +%! 653, 3.12; 575, 2.74; 545, 2.76; 572, 2.88; 594, 2.96]; |
| 197 | +%! x = data(:, 1); |
| 198 | +%! y = data(:, 2); |
| 199 | +%! r = cor (x, y); |
125 | 200 | %! |
126 | 201 | %! % 95% confidence interval for the mean |
127 | | -%! CI = bootint (Y,0.95); # 95% percentile interval |
128 | | -%! CI = bootint (Y,[0.025,0.975]); # 95% percentile interval |
| 202 | +%! bootstat = bootstrp (4999, @cor, x, y); |
| 203 | +%! CI = bootint (bootstat,0.95); % 95% percentile interval |
| 204 | +%! CI = bootint (bootstat,[0.025,0.975]); % 95% percentile interval |
| 205 | +%! CI = bootint (bootstat,0.95,r); % 95% bias-corrected interval |
| 206 | +%! CI = bootint (bootstat,[0.025,0.975],r); % 95% bias-corrected interval |
0 commit comments