# Bootstrap prediction error rate

### Dimitri Shvorob (view profile)

15 Apr 2007 (Updated )

(Application of BSTRAP)

bprederr
```% BPREDERR : BSTATS application: bootstrap estimates of prediction error
%            rate. (Special case: simple regression / mean squared error)
% EXAMPLE  : bprederr (Oh, the FEX code metrics..)
% AUTHOR   : Dimitri Shvorob, dimitri.shvorob@vanderbilt.edu, 4/15/07

B = 1000;

% Simple regression
n = 100;
x = [ones(n,1) rand(n,1)];
b = [3 5]';
y = x*b + randn(n,1);

disp('Apparent prediction error')
b   = regress(y,x);
e   = y - x*b;
ape = mean(e.^2)    %#ok

disp('Bootstrapped prediction error: "simple" bootstrap')
z    = [y x];
res  = bstrap(B,1,'bpredfun1',z,z);     % bpredfun1 outputs a scalar, average squared prediction error
s    = [res.e];
bpe1 = mean(s,2)    %#ok

disp('Bootstrapped prediction error: 0.632 bootstrap')
z    = [(1:n)' y x];                    % row index passed through z
res  = bstrap(B,1,'bpredfun2',z,z);     % bpredfun2 outputs an n*1 squared-error vector, with 'ineligible' values replaced by NaNs
S    = [res.e];
s    = nanmean(S,2);                    % each row sum has a different number of averaged 'eligible' elements
bpe2 = .368*ape + .632*mean(s)          %#ok
```
```Apparent prediction error

ape =

0.6859

Bootstrapped prediction error: "simple" bootstrap

bpe1 =

0.6981

Bootstrapped prediction error: 0.632 bootstrap

bpe2 =

0.7104

```