2

I am running some regression in matlab.

I want to store the varibles nicely through structures.

Here is some code:

    clc;                                                                                        
clear;                                                                                      
fruit_names={'Apple','Pear','Melon'};
Predictors.Apple = rand(500,11);
Predictors.Pear = rand(500,11);
Predictors.Melon = rand(500,11);
Returns.Apple = rand(500,1);
Returns.Pear = rand(500,1);
Returns.Melon = rand(500,1);
%%
 kk=1;
 for d = 1:length(fruit_names)
 for i = [1,2,3,4,5,6,12,24,48,60]
 for jj = 1:11
     K = i;
     xinit=[Predictors.(fruit_names{d})(:,jj)];
     yinit=Returns.(fruit_names{d});
     [b,bint,r,rint,stats] = regress(yinit,xinit);
     Stats.(fruit_names{d})(kk+1)=stats(1);
     kk=kk+1;%to help with reporting
 end
 end   
 end

It honestly my best attempt at a simple example. It does require the Econometrics toolbox if I remember correctly.

The problem I am having is the structure Stats stores the results I need but after the first variables include some useless zeros.

I have posted and deleted an earlier question which suggests removing the (kk,:) variables, but if I do this it only contains the final results not the evolution of results through the for loop.

3
  • 2
    you need to reset the iterator kk for each fruit_names, otherwise after the first fruit is proccessed, kk already reached value 11xnumel(i)=110. When the next fruit is processed, kk starts at 111 ... Commented Apr 17, 2019 at 14:10
  • How can I do what you propose Commented Apr 17, 2019 at 14:15
  • 1
    I think @user22485 answer below did just that. Commented Apr 17, 2019 at 14:25

1 Answer 1

1

Its the position of the kk variable that is the problem.

It seems like you need to collect the results from the second two loops.

clc;                                                                                        
clear;                                                                                      
fruit_names={'Apple','Pear','Melon'};
Predictors.Apple = rand(500,11);
Predictors.Pear = rand(500,11);
Predictors.Melon = rand(500,11);
Returns.Apple = rand(500,1);
Returns.Pear = rand(500,1);
Returns.Melon = rand(500,1);
%%

 for d = 1:length(fruit_names)

 kk=1; %move the kk = 1 variable here.

 for i = [1,2,3,4,5,6,12,24,48,60]
 for jj = 1:11
     K = i;
     xinit=[Predictors.(fruit_names{d})(:,jj)];
     yinit=Returns.(fruit_names{d});
     [b,bint,r,rint,stats] = regress(yinit,xinit);
     Stats.(fruit_names{d})(kk+1)=stats(1);
     kk=kk+1;%to help with reporting
 end
 end   
 end

Enjoy your fruit!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.