Rearranging a data set to create one with lags (Past obervations)

Viewed 33

Let's say the original data set has 100 observations

X1, X2, X3, --- , X100

Now I want to rearrange the data as follows and create a new data set

Current_State Lag1 Lag2 Lag3 Lag4 Lag5

X100 X99 X98 X97 X96 X95

X99 X98 X97 X96 X95 X94

X98 X97 X96 X95 X94 X93

X97 X96 X95 X94 X93 X92

. . .

X6 X5 X4 X3 X2 X1

Is there anyway to do this simple manipulation in r or matlab rather than doing a series of for loops.

2 Answers

Matlab

You can build an index with implicit expansion (broadcasting) to generate the desired result:

x = [10 34 22 5 17 48 56 6 19]; % data
L = 5; % number of lags
result = x((end-L:-1:1).'+(L:-1:0));

This gives a result in which

  • the row defines the current sample, and
  • each column is a lag.

Example:

>> x
x =
    10    34    22     5    17    48    56     6    19
>> result
result =
    19     6    56    48    17     5
     6    56    48    17     5    22
    56    48    17     5    22    34
    48    17     5    22    34    10

You need the equivalent of a loop in any language, but it can be disguised and abbreviated. In R:

x <- 1:100 # Example of the data
k <- 5     # Specify number of lags
#
# Here is the computation.  It creates each column successively, putting
# them (automatically) into a matrix.  The rest is just cosmetic--turning
# the matrix into a dataframe and naming its variables.
#
y <- rev(x)
X <- as.data.frame(sapply(0:k, function(i) y[1:(length(y)-k)+i]))
colnames(X) <- paste0("Lag", 0:k)
Related