Stepwise rolling regression: plot of coeffiecients

For questions regarding programming in the EViews programming language.

Moderators: EViews Gareth, EViews Jason, EViews Moderator, EViews Matt

jank
Posts: 2
Joined: Fri Jan 23, 2009 2:38 am

Stepwise rolling regression: plot of coeffiecients

Postby jank » Fri Jan 23, 2009 2:51 am

I have a stepwise rolling regression. For every subsample Ican have a different number of significant parameters. It is easy to get the calculation but my question now is how I can store the coefficients ( of the different equations) so, that I am able to plot a chart for the whole time period for every single coefficient (e.g. alpha, beta 1, beta 2 etc.).

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Stepwise rolling regression: plot of coeffiecients

Postby EViews Gareth » Fri Jan 23, 2009 9:18 am

Unfortunately this is not possible in the stepwise routine included in EViews (the main reason being that the stepwise routines doesn't actually calculate and/or store the coefficients). However you can always write your own stepwise routine and store them.

As a guide, here is a simple manual stepwise routine I wrote a while back:

Code: Select all


'create some data - 20 X variables and a Y variable that depends upon X11-X15.
'X variables are put into a group called XS

create u 1000
rndseed 1

group xs
group g
for !i=1 to 20
   series x!i=nrnd
   %name="x"+@str(!i)
   xs.add {%name}
   g.add {%name}
next

series y = nrnd + 3
for !i=11 to 15
   y = y + !i*x{!i}
next


'perform backwards stepwise regression - first run a model with all Xs, then remove Xs one at a time based upon tstat.

equation e1
!tstop=5
!stop=0
while !stop=0
e1.ls y c xs

!mint=10000
!removedvar=0
for !i=1 to xs.@count
   !currentt = @abs(e1.@tstats(!i+1))    'note plus one is since the equation has a constant as the first variable, but we are not testing the constant
   if !currentt < !mint then
      !mint = !currentt
      !removedvar=!i
   endif
next

if !mint>!tstop then
   !stop=1
else
   if !removedvar>0 then
      %removedvarname=xs.@seriesname(!removedvar)
      xs.drop {%removedvarname}
   endif
endif
wend

show e1


'run an automatic stepwise and see if it matches
equation e2.stepls(METHOD=UNI,BTOL=5,BACK,TSTAT) y c @ g
show e2
Follow us on Twitter @IHSEViews

jank
Posts: 2
Joined: Fri Jan 23, 2009 2:38 am

Re: Stepwise rolling regression: plot of coeffiecients

Postby jank » Mon Jan 26, 2009 2:28 am

Thanks a lot for your answer. But I mean the values for the significant variable are calculated as observabe in the tables and the ones not included could simply be set to zero. Isn't there a possibility to save these values (just the ones included in the regression) and to recognize for each value to which variable it belongs. Sorry for asking again ..

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Stepwise rolling regression: plot of coeffiecients

Postby EViews Gareth » Mon Jan 26, 2009 9:14 am

You can use the @coefs data-member of the equation to store the chosen coefficients into a vector. If you wanted, you could then place that vector into a row/column of a matrix to store all of your "rolls" as one big matrix.
Follow us on Twitter @IHSEViews

PCA
Posts: 24
Joined: Thu Aug 25, 2011 11:50 am

Re: Stepwise rolling regression: plot of coeffiecients

Postby PCA » Thu Aug 25, 2011 11:57 am

Could you elaborate on storing the coefficients for a stepwise regression?

I am running a very similar regression, and for example, sometimes the stepwise procedure will keep 3 explanatory variables, and sometimes it will keep 8. I think that this anomaly messes with the matrix set-up I have.

I am new to programming, so I know for a fact I am not setting up the matrix properly. Could you lend a hand?


I'm using eViews 7.

Code: Select all

   workfile riskdata_eviews q 1990q1 2011q2
 
    'run rolling regression

    ' set window size
    !window = 20

    ' set step size
    !step = 1

    ' get size of workfile
    !length = @obsrange

    ' declare equation for estimation
    equation eq1

    'calculate number of rolls
    !nrolls = @round((!length-!window)/!step)

    'matrix to store coefficient estimates
    matrix(13,!nrolls)  coefmat ' where 13 is the number of coefficients

    'variable keeping track of how many rolls we've done
    !j=0

    ' move sample !step obs at a time
    for !i = 1  to  !length-!window+1-!step step !step
       !j=!j+1

       ' set sample to estimation period
       smpl @first+!i-1 @first+!i+!window-2

       ' estimate equation
       eq1.stepls(METHOD=step,BTOL=2,BACK,TSTAT) y c @ _10yr cpi duration gold hy_spread ig_spread m1 move oil tedspread vix yieldcurveslope

       'store coefficients
       colplace(coefmat,eq1.@coefs,!j)
    next

    show coefmat

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Stepwise rolling regression: plot of coeffiecients

Postby EViews Gareth » Thu Aug 25, 2011 1:25 pm

Yeah, that's going to be problematic. From a conceptual point of view, how do you want to store the coefficients?
Follow us on Twitter @IHSEViews

PCA
Posts: 24
Joined: Thu Aug 25, 2011 11:50 am

Re: Stepwise rolling regression: plot of coeffiecients

Postby PCA » Thu Aug 25, 2011 1:45 pm

What I would like is a column for each explanatory variable, and a row for each "regression roll".

Is there anyway to possible store each coefficient into its own vector? That way if it is excluded from the model based on t-stat or p-value, then it would just say NA for that particular regression?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Stepwise rolling regression: plot of coeffiecients

Postby EViews Gareth » Thu Aug 25, 2011 2:10 pm

That's going to be very tricky, because you need to keep track of which variables were kept, and their position in the original list (i.e. if only MOVE, OIL, VIX are kept, you need to know to fill in the 9, 10, and 12th elements of the matrix).
Follow us on Twitter @IHSEViews

PCA
Posts: 24
Joined: Thu Aug 25, 2011 11:50 am

Re: Stepwise rolling regression: plot of coeffiecients

Postby PCA » Thu Aug 25, 2011 2:26 pm

Exactly. That is why I was wondering if it is possible to store every coefficient in a separate vector. Or somehow store each equation in its own matrix, and then combine them after the fact?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Stepwise rolling regression: plot of coeffiecients

Postby EViews Gareth » Thu Aug 25, 2011 2:35 pm

Sure, you can store every equation's coefficients in their own vector, just by doing something like:

Code: Select all

vector coefs!i = eq1.@coefs

immediately after you've performed the stepls. You're going to end up with a bunch of vectors with different lengths, but it will work.
Follow us on Twitter @IHSEViews

PCA
Posts: 24
Joined: Thu Aug 25, 2011 11:50 am

Re: Stepwise rolling regression: plot of coeffiecients

Postby PCA » Thu Aug 25, 2011 2:40 pm

Yeah, a little cumbersome, but it does works.

Ideally though, I'd like a fully filled matrix. I know you said it's very tricky, but is it even feasible you think?

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Stepwise rolling regression: plot of coeffiecients

Postby EViews Gareth » Thu Aug 25, 2011 2:51 pm

Currently I'm going to say no :(
Follow us on Twitter @IHSEViews

EViews Gareth
Fe ddaethom, fe welon, fe amcangyfrifon
Posts: 13305
Joined: Tue Sep 16, 2008 5:38 pm

Re: Stepwise rolling regression: plot of coeffiecients

Postby EViews Gareth » Thu Aug 25, 2011 3:02 pm

Actually, I take that back.

Theoretically you could do the following....

  • Use equation.@varlist to return a string containing the list of regressors that were chosen by the stepwise procedure
  • Loop through the elements of that string one at a time, and find their position in the original list of regressors using @wfind
  • Copy the coefficient associated with that regressor into the corresponding position in the original matrix

So something, very roughly, equal to:

Code: Select all

%fullregs = "u v w y z"

matrix(5, !nrolls) coefs

'after this inside your loop
equation eq1.stepls y c @ {%fullregs}
%chosenregs = eq1.@varlist
%chosenregs = @wmid(%chosenregs, 3)   'drop off Y and C from the varlist

for !n = 1 to @wcount(%chosenregs)
  %reg = @word(%chosenregs, !n)
   !nn = !n+1   'plus one because we don't care about C
  !coef = eq1.@coefs(!nn)
  %pos = @wfind(%fullregs, %reg)
  coefs(!i, %pos) = !coef
next
Follow us on Twitter @IHSEViews

PCA
Posts: 24
Joined: Thu Aug 25, 2011 11:50 am

Re: Stepwise rolling regression: plot of coeffiecients

Postby PCA » Thu Aug 25, 2011 3:24 pm

Still trying to work through this. It's giving me the following error:

"scalar assigned to string in "%POS = @WFIND("_10YR CPI DURATION GOLD HY_SPREAD IG_SPREAD M1 MOVE OIL TEDSPREAD VIX YIELDCURVESLOPE", "_10YR")

I'm not sure if I implemented your coding right. Still trying to decipher it a bit.


Code: Select all

   workfile riskvariables_eviews q 1990q1 2011q2
'group riskvariables
'run rolling regression

' set window size
!window = 20

' set step size
!step = 1

' get size of workfile
!length = @obsrange

' declare equation for estimation
equation eq2

'string of variables
%fullregs = "_10yr cpi duration gold hy_spread ig_spread m1 move oil tedspread vix yieldcurveslope"

'calculate number of rolls
!nrolls = @round((!length-!window)/!step)

'matrix to store coefficient estimates
matrix(13,!nrolls)  coefs' where 13 is the number of coefficients

'variable keeping track of how many rolls we've done
!j=0

' move sample !step obs at a time
for !i = 1  to  !length-!window+1-!step step !step
   !j=!j+1

   ' set sample to estimation period
   smpl @first+!i-1 @first+!i+!window-2

   ' estimate equation
   ' eq2.stepls(METHOD=UNI,BTOL=2,BACK,TSTAT) y c @ _10yr cpi duration gold hy_spread ig_spread m1 move oil tedspread vix yieldcurveslope

    'after this inside your loop
    equation eq2.stepls(METHOD=UNI,BTOL=2,BACK,TSTAT) y c @ {%fullregs}
    %chosenregs = eq2.@varlist
    %chosenregs = @wmid(%chosenregs, 3)   'drop off Y and C from the varlist

    for !n = 1 to @wcount(%chosenregs)
      %reg = @word(%chosenregs, !n)
       !nn = !n+1   'plus one because we don't care about C
      !coef = eq2.@coefs(!nn)
      %pos = @wfind(%fullregs, %reg)
      coefs(!i, %pos) = !coef
   next
next
   

PCA
Posts: 24
Joined: Thu Aug 25, 2011 11:50 am

Re: Stepwise rolling regression: plot of coeffiecients

Postby PCA » Thu Aug 25, 2011 3:29 pm

And actually, I need to know the constant, so does that alter the coding you provided at all?

%chosenregs = @wmid(%chosenregs, 3) 'drop off y and C from the varlist

and

!nn = !n+1 'plus one because we don't care about C


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 33 guests