我想出了一个向后转换的方法(即。预测过去)与时间序列。现在,我只是在R中苦苦挣扎编程。
我想反转时间序列数据,这样我就可以预测过去了。我该怎么做呢?
假设原始时间序列对象如下所示:
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我想让它看起来像这样的‘回溯’:
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请注意,我没有忘记更改年份-我基本上是在镜像/反转数据并保留年份,然后进行预测。
我希望这可以在R中完成?或者我应该导出并以某种方式在Excel中完成?
发布于 2011-08-08 02:34:23
试试这个:
tt <- ts(1:24, start = 2008, freq = 12)
tt[] <- rev(tt)已添加。这也是有效的,并且不修改tt:
replace(tt, TRUE, rev(tt))发布于 2011-08-08 02:10:46
您可以将矩阵强制转换为向量,将其反转,然后再次将其转换为矩阵。下面是一个例子:
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发布于 2014-06-12 13:59:51
我在http://www.r-bloggers.com/backcasting-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]))https://stackoverflow.com/questions/6974653
复制相似问题