I am using the forecast package in R for some basic time series forecasting across a dozen business metrics.
我正在使用R中的预测包来预测十几个业务指标的基本时间序列。
I typically set quarterly goals based on data over the last several years.
我通常根据过去几年的数据设定季度目标。
During the course of the quarter I get actual data and re-forecast to see if there has been a significant shift that would make me revise the expected goals. I only want to revise the goals if the mean values are statistically different or if the trend has shifted meaningful - something like a control chart.
在本季度的过程中,我获得了实际数据并重新预测,看看是否有重大转变会让我修改预期目标。我只想修改目标,如果平均值在统计上有所不同,或者趋势是否有意义 - 就像控制图一样。
Ideally I want to do this automatically in the script that I'm running.
理想情况下,我想在我正在运行的脚本中自动执行此操作。
For example lets say I have monthly data for last year and I forecast out a year
例如,假设我有去年的月度数据,我预测一年
library(forecast)
StartingData <- (1:12)+rnorm(1:12)
forecast(ts(StartingData,start=c(2011,1), frequency =12),h=12)
Then I get the next three months data, which happens to be '10' instead of continuing the linear trend.
然后我得到接下来三个月的数据,恰好是'10',而不是继续线性趋势。
StartingData[13:15] <- 10
forecast(ts(StartingData,start=c(2011,1), frequency =12),h=12)
What I'd like to do is access the forecast data to make this comparison by the time value listed in the output to compare my new forecast to my old forecast. However I can't find an object associated with the row's time value.
我想要做的是访问预测数据,通过输出中列出的时间值进行比较,以将我的新预测与我的旧预测进行比较。但是我找不到与行的时间值相关联的对象。
Is there a way to access those time values to help me match the old forecast with the new forecast? Or do I need to write code to figure out how much more data I have in my new data set than my old data set?
有没有办法访问这些时间值,以帮助我将旧预测与新预测相匹配?或者我是否需要编写代码来确定我的新数据集中的数据比旧数据集多多少?
Thanks-
谢谢-
1 个解决方案
#1
1
This is one way to do it. If you want old and new side by side, then you can recast the data.
这是一种方法。如果你想要新旧并排,那么你可以重铸数据。
library(forecast)
StartingData <- (1:12)+rnorm(1:12)
d1=data.frame(forecast(ts(StartingData,start=c(2011,1), frequency =12),h=12))
d1$times=row.names(d1)
d1$fcast='old'
StartingData[13:15] <- 10
d2=data.frame(forecast(ts(StartingData,start=c(2011,1), frequency =12),h=12))
d2$times=row.names(d2)
d2$fcast='new'
combined=rbind(d1,d2)
row.names(combined)=NULL
combined
> combined
Point.Forecast Lo.80 Hi.80 Lo.95 Hi.95 times fcast
1 12.58567 11.652976 13.51837 11.159237 14.01211 Jan 2012 old
2 13.53736 12.604661 14.47005 12.110921 14.96379 Feb 2012 old
3 14.48904 13.556345 15.42174 13.062605 15.91548 Mar 2012 old
4 15.44073 14.508029 16.37342 14.014289 16.86716 Apr 2012 old
5 16.39241 15.459713 17.32511 14.965973 17.81885 May 2012 old
6 17.34409 16.411397 18.27679 15.917657 18.77053 Jun 2012 old
7 18.29578 17.363081 19.22848 16.869341 19.72222 Jul 2012 old
8 19.24746 18.314765 20.18016 17.821024 20.67390 Aug 2012 old
9 20.19915 19.266449 21.13185 18.772708 21.62559 Sep 2012 old
10 21.15083 20.218133 22.08353 19.724391 22.57727 Oct 2012 old
11 22.10252 21.169816 23.03522 20.676075 23.52896 Nov 2012 old
12 23.05420 22.121500 23.98690 21.627758 24.48064 Dec 2012 old
13 11.06443 8.716179 13.41269 7.473087 14.65578 Apr 2012 new
14 11.33021 8.925497 13.73491 7.652521 15.00789 May 2012 new
15 11.56613 9.111298 14.02095 7.811791 15.32046 Jun 2012 new
16 11.77555 9.276224 14.27488 7.953161 15.59794 Jul 2012 new
17 11.96145 9.422619 14.50028 8.078643 15.84426 Aug 2012 new
18 12.12647 9.552565 14.70038 8.190020 16.06293 Sep 2012 new
19 12.27296 9.667908 14.87802 8.288876 16.25705 Oct 2012 new
20 12.40300 9.770290 15.03571 8.376618 16.42938 Nov 2012 new
21 12.51843 9.861164 15.17569 8.454494 16.58236 Dec 2012 new
22 12.62089 9.941825 15.29996 8.523612 16.71817 Jan 2013 new
23 12.71185 10.013418 15.41028 8.584955 16.83874 Feb 2013 new
24 12.79259 10.076963 15.50822 8.639396 16.94579 Mar 2013 new
>
#1
1
This is one way to do it. If you want old and new side by side, then you can recast the data.
这是一种方法。如果你想要新旧并排,那么你可以重铸数据。
library(forecast)
StartingData <- (1:12)+rnorm(1:12)
d1=data.frame(forecast(ts(StartingData,start=c(2011,1), frequency =12),h=12))
d1$times=row.names(d1)
d1$fcast='old'
StartingData[13:15] <- 10
d2=data.frame(forecast(ts(StartingData,start=c(2011,1), frequency =12),h=12))
d2$times=row.names(d2)
d2$fcast='new'
combined=rbind(d1,d2)
row.names(combined)=NULL
combined
> combined
Point.Forecast Lo.80 Hi.80 Lo.95 Hi.95 times fcast
1 12.58567 11.652976 13.51837 11.159237 14.01211 Jan 2012 old
2 13.53736 12.604661 14.47005 12.110921 14.96379 Feb 2012 old
3 14.48904 13.556345 15.42174 13.062605 15.91548 Mar 2012 old
4 15.44073 14.508029 16.37342 14.014289 16.86716 Apr 2012 old
5 16.39241 15.459713 17.32511 14.965973 17.81885 May 2012 old
6 17.34409 16.411397 18.27679 15.917657 18.77053 Jun 2012 old
7 18.29578 17.363081 19.22848 16.869341 19.72222 Jul 2012 old
8 19.24746 18.314765 20.18016 17.821024 20.67390 Aug 2012 old
9 20.19915 19.266449 21.13185 18.772708 21.62559 Sep 2012 old
10 21.15083 20.218133 22.08353 19.724391 22.57727 Oct 2012 old
11 22.10252 21.169816 23.03522 20.676075 23.52896 Nov 2012 old
12 23.05420 22.121500 23.98690 21.627758 24.48064 Dec 2012 old
13 11.06443 8.716179 13.41269 7.473087 14.65578 Apr 2012 new
14 11.33021 8.925497 13.73491 7.652521 15.00789 May 2012 new
15 11.56613 9.111298 14.02095 7.811791 15.32046 Jun 2012 new
16 11.77555 9.276224 14.27488 7.953161 15.59794 Jul 2012 new
17 11.96145 9.422619 14.50028 8.078643 15.84426 Aug 2012 new
18 12.12647 9.552565 14.70038 8.190020 16.06293 Sep 2012 new
19 12.27296 9.667908 14.87802 8.288876 16.25705 Oct 2012 new
20 12.40300 9.770290 15.03571 8.376618 16.42938 Nov 2012 new
21 12.51843 9.861164 15.17569 8.454494 16.58236 Dec 2012 new
22 12.62089 9.941825 15.29996 8.523612 16.71817 Jan 2013 new
23 12.71185 10.013418 15.41028 8.584955 16.83874 Feb 2013 new
24 12.79259 10.076963 15.50822 8.639396 16.94579 Mar 2013 new
>