Bootstrap multi linear model

15 次查看(过去 30 天)
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

采纳的回答

Jeff Miller
Jeff Miller 2021-10-4
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 个评论
Jeff Miller
Jeff Miller 2021-10-5
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 2021-10-5
yes, good point. That makes it more flexible than using regress. Thanks for the help!

请先登录,再进行评论。

更多回答(0 个)

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by