创建时间序列的未来值矩阵
我在 R 中有一个时间序列。我想构造一个矩阵,其中每一行是当前观察值,每一列代表从该点开始的该序列的未来值。例如:
x <- ts(1:25,start=2000, frequency=12)
maxHorizon <- 12
freq <- frequency(x)
st <- tsp(x)[1]-(1/freq)
actuals <- matrix(NA,length(x)-1,maxHorizon)
for(i in seq(1, (length(x)-1))) {
xnext <- window(x, start=st+(i+1)/freq, end=st+(i+maxHorizon)/freq)
actuals[i,1:length(xnext)] <- xnext
}
actuals
在本例中,我们有一个包含 25 个观测值的时间序列,因此我们的最终矩阵有 24 行。从第 1 行开始,接下来的 12 个观测值是 2-13。第 2 行是 3-13,等等。在矩阵的末尾,我们用 NA 值填充它。
> x
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2000 1 2 3 4 5 6 7 8 9 10 11 12
2001 13 14 15 16 17 18 19 20 21 22 23 24
2002 25
> actuals
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 2 3 4 5 6 7 8 9 10 11 12 13
[2,] 3 4 5 6 7 8 9 10 11 12 13 14
[3,] 4 5 6 7 8 9 10 11 12 13 14 15
[4,] 5 6 7 8 9 10 11 12 13 14 15 16
[5,] 6 7 8 9 10 11 12 13 14 15 16 17
[6,] 7 8 9 10 11 12 13 14 15 16 17 18
[7,] 8 9 10 11 12 13 14 15 16 17 18 19
[8,] 9 10 11 12 13 14 15 16 17 18 19 20
[9,] 10 11 12 13 14 15 16 17 18 19 20 21
[10,] 11 12 13 14 15 16 17 18 19 20 21 22
[11,] 12 13 14 15 16 17 18 19 20 21 22 23
[12,] 13 14 15 16 17 18 19 20 21 22 23 24
[13,] 14 15 16 17 18 19 20 21 22 23 24 25
[14,] 15 16 17 18 19 20 21 22 23 24 25 NA
[15,] 16 17 18 19 20 21 22 23 24 25 NA NA
[16,] 17 18 19 20 21 22 23 24 25 NA NA NA
[17,] 18 19 20 21 22 23 24 25 NA NA NA NA
[18,] 19 20 21 22 23 24 25 NA NA NA NA NA
[19,] 20 21 22 23 24 25 NA NA NA NA NA NA
[20,] 21 22 23 24 25 NA NA NA NA NA NA NA
[21,] 22 23 24 25 NA NA NA NA NA NA NA NA
[22,] 23 24 25 NA NA NA NA NA NA NA NA NA
[23,] 24 25 NA NA NA NA NA NA NA NA NA NA
[24,] 25 NA NA NA NA NA NA NA NA NA NA NA
如何在不使用丑陋的 for
循环的情况下做到这一点?
编辑:如果数据以其他格式返回,例如 data.frame 甚至行列表,那就可以了。
编辑:这里有一些代码来比较我们迄今为止拥有的 3 个函数:
rm(list = ls(all = TRUE))
zach1 <- function(x,maxHorizon) {
freq <- frequency(x)
st <- tsp(x)[1]-(1/freq)
actuals <- matrix(NA,length(x)-1,maxHorizon)
for(i in seq(1, (length(x)-1))) {
xnext <- window(x, start=st+(i+1)/freq, end=st+(i+maxHorizon)/freq)
actuals[i,1:length(xnext)] <- xnext
}
actuals
}
zach2 <- function(x,maxHorizon) {
t(apply(embed(c(x,rep(NA,maxHorizon)),maxHorizon),1,rev))[2:length(x),]
}
josh1 <- function(x,maxHorizon) {
actuals <- outer(seq_along(x), seq_len(maxHorizon), FUN="+")
actuals[actuals > length(x)] <- NA
actuals <- actuals[1:(length(x)-1),]
actuals <- apply(actuals,2,function(a) x[a])
actuals
}
x <- ts(rnorm(10000),start=2000, frequency=12)
> system.time(actuals1 <- zach1(x, 6))
user system elapsed
11.81 0.00 11.93
> system.time(actuals2 <- zach2(x, 6))
user system elapsed
0.15 0.00 0.16
> system.time(actuals3 <- josh1(x, 6))
user system elapsed
0 0 0
> all.equal(actuals1,actuals2)
[1] TRUE
> all.equal(actuals1,actuals3)
[1] TRUE
I have a time series in R. I want to construct a matrix, where each row is the current observation, and each column represent the future values of that series, starting from that point. eg:
x <- ts(1:25,start=2000, frequency=12)
maxHorizon <- 12
freq <- frequency(x)
st <- tsp(x)[1]-(1/freq)
actuals <- matrix(NA,length(x)-1,maxHorizon)
for(i in seq(1, (length(x)-1))) {
xnext <- window(x, start=st+(i+1)/freq, end=st+(i+maxHorizon)/freq)
actuals[i,1:length(xnext)] <- xnext
}
actuals
In this case, we've got a time series with 25 observations, so our final matrix has 24 rows. Starting with row 1, the next 12 ovbservations are 2-13. Row 2 is 3-13, etc. At the end of the matrix we fill it in with NA values.
> x
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2000 1 2 3 4 5 6 7 8 9 10 11 12
2001 13 14 15 16 17 18 19 20 21 22 23 24
2002 25
> actuals
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 2 3 4 5 6 7 8 9 10 11 12 13
[2,] 3 4 5 6 7 8 9 10 11 12 13 14
[3,] 4 5 6 7 8 9 10 11 12 13 14 15
[4,] 5 6 7 8 9 10 11 12 13 14 15 16
[5,] 6 7 8 9 10 11 12 13 14 15 16 17
[6,] 7 8 9 10 11 12 13 14 15 16 17 18
[7,] 8 9 10 11 12 13 14 15 16 17 18 19
[8,] 9 10 11 12 13 14 15 16 17 18 19 20
[9,] 10 11 12 13 14 15 16 17 18 19 20 21
[10,] 11 12 13 14 15 16 17 18 19 20 21 22
[11,] 12 13 14 15 16 17 18 19 20 21 22 23
[12,] 13 14 15 16 17 18 19 20 21 22 23 24
[13,] 14 15 16 17 18 19 20 21 22 23 24 25
[14,] 15 16 17 18 19 20 21 22 23 24 25 NA
[15,] 16 17 18 19 20 21 22 23 24 25 NA NA
[16,] 17 18 19 20 21 22 23 24 25 NA NA NA
[17,] 18 19 20 21 22 23 24 25 NA NA NA NA
[18,] 19 20 21 22 23 24 25 NA NA NA NA NA
[19,] 20 21 22 23 24 25 NA NA NA NA NA NA
[20,] 21 22 23 24 25 NA NA NA NA NA NA NA
[21,] 22 23 24 25 NA NA NA NA NA NA NA NA
[22,] 23 24 25 NA NA NA NA NA NA NA NA NA
[23,] 24 25 NA NA NA NA NA NA NA NA NA NA
[24,] 25 NA NA NA NA NA NA NA NA NA NA NA
How can I do this without using the ugly for
loop?
edit: it would be ok if the data were returned in another format, such as a data.frame or even a list of rows.
edit: here's some code to compare the 3 functions we have so far:
rm(list = ls(all = TRUE))
zach1 <- function(x,maxHorizon) {
freq <- frequency(x)
st <- tsp(x)[1]-(1/freq)
actuals <- matrix(NA,length(x)-1,maxHorizon)
for(i in seq(1, (length(x)-1))) {
xnext <- window(x, start=st+(i+1)/freq, end=st+(i+maxHorizon)/freq)
actuals[i,1:length(xnext)] <- xnext
}
actuals
}
zach2 <- function(x,maxHorizon) {
t(apply(embed(c(x,rep(NA,maxHorizon)),maxHorizon),1,rev))[2:length(x),]
}
josh1 <- function(x,maxHorizon) {
actuals <- outer(seq_along(x), seq_len(maxHorizon), FUN="+")
actuals[actuals > length(x)] <- NA
actuals <- actuals[1:(length(x)-1),]
actuals <- apply(actuals,2,function(a) x[a])
actuals
}
x <- ts(rnorm(10000),start=2000, frequency=12)
> system.time(actuals1 <- zach1(x, 6))
user system elapsed
11.81 0.00 11.93
> system.time(actuals2 <- zach2(x, 6))
user system elapsed
0.15 0.00 0.16
> system.time(actuals3 <- josh1(x, 6))
user system elapsed
0 0 0
> all.equal(actuals1,actuals2)
[1] TRUE
> all.equal(actuals1,actuals3)
[1] TRUE
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:要使用
x
的元素(而不是它们的索引)填充矩阵,您可以传递outer()
一个“匿名函数”你自己的设计。这应该可以解决问题:EDIT: To fill the matrix with the elements of
x
(rather than their indices), you can passouter()
an 'anonymous function' of your own devising. This should do the trick:这摆脱了 for 循环,但我不确定它是否更优雅:
t(apply(embed(c(x,rep(NA,maxHorizon)),maxHorizon),1,rev))[2:length(x),]
编辑:不过速度要快得多。
This gets rid of the
for
loop, but I'm not sure if it's any more elegant:t(apply(embed(c(x,rep(NA,maxHorizon)),maxHorizon),1,rev))[2:length(x),]
edit: it's a lot faster though.