Search code examples
matlabstatistics-bootstrap

bootstrap data in confidence interval MATLAb


I tried this as well:

plot(x(bootsam(:,100)),y(bootsam(:,100)), 'r*') but it was exactly the same to my data! I want to resample my data in 95% confidence interval . But it seems this command bootstrp doesn't work alone, it needs some function or other commands to combine. Would you help me to figure it out?

I would like to generate some data randomly but behave like my function around the original data, I attached a plot which original data which are red and resampled data are in blue and green colors. enter image description here

Generally, I would like to use bootstrap to find error for my best-fit parameters. I read in this book: http://books.google.de/books?id=ekyupqnDFzMC&lpg=PA131&vq=bootstrap&hl=de&pg=PA130#v=onepage&q&f=false other methods for error analysis my fitted parameters are appreciated.


Solution

  • I suggest you start this way and then adapt it to your case.

    % One step at a time.
    % Step 1: Suppose you generate a simple linear deterministic trend with
    % noise from the standardized Gaussian distribution:
    N = 1000;    % number of points
    x = [(1:N)', ones(N, 1)];    % x values
    b = [0.15, 157]';    % parameters
    y = x * b + 10 * randn(N, 1);    % linear trend with noise
    % Step 2: Suppose you want to fit y with a linear equation:
    [b_hat, bint1] = regress(y, x);    % estimate parameters with linear regression
    y_fit = x * b_hat;    % calculate fitted values
    resid = y - y_fit;    % calculate residuals
    plot(x(:, 1), y, '.')    % plot
    hold on
    plot(x(:, 1), y_fit, 'r', 'LineWidth', 5)    % fitted values
    % Step 3: use bootstrap approach to estimate the confidence interval of
    % regression parameters
    N_boot = 10000;    % size of bootstrap
    b_boot = bootstrp(N_boot, @(bootr)regress(y_fit + bootr, x), resid);    % bootstrap
    bint2 = prctile(b_boot, [2.5, 97.5])';    % percentiles 2.5 and 97.5, a 95% confidence interval
    % The confidence intervals obtained with regress and bootstrp are
    % practically identical:
    bint1
    bint2