I figured out a way to backcast (ie. predicting the past) with a time series. Now I'm just struggling with the programming in R.
我想出了一个倒投的办法。预测过去)用一个时间序列。现在我只是在和R的编程斗争。
I would like to reverse the time series data so that I can forecast the past. How do I do this?
我想要逆转时间序列数据,以便我可以预测过去。我该怎么做呢?
Say the original time series object looks like this:
假设原始的时间序列对象是这样的:
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2008 116 99 115 101 112 120 120 110 143 136 147 142
2009 117 114 133 134 139 147 147 131 125 143 136 129
I want it to look like this for the 'backcasting':
我想让它看起来像这样
Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
2008 129 136 143 125 131 147 147 139 134 133 114 117
2009 142 147 136 143 110 120 120 112 101 115 99 116
Note, I didn't forget to change the years - I am basically mirroring/reversing the data and keeping the years, then going to forecast.
注意,我并没有忘记改变这些年来——我基本上是在对数据进行镜像和转换,并保持这些年,然后进行预测。
I hope this can be done in R? Or should I export and do it in Excel somehow?
我希望这能在R中完成?或者我应该导出并在Excel中做?
3 个解决方案
#1
11
Try this:
试试这个:
tt <- ts(1:24, start = 2008, freq = 12)
tt[] <- rev(tt)
ADDED. This also works and does not modify tt
:
补充道。这也适用,并且不修改tt:
replace(tt, TRUE, rev(tt))
#2
4
You can just coerce the matrix to a vector, reverse it, and make it a matrix again. Here's an example:
你可以把这个矩阵强制到一个向量上,把它反过来,再把它变成一个矩阵。这里有一个例子:
mat <- matrix(seq(24),nrow=2,byrow=TRUE)
> mat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 13 14 15 16 17 18 19 20 21 22 23 24
> matrix( rev(mat), nrow=nrow(mat) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 24 23 22 21 20 19 18 17 16 15 14 13
[2,] 12 11 10 9 8 7 6 5 4 3 2 1
#3
3
I found this post of Hyndman under http://www.r-bloggers.com/backcasting-in-r/ and am basically pasting in his solution, which in my opinion provids a complete answer to you question.
我在http://www.r-bloggers.com/backcast -in-r/下找到了Hyndman的这篇文章,我基本上把它粘贴在了他的解决方案中,在我看来,这为你的问题提供了一个完整的答案。
library(forecast)
x <- WWWusage
h <- 20
f <- frequency(x)
# Reverse time
revx <- ts(rev(x), frequency=f)
# Forecast
fc <- forecast(auto.arima(revx), h)
plot(fc)
# Reverse time again
fc$mean <- ts(rev(fc$mean),end=tsp(x)[1] - 1/f, frequency=f)
fc$upper <- fc$upper[h:1,]
fc$lower <- fc$lower[h:1,]
fc$x <- x
# Plot result
plot(fc, xlim=c(tsp(x)[1]-h/f, tsp(x)[2]))
#1
11
Try this:
试试这个:
tt <- ts(1:24, start = 2008, freq = 12)
tt[] <- rev(tt)
ADDED. This also works and does not modify tt
:
补充道。这也适用,并且不修改tt:
replace(tt, TRUE, rev(tt))
#2
4
You can just coerce the matrix to a vector, reverse it, and make it a matrix again. Here's an example:
你可以把这个矩阵强制到一个向量上,把它反过来,再把它变成一个矩阵。这里有一个例子:
mat <- matrix(seq(24),nrow=2,byrow=TRUE)
> mat
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 1 2 3 4 5 6 7 8 9 10 11 12
[2,] 13 14 15 16 17 18 19 20 21 22 23 24
> matrix( rev(mat), nrow=nrow(mat) )
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12]
[1,] 24 23 22 21 20 19 18 17 16 15 14 13
[2,] 12 11 10 9 8 7 6 5 4 3 2 1
#3
3
I found this post of Hyndman under http://www.r-bloggers.com/backcasting-in-r/ and am basically pasting in his solution, which in my opinion provids a complete answer to you question.
我在http://www.r-bloggers.com/backcast -in-r/下找到了Hyndman的这篇文章,我基本上把它粘贴在了他的解决方案中,在我看来,这为你的问题提供了一个完整的答案。
library(forecast)
x <- WWWusage
h <- 20
f <- frequency(x)
# Reverse time
revx <- ts(rev(x), frequency=f)
# Forecast
fc <- forecast(auto.arima(revx), h)
plot(fc)
# Reverse time again
fc$mean <- ts(rev(fc$mean),end=tsp(x)[1] - 1/f, frequency=f)
fc$upper <- fc$upper[h:1,]
fc$lower <- fc$lower[h:1,]
fc$x <- x
# Plot result
plot(fc, xlim=c(tsp(x)[1]-h/f, tsp(x)[2]))