Deepak Agarwal
Deepak Agarwal

Reputation: 399

Create multiple lagged variables using a zoo object

I need to create 'n' number of variables with lags of the original variable from 1 to 'n' on the fly. Something like so :- OrigVar

DatePeriod, value
2/01/2018,6
3/01/2018,4
4/01/2018,0
5/01/2018,2
6/01/2018,4
7/01/2018,1
8/01/2018,6
9/01/2018,2
10/01/2018,7

Lagged 1 variable
2/01/2018,NA
3/01/2018,6
4/01/2018,4
5/01/2018,0
6/01/2018,2
7/01/2018,4
8/01/2018,1
9/01/2018,6
10/01/2018,2
11/01/2018,7

Lagged 2 variable
2/01/2018,NA
3/01/2018,NA
4/01/2018,6
5/01/2018,4
6/01/2018,0
7/01/2018,2
8/01/2018,4
9/01/2018,1
10/01/2018,6
11/01/2018,2
12/01/2018,7

Lagged 3 variable
2/01/2018,NA
3/01/2018,NA
4/01/2018,NA
5/01/2018,6
6/01/2018,4
7/01/2018,0
8/01/2018,2
9/01/2018,4
10/01/2018,1
11/01/2018,6
12/01/2018,2
13/01/2018,7

and so on

I tried using the shift function and various other functions. Wtih most of them that worked for me, the lagged variables finished at the last date of the original variable. In other words, the length of the lagged variable is the same as that of the original variable.

What I am looking for the new lagged variable to be shifted down by the 'kth' lag and the data series to be extended by 'k' elements including the index.

The reason I need this is to be able to compute the value of the dependent variable using the regression coeffficients and the corresponding lagged variable value beyond the in-sample period

y1 <- Lag(ciresL1_usage_1601_1612, shift = 1)
head(y1)
2016-01-02 2016-01-03 2016-01-04 2016-01-05 2016-01-06 2016-01-07 
        NA  -5171.051  -6079.887  -3687.227  -3229.453  -2110.368 

y2 <- Lag(ciresL1_usage_1601_1612, shift = 2)
head(y2)
2016-01-02 2016-01-03 2016-01-04 2016-01-05 2016-01-06 2016-01-07 
        NA         NA  -5171.051  -6079.887  -3687.227  -3229.453 
tail(y2)
2016-12-26 2016-12-27 2016-12-28 2016-12-29 2016-12-30 2016-12-31 
 -2316.039  -2671.185  -4100.793  -2043.020  -1147.798   1111.674 

tail(ciresL1_usage_1601_1612)
2016-12-26 2016-12-27 2016-12-28 2016-12-29 2016-12-30 2016-12-31 
 -4100.793  -2043.020  -1147.798   1111.674   3498.729   2438.739 

Is there a way to do it relatively easily. I know I can do it by looping and adding 'k' rows in a new vector and reloading the data in to this new vector appropriately shifting the data values in the new vector but I don't want to use that method unless I have to. I am quietly confident that there has to be a better way to do it than this!

By the way, the object is a zoo object with daily dates as the index.

Best regards

Deepak

Upvotes: 1

Views: 477

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269491

Convert the input zoo object to zooreg and then use lag.zooreg like this:

library(zoo)

# test input
z <- zoo(1:10, as.Date("2008-01-01") + 0:9)

zr <- as.zooreg(z)
lag(zr, -(0:3))

giving:

           lag0 lag-1 lag-2 lag-3
2008-01-01    1    NA    NA    NA
2008-01-02    2     1    NA    NA
2008-01-03    3     2     1    NA
2008-01-04    4     3     2     1
2008-01-05    5     4     3     2
2008-01-06    6     5     4     3
2008-01-07    7     6     5     4
2008-01-08    8     7     6     5
2008-01-09    9     8     7     6
2008-01-10   10     9     8     7
2008-01-11   NA    10     9     8
2008-01-12   NA    NA    10     9
2008-01-13   NA    NA    NA    10

Upvotes: 2

Related Questions