How to calculate Bootstrap confidence interval
25 views (last 30 days)
Show older comments
Abhishek K Singh
on 15 May 2024
Commented: Abhishek K Singh
on 16 May 2024
Hi all. I wonder how to calculate bootstrapped confidence interval for following data sent. The conventiona confidence interval is quite large and I wonder if it may make more sense with bootstrapped CIs.
x = [6 10 14 20 26 34 38];
y = [122 107 119 120 105 95 92];
I am using a*exp(-x/b) for fitting which gives a = 127, b = 130. I want to calculated the CIs for bootstrapped sample.
0 Comments
Accepted Answer
the cyclist
on 16 May 2024
Edited: the cyclist
on 16 May 2024
As you may have noticed already, the data have a huge variance around that fit:
x = [6 10 14 20 26 34 38]';
y = [122 107 119 120 105 95 92]';
% Tabulate the data
tbl = table(x,y);
% % Fit the model
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) F(1).*exp(-x/F(2));
beta0 = [1 1];
mdl = fitnlm(tbl,f,beta0)
% Calculate the model values at the empirical x
y_predicted = predict(mdl,x);
% Plot the data and fit
figure
plot(x,y,'*',x,y_predicted,'g');
legend('data','fit')
But, here is the code to build the bootstrap intervals. b in particular has a huge variance, and the fit to the resampled data off throws off warnings. I turned off warnings here, so you don't have to scroll through all of them to see the output, but I recommend leaving them on locally, so that you can investigate them. Caveat emptor.
rng default
x = [6 10 14 20 26 34 38]';
y = [122 107 119 120 105 95 92]';
numberObservations = numel(x);
NUMBER_RESAMPLE = 5000;
bootstrappedCoefficients = zeros(2,NUMBER_RESAMPLE);
warning("off")
for nr = 1:NUMBER_RESAMPLE
% if mod(nr,100)==0
% fprintf("Iteration %d of %d",nr,NUMBER_RESAMPLE)
% end
resampleIdx = randsample(numberObservations,numberObservations,true);
xr = x(resampleIdx);
yr = y(resampleIdx);
% Tabulate the data
tbl = table(xr,yr);
% % Fit the model
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) F(1).*exp(-x/F(2));
beta0 = [1 1];
mdl = fitnlm(tbl,f,beta0);
bootstrappedCoefficients(:,nr) = mdl.Coefficients.Estimate;
end
warning("on")
figure
tiledlayout(2,1)
nexttile
histogram(bootstrappedCoefficients(1,:))
nexttile
histogram(bootstrappedCoefficients(2,:))
coef1 = median(bootstrappedCoefficients(1,:))
coef1_lo95 = prctile(bootstrappedCoefficients(1,:), 2.5)
coef1_hi95 = prctile(bootstrappedCoefficients(1,:), 97.5)
coef2 = median(bootstrappedCoefficients(2,:))
coef2_lo95 = prctile(bootstrappedCoefficients(2,:), 2.5)
coef2_hi95 = prctile(bootstrappedCoefficients(2,:), 97.5)
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!