How to save coefficients and residuals in the loop?

For questions and discussion related to reading in and working with data.
yelena
Posts: 31
Joined: Wed Sep 09, 2015 8:49 pm

How to save coefficients and residuals in the loop?

Unread post by yelena »

Dear Tom, I am trying to run a loop for several series. But it seems that in my code the residuals are overwitten with the last series. How to save each residuals (resids) and coefficients (coef) for each series in the loop separately (in a matrix or so)? The incorrect program is attached:

dofor i = DEPENDENT FED LIBOR12 RGDP UR CPI TR3M TR2Y TR5Y TR10Y BAA MORT SP500 NCREIF CRE HPI $
OIL VOL NGDP BAASPREAD TERM SLOPE
linreg(robusterrors) DEPENDENT sambeg samend resids coef
# CONSTANT i
end dofor
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How to save coefficients and residuals in the loop?

Unread post by TomDoan »

What are you planning to do with them? I assume you want 22 sets of each at the end. But what are you doing once you have them?
yelena
Posts: 31
Joined: Wed Sep 09, 2015 8:49 pm

Re: How to save coefficients and residuals in the loop?

Unread post by yelena »

I have a to have 22 linear regressions for each of the series and report coefficients and residuals for each regression.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How to save coefficients and residuals in the loop?

Unread post by TomDoan »

Report how?
yelena
Posts: 31
Joined: Wed Sep 09, 2015 8:49 pm

Re: How to save coefficients and residuals in the loop?

Unread post by yelena »

In Series Window I have to see the residuals for each series and I'd prefer to have coefficients for a constant and slope for each series as well, possibly in a nice matrix form. Or if not possible, just to see them in a Series Window.
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How to save coefficients and residuals in the loop?

Unread post by TomDoan »

Code: Select all

dofor i = DEPENDENT FED LIBOR12 RGDP UR CPI TR3M TR2Y TR5Y TR10Y BAA MORT SP500 NCREIF CRE HPI $
   OIL VOL NGDP BAASPREAD TERM SLOPE
   linreg(robusterrors) DEPENDENT sambeg samend resids coef
   # CONSTANT i
end dofor
There's something wrong with the loop logic, since you have DEPENDENT regressed on itself in the first regression.

The quick way to get those into separately named series is to use %S and %L, as in

linreg(robusterrors) DEPENDENT sambeg samend %s("R_"+%l(i)) %s("C_"+%l(i))

which will create R_FED, C_FED series when FED is the explanatory variable, etc. You might want to look into the REPORT instruction for handling the coefficients---it wouldn't be hard to write this to generate a table with the coefficients.
yelena
Posts: 31
Joined: Wed Sep 09, 2015 8:49 pm

Re: How to save coefficients and residuals in the loop?

Unread post by yelena »

Dear Tom,
Thank you very much. It works now! Great!
One more question: since these loop was after many other loops in the code, the error terms and coefficients are not marked from 1 - 22. Do you know what causes it? Can we somehow to force it run from 1 - N of linear regressions.
Attachments
RATS1.PNG
RATS1.PNG (70.76 KiB) Viewed 12859 times
TomDoan
Posts: 7814
Joined: Wed Nov 01, 2006 4:36 pm

Re: How to save coefficients and residuals in the loop?

Unread post by TomDoan »

Did you write %s("R_"+%l(i)) (as I wrote) or %s("R_"+i)? The former uses the labels, the latter just uses the sequence number.
yelena
Posts: 31
Joined: Wed Sep 09, 2015 8:49 pm

Re: How to save coefficients and residuals in the loop?

Unread post by yelena »

Oops. My faults. Exactly, I used %("R_" +%(i)). Thank you very much.
Post Reply