Bootstrap multi linear model

조회 수: 14 (최근 30일)
Fabrice Lambert
Fabrice Lambert 2021년 10월 4일
댓글: Fabrice Lambert 2021년 10월 5일
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