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),T(2),T(3),T(4)... Tn此外我有2个其他时间相关参数P(1),P2(2),P(3) )...和R(1),R(2),R(3)...因此数据看起来像
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..
在该特定示例中,最后一列是模型目标,前九列是预测变量。我试图用T,P和R的前三个时间点来预测T.
How can I do this in R? I am new to R and really struggling trying to work with the reshape package?
我怎么能在R中这样做?我是R的新手并且正在努力尝试使用重塑包装?
For what it is worth, I actually have 10 different predictors not 2 . Thank you so much!
对于它的价值,我实际上有10个不同的预测因子而不是2。非常感谢!
1 个解决方案
#1
1
This can be done with zoo's lag
function. Also we use mixedsort
from gtools:
这可以通过动物园的滞后功能来完成。我们也使用gtools的mixedsort:
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.
如果zL列的顺序不重要,则可以省略以##结尾的行。
#1
1
This can be done with zoo's lag
function. Also we use mixedsort
from gtools:
这可以通过动物园的滞后功能来完成。我们也使用gtools的mixedsort:
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.
如果zL列的顺序不重要,则可以省略以##结尾的行。