blast00
blast00

Reputation: 559

Reshape multivariate time series in R

I have time series T(1), T(2), T(3), T(4)...Tn in addition I have 2 other time-dependent parameters P(1), P2(2), P(3)... and R(1),R(2),R(3)... Therefore the data looks like

T(1) P(1) R(1)
T(2) P(2) R(2)
T(3) P(3) R(3)
T(4) P(4) R(4)
T(5) P(5) R(5)
T(6) P(6) R(6)
T(7) P(7) R(7)
T(8) P(8) R(8)
...

I want to reshape the data as follows: 
T(1) T(2) T(3) P(1) P(2) P(3) R(1) R(2) R(3) T(4)
T(2) T(3) T(4) P(2) P(3) P(4) R(2) R(3) R(4) T(5)
T(3) T(4) T(5) P(3) P(4) P(5) R(3) R(4) R(5) T(6)
…

In that particular example the last column is the model target and first nine columns are predictors. I am trying to predict T using the three previous time points of T, P and R..

How can I do this in R? I am new to R and really struggling trying to work with the reshape package?

For what it is worth, I actually have 10 different predictors not 2 . Thank you so much!

Upvotes: 0

Views: 159

Answers (1)

G. Grothendieck
G. Grothendieck

Reputation: 269481

This can be done with zoo's lag function. Also we use mixedsort from gtools:

library(zoo)
library(gtools) ##
DF <- data.frame(T = 1:10, P = 11:20, R = 21:30) # test data

z <- zoo(DF)
zL <- cbind(lag(z, 0:2), Y = lag(z$T, 3, na.pad = TRUE))
zL <- na.omit(zL)

# reorder columns
names(zL) <- sub("T", "A", names(zL)) ##
zL <- zL[, mixedsort(names(zL))] ##
names(zL) <- sub("A", "T", names(zL)) ##

mL <- coredata(zL) # matrix

giving:

> mL
     T.lag0 T.lag1 T.lag2 P.lag0 P.lag1 P.lag2 R.lag0 R.lag1 R.lag2  Y
[1,]      1      2      3     11     12     13     21     22     23  4
[2,]      2      3      4     12     13     14     22     23     24  5
[3,]      3      4      5     13     14     15     23     24     25  6
[4,]      4      5      6     14     15     16     24     25     26  7
[5,]      5      6      7     15     16     17     25     26     27  8
[6,]      6      7      8     16     17     18     26     27     28  9
[7,]      7      8      9     17     18     19     27     28     29 10

If the order of the columns of zL is not important then the lines ending with ## can be omitted.

Upvotes: 1

Related Questions