How can I compute multivariate regression residuals?

I am running a loop to obtain residuals from a number of regressions. I have got 1939 y values(dependent variables). I need a series of residuals for each. I use the following code.
for i=1:1939
stats= regstats(y(:,i),x,'linear', {'beta', 'r'});
end
However, I get only one column of residuals when I checked the stats. I doubt the residuals overwrite on only one column after each regression. Any ideas to solve this problem? Thanks.

 Accepted Answer

Thank you for your answer. Your code works:
for i=1:1939 stats(i)= regstats(y(:,i),f,'linear', {'beta', 'r'}); a(:,i)=stats(i).r; end
Yes, weird but I really have 1939 regressions and number of observations is 252.

3 Comments

OK, just checking...
If this closes the thread, please "ACCEPT" the Answer so others can tell and not take time to answer again.
Then I'll clean up the thread...dpb
Sure, thank you again.
Well, I really meant to Accept the DB Original Answer... :)

Sign in to comment.

More Answers (1)

You really have 1939 different regressions or 1939 observations?
Whichever, you get only one (the last) result the way the code is written because you overwrite the stats structure every pass through the loop.
stats(i)=regstats(y(:,i),x,'linear', {'beta', 'r'});

1 Comment

[BA Answer moved to Comment -- dpb]
Thank you for your answer. Your code works:
for i=1:1939
stats(i)= regstats(y(:,i),f,'linear', {'beta', 'r'}); a(:,i)=stats(i).r;
end
Yes, weird but I really have 1939 regressions and number of observations is 252.

Sign in to comment.

Products

Release

R2018a

Asked:

on 8 Oct 2018

Commented:

dpb
on 8 Oct 2018

Community Treasure Hunt

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

Start Hunting!