Rolling window with NA values
Posted: 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
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