For loop in linear regression for number of assets

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!

 Accepted Answer

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 Comments

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?
Look for "grouping variables" and/or varfun or findgroups, splitapply

Sign in to comment.

More Answers (0)

Categories

Find more on Descriptive Statistics and Insights in Help Center and File Exchange

Asked:

on 20 Mar 2019

Commented:

dpb
on 7 Apr 2020

Community Treasure Hunt

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

Start Hunting!