Bootstrap multi linear model

5 views (last 30 days)
Hi. I'm trying to bootstrap a multilinear model using bootstrp. I can get it to work with regress, but not with fitlm. Unfortunatly, the help doesn't provide an example for this. Here's some model code:
x = (1:1000)';
y1 = 2*x+1 + random('normal',0,2,1000,1);
y2 = 0.5*x - 2 + random('normal',0,2,1000,1);
z = 3 * y1 + 2 * y2 + 1 + random('normal',0,2,1000,1);
b1 = bootstrp(1000, @regress, z, [y1 y2 ones(1000,1)] ); % <-- This works
b2 = bootstrp(1000, @fitlm, [y1 y2], z); % < -- This doesn't work
Could someone please explain how to use fitlm withn bootstrp?
Thanks a lot,
Fabrice

Accepted Answer

Jeff Miller
Jeff Miller on 4 Oct 2021
The problem is that fitlm produces a complicated output structure and bootstrp doesn't know what to do with it.
x = (1:1000)';
y1 = 2*x+1 + random('normal',0,2,1000,1);
y2 = 0.5*x - 2 + random('normal',0,2,1000,1);
z = 3 * y1 + 2 * y2 + 1 + random('normal',0,2,1000,1);
% look at the very different outputs of regress vs fitlm:
b11 = regress(z,[y1 y2 ones(1000,1)])
b21 = fitlm([y1 y2],z)
% One way around the problem is to define & bootstrap your own version
% of the function fitlm that returns only a vector of values
b2 = bootstrp(1000, @myfitlm, [y1 y2], z); % < -- This works
function myout = myfitlm(preds,tobepred)
% select out the coefficients that are to be bootstrapped.
b21 = fitlm(preds,tobepred);
myout = b21.Coefficients.Estimate;
end
  3 Comments
Fabrice Lambert
Fabrice Lambert on 5 Oct 2021
yes, good point. That makes it more flexible than using regress. Thanks for the help!

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!