Bootstrap multi linear model
4 ビュー (過去 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
0 件のコメント
採用された回答
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
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.
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!