Charlie
Charlie

Reputation: 2851

do.call with cbind and lags of a variable

I want to create a function that produces a matrix containing several lags of a variable. A simple example that works is

a <- ts(1:10)
cbind(a, lag(a, -1))

To do this for multiple lags, I have

lagger <- function(var, lags) {

  ### Create list of lags
  lagged <- lapply(1:lags, function(x){
    lag(var, -x)
  })

  ### Join lags together
  do.call(cbind, list(var, lagged))
}

Using the above example gives unexpected results;

lagger(a, 1)

gives a length 20 list with the original time series broken out into separate list slots and the final 10 each being a replication of the lagged series.

Any suggestions to getting this working? Thanks!

Upvotes: 1

Views: 415

Answers (3)

G. Grothendieck
G. Grothendieck

Reputation: 270448

This gives a lag of 0 and of 1.

library(zoo)
a <- ts(11:13)
lags <- -(0:1)

a.lag <- as.ts(lag(as.zoo(a), lags))

Now a.lag is this:

> a.lag
Time Series:
Start = 1 
End = 4 
Frequency = 1 
  lag0 lag-1
1   11    NA
2   12    11
3   13    12
4   NA    13

If you don't want the NA entries then use: as.ts(na.omit(lag(as.zoo(a), lags))) .

Upvotes: 3

Jilber Urbina
Jilber Urbina

Reputation: 61214

Based on @Joshua Ulrich answer.

I thinkd embed is the correct answer but you get the vectors in the other way around. I mean using embed you'll get the lagged series not in the proper order, see the following

lagged <- embed(a,4)
colnames(lagged) <- paste('t', 3:0, sep='-')
lagged 
    t-3 t-2 t-1 t-0
[1,]   4   3   2   1
[2,]   5   4   3   2
[3,]   6   5   4   3
[4,]   7   6   5   4
[5,]   8   7   6   5
[6,]   9   8   7   6
[7,]  10   9   8   7

this gives the correct answer to you but not in the correct order, since the lags are in descending order.

But it you reorder just like this:

lagged_OK <- lagged[,ncol(lagged):1]
colnames(lagged_OK) <- paste('t', 0:3, sep='-')
lagged_OK  
    lag.0 lag.1 lag.2 lag.3
[1,]     1     2     3     4
[2,]     2     3     4     5
[3,]     3     4     5     6
[4,]     4     5     6     7
[5,]     5     6     7     8
[6,]     6     7     8     9
[7,]     7     8     9    10

Then, you get the right lagged matrix.

I add colnames only for explanation purpose, you can just do:

embed(a,4)[ ,4:1] 

If you really want a lagger function, try this

lagger <- function(x, lag=1){
      lag <- lag+1
      Lagged <- embed(x,lag)[ ,lag:1]
      colnames(Lagged) <- paste('lag', 0:(lag-1), sep='.')
      return(Lagged)
        }


lagger(a, 4)
     lag.0 lag.1 lag.2 lag.3 lag.4
[1,]     1     2     3     4     5
[2,]     2     3     4     5     6
[3,]     3     4     5     6     7
[4,]     4     5     6     7     8
[5,]     5     6     7     8     9
[6,]     6     7     8     9    10

lagger(a, 1)
      lag.0 lag.1
 [1,]     1     2
 [2,]     2     3
 [3,]     3     4
 [4,]     4     5
 [5,]     5     6
 [6,]     6     7
 [7,]     7     8
 [8,]     8     9
 [9,]     9    10

Upvotes: 2

Joshua Ulrich
Joshua Ulrich

Reputation: 176748

I'm not sure what's wrong with your function, but you can probably use embed instead.

> embed(a,4)
     [,1] [,2] [,3] [,4]
[1,]    4    3    2    1
[2,]    5    4    3    2
[3,]    6    5    4    3
[4,]    7    6    5    4
[5,]    8    7    6    5
[6,]    9    8    7    6
[7,]   10    9    8    7

Upvotes: 1

Related Questions