Bootstrap multi linear model

6 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
Jeff Miller
Jeff Miller on 5 Oct 2021
In the myfitlm function, you could select out different or additional information from the b21 structure if you wanted to bootstrap something other than the coefficients. I think you can select whatever / as much as / you want from that structure as long as you assemble all of it into a numerical vector myout.
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!