Rolling regression with some missing entries

For questions regarding programming in the EViews programming language.

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

jayk
Posts: 2
Joined: Tue Mar 30, 2010 4:33 pm

Rolling regression with some missing entries

Postby jayk » Tue Mar 30, 2010 5:30 pm

Hi. I'm new to eviews programming, so I may need a little more guidance here. I'm working on getting rolling regression coefficients for several estimations. I have three time series,y,u, and c, each with 170 observations, but need to run rolling regressions with a window of 60 observations, and store the coefficients of the regressions. I also want to run these equations for 6 different country sets at once, to give cofficients for each country.
However, for some countries, there is no data for the independent varaible c, so when the program runs and finds no data, it terminates at the first set of rolling regression, and gives no coefficients.The program works fine for those countries with complete data.
So, is there a way to tell the program to skip the regression for the windows with no data on a specific variable, and put "N/A" and continue to the next window?
This is how my program is set up so far;

!window =60

' set step size
!step =1

' get size of workfile
!length = @obsrange

' declare equation for estimation
for %z can ger ken uk fra ger
equation eq1_{%z}
equation eq2_{%z}
next

'calculate number of rolls

!nrolls = @round((!length-!window)/!step)

'matrix to store coefficient estimates (2X!nrolls matrix) '

for %z can ger ken uk fra ger
matrix(2,!nrolls) coefmatC1C2_{%z}

next

'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
for %z can ger ken uk fra ger
'set sample to estimation period

vector p1_{%z} = @cmin(c_{%z})
vector p2_{%z} = @cmin(u_{%z})
vector p3_{%z} = @cmin(y_{%z})
next
smpl @first+!i @first+!i+!window-1

for %z can ger ken uk fra ger
'v1, v2 and v3 recode the series so that the NA's are replaced with -999999 and we can use the numerical commands for 'if'
'series v1_{%z} = @nan(c_{%z},lowerbound)
'series v2_{%z} = @nan(u_{%z},lowerbound)
'series v3_{%z} = @nan(y_{%z},lowerbound)
'p1, p2 and p3 simply computes the minimum of the original series - which implies e.g. that if the minimum of v1 is -999999, it will report NA instead

vector p1_{%z} = @cmin(c_{%z})
vector p2_{%z} = @cmin(u_{%z})
vector p3_{%z} = @cmin(y_{%z})

'mv1, mv2 and mv3 recode the minimum computed before so that the NA's are replaced with -999999 and we can use the numerical commands for 'if'
scalar lowerbound1 = -999999
vector mv1_{%z} = @nan(p1_{%z},lowerbound1)
vector mv2_{%z} = @nan(p2_{%z},lowerbound1)
vector mv3_{%z} = @nan(p3_{%z},lowerbound1)
if mv1_{%z} > -999999 and mv2_{%z} > -999999 and mv3_{%z} > -999999 then
eq1_{%z}.ls y_{%z} =C(1)*u_{%z}+C(2)* c_{%z}
endif
next

'store coefficients
for %z can ger ken uk fra ger
if mv1_{%z} > -999999 and mv2_{%z} > -999999 and mv3_{%z} > -999999 then
colplace(coefmatC1C2_{%z},eq1_{%z}.@coefs,!j)

else
'vec12 is created so that we can fill the blank spaces in those instances in which the regression did not run before because of insufficient data
vector (2) vec12_{%z}
vec12_{%z} = -999999

colplace(coefmatC1C2_{%z},vec12_{%z},!j)

endif
next

next

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

Re: Rolling regression with some missing entries

Postby EViews Gareth » Tue Mar 30, 2010 6:10 pm

Without having looked at your code in detail, what happens if you run your program with max errors set high?
Follow us on Twitter @IHSEViews

Amber
Posts: 31
Joined: Wed Aug 03, 2011 4:35 am

Re: Rolling regression with some missing entries

Postby Amber » Wed Jan 11, 2012 6:31 am

Dear Gareth,

I have a similar problem:
I try to run the rolling regresssion on each firm data in a pool workfile (360 obs * 300 firms)
For each firm: 36 window with 12 step for each firm using firm=c(1)+c(2)*mkt.
And loop across all 300 firms.
The problem is my pool data is not balanced. So most of the firms have missing value at the start/end of the period. And these NAs make it impossibe to start rolling regression.

Could you please help me out, thanks so much!

Best regards,

Amber

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

Re: Rolling regression with some missing entries

Postby EViews Gareth » Wed Jan 11, 2012 9:04 am

You could use the @ifirst function, along with the @otod function to find the date of the first available observation for the current firm, and then roll from that date onwards.
Follow us on Twitter @IHSEViews

Amber
Posts: 31
Joined: Wed Aug 03, 2011 4:35 am

Re: Rolling regression with some missing entries

Postby Amber » Thu Jan 12, 2012 5:42 am

Dear Gareth, thanks for you prompt reply. I have tried to program for one firm (firm 10873), whose begining 50 and last 40 observations are NAs. I use monthly data and rolling 1 year ahead every 3 years.

Code: Select all

'------------------------------------
!window=36
!step=12
!length=@obsrange
!nrolls=@round((!length-!window)/!step)
matrix(2, !nrolls) coefmat
'-------------------------------------
%ser="exmret_10873"
smpl @all
series temp=@recode({%ser}<>na, @trend, na)+1
%start=@otod(@ifirst(temp))
%end=@otod(@ilast(temp))
'----------------------------------------
!j=0
'----------
for !i=1 to !length-!window+1-!step step !step
      !j=!j+1
      %first=@otod(@dtoo(%start)+!i-1)
      %last=@otod(@dtoo(%start)+!i+!window-2)
      smpl {%first} {%last}
      equation eq1.ls exmret_10873 c mktrf
      colplace(coefmat,eq1.@coefs,!j)
next



My problem is: Since I fail to code the rounds properly, the matrix to hold coefs doesn't match the data with NAs. So error showed as: insufficient number of obs to estimate equation.
My second question is: how shall I add the loop to make this program repeated across all firms? (I have the firm id)

Thanks so much, your help has been always valuable to me.

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

Re: Rolling regression with some missing entries

Postby EViews Gareth » Thu Jan 12, 2012 8:35 am

I don't understand your first question.

For your second question, can't you just loop over the firm names?
Follow us on Twitter @IHSEViews

Amber
Posts: 31
Joined: Wed Aug 03, 2011 4:35 am

Re: Rolling regression with some missing entries

Postby Amber » Thu Jan 12, 2012 8:55 am

Dear Gareth,

Sorry for the confusion. My code seems have some problems: error as: insufficient number of observations to estimate equation. Because the ending and starting date of non-NA is different for each firm, so the round is also firm-dependent, so is the dimension of the matrix that holds the coefs from each roll. How can I solve this?

My problem about my second question is: the fund id are numerical, and can't just list it in string.

Thanks!!

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

Re: Rolling regression with some missing entries

Postby EViews Gareth » Thu Jan 12, 2012 9:12 am

Just size the matrix big enough so that it can hold all firms? I guess I'm still not seeing what the problem is.
Follow us on Twitter @IHSEViews

Amber
Posts: 31
Joined: Wed Aug 03, 2011 4:35 am

Re: Rolling regression with some missing entries

Postby Amber » Thu Jan 12, 2012 9:53 am

Dear Gareth,

Code: Select all

matrix(27, 249) xx
'----------------------------------------------------------------
for !m=1 to exmret.@count
    %mname=exmret.@seriesname(!m)
    smpl @all
    series temp{!m}=@recode({%mname}<>na, @trend, na)+1
    %start=@otod(@ifirst(temp{!m}))
    %end=@otod(@ilast(temp{!m}))
'----------------------------------------------------------------
    !window=36
    !step=12
    !length=@obsrange
    !nrolls=@round((!length-!window)/!step)
    matrix(2, !nrolls) coefmat{!m}
'-----------------------------------------------------------------
    !j=0
'-----------------------------------------------------------------
    for !i=1 to !length-!window+1-!step step !step
        !j=!j+1
        %first=@otod(@dtoo(%start)+!i-1)
        %last=@otod(@dtoo(%start)+!i+!window-2)
        smpl {%first} {%last}
        equation eq1.ls {%mname} c mktrf
        colplace(coefmat{!m},eq1.@coefs,!j)
    next
'----------------------------------------------------------------
    matrix coefs=@transpose(coefmat{!m})
    vector alpha=@columnextract(coefs, 1)
    matplace(xx, alpha, 1, !m)
next


I have tried the codes above in the pool of all firms (248 firms, unbalanced pool, 360 range). The problem is the loop can't continue. Error is: Insufficient number of observations in "EQUATION EQ1.LS EXMRET_10873 C MKTRF".
I am really desperate now. Could you have a look of the codes for me? I really appreciate.

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

Re: Rolling regression with some missing entries

Postby EViews Gareth » Thu Jan 12, 2012 9:56 am

Well, the error message is probably correct - you probably don't have enough observations in the current sample. I'm not sure how much help I can be other than that.
Follow us on Twitter @IHSEViews

Amber
Posts: 31
Joined: Wed Aug 03, 2011 4:35 am

Re: Rolling regression with some missing entries

Postby Amber » Thu Jan 12, 2012 10:26 am

Dear Gareth, thanks for your reply. I think I find my problem. I need to make the rolling window for each firm stops by firm's last non-NA observation, and then carry on the repetition to the next firm. I defined the last non-NA observation by

Code: Select all

%end=@otod(@ilast(temp{!m}))
in the loop. But in the following rolling window loop, I don't know how to adjust the estimation sample.

Code: Select all

        %first=@otod(@dtoo(%start)+!i-1)
        %last=@otod(@dtoo(%start)+!i+!window-2)
        smpl {%first} {%last}

Do you have any idea? :) Thank you very much.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 8 guests