Rolling window with NA values

For questions regarding programming in the EViews programming language.

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

justin_m
Posts: 13
Joined: Sun Feb 01, 2015 8:37 pm

Rolling window with NA values

Postby justin_m » Mon Apr 24, 2017 6:30 pm

Hi. I have the following code which works fine until !h=20 (the results match those of my matlab code). The problem is that for country_20, the first x>>252 observations are NaN, therefore the regression cannot be run and the loop breaks down. How can I apply this so that the first observation is the first nonnan observation, which varies across countries? Note that the excel sheet is such that the data for each country has the same amount of rows; the difference is that data for some countries starts later than for others (meaning that before the starting data there are NAs everywhere).

Ex:
Country 1 Country 2

NA NA
NA NA
1 NA
2 NA
3 8
4 9
5 10
6 9
7 9


After loading the data, the code is:

'********************************************************************
'*************************'Regression 1****************************
'********************************************************************
'run rolling regression

' set window size
!window = 252

' set step size
!step = 1

' get size of workfile
!length = @obsrange

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

for !h=1 to 70 'this is number of countries

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

'matrix to store t-stat eestimates
matrix(7,!nrolls) tstatsmat' where 7 is the number of coefficients

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

' declare equation for estimation
equation eq!h

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 - where the equation is y=c(1) + c(2)*x1 + c(3)*x2
eq!h.ls country!h c x1(-1) x2(-1) x3(-1) x4(-1) x5(-1) country!h(-1)

'store coefficients
colplace(coefmat,eq!h.@coefs,!j)

'store tstats
colplace(tstatsmat,eq!h.@tstats,!j)

next

rename coefmat coefmat_r1_!h
rename tstatsmat tstatsmat_r1_!h

coefmat_r1_!h = @transpose(coefmat_r1_!h)
tstatsmat_r1_!h = @transpose(tstatsmat_r1_!h)

next

EViews Matt
EViews Developer
Posts: 562
Joined: Thu Apr 25, 2013 7:48 pm

Re: Rolling window with NA values

Postby EViews Matt » Tue Apr 25, 2017 9:12 am

Hello,

For an individual series x, you can use @ifirst(x) or x.@first to retrieve the observation/date of the first non-NA value in the series, which you could then use to adjust your sample.


Return to “Programming”

Who is online

Users browsing this forum: No registered users and 20 guests