For loop in linear regression for number of assets

12 次查看(过去 30 天)
Hello!
I need to build linear regressions for variety of assets (more than 50) to get coefficients, pValues and Rsqured for each assets
My code is
X = Indreturn; %(historical data of market index returns)
Y=EFreturn(:,:); %(historical data of assests — 56 equity funds' returns)
for index = 1:56
md = fitlm(X,Y(:,index));
end
The problem is that I have linear regression output only for the last 56th fund as if I just write
y56 = EFreturn(:,56);
x = Indreturn;
md56=fitlm(x,y56);
I can do it 56 times for each asset of course, but I would like to know how to optimise this process and save time.
Thank you!

采纳的回答

dpb
dpb 2019-3-20
X = Indreturn; %(historical data of market index returns)
Y=EFreturn(:,:); %(historical data of assests — 56 equity funds' returns)
N=size(Y,2); % number funds in array Y
md=cell(N,1); % preallocate cell array to hold each model result
for index = 1:N
md{index} = fitlm(X,Y(:,index));
end
  3 个评论
Mahad Jama
Mahad Jama 2020-4-7
Is there a way to add a code to divide the rows? If for example you have 2 years of data and you want to run regression for each month?
dpb
dpb 2020-4-7
Look for "grouping variables" and/or varfun or findgroups, splitapply

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by