0

我一直在使用 R 中的预测包,但发现很难将我自己的每日时间序列加载到 ts 对象中,然后将其与预测算法一起使用。我改为使用 zoo 来创建我的每日时间序列对象,但我无法将其直接传递给 R Forecast 包中的预测算法。

任何正确方向的帮助将不胜感激。我对此感到非常困惑。

谢谢


嗨这是一些示例代码。使用简单的季度数据集,我可以进行一些预测,但使用每日数据集,我无法使其正常工作。非常感谢

require("forecast")
require("fpp")

# Example from Forecasting Principles and Practice
# http://otexts.com/fpp/2/5/

#beer2 <- window(ausbeer,start=1992,end=2006-.1)

#start with a really small dataset (only 6 data points)
beer2 <- window(ausbeer,start=2006,end=2006-.1)
print(beer2)

beerfit1 <- meanf(beer2,h=11)
beerfit2 <- rwf(beer2,h=11)
beerfit3 <- snaive(beer2,h=11)

plot(beerfit1, plot.conf=FALSE, main="Forecasts for quarterly beer production")
lines(beerfit2$mean,col=2)
lines(beerfit3$mean,col=3)
lines(ausbeer)
legend("topright", lty=1, col=c(4,2,3), legend=c("Mean method","Naive method","Seasonal     naive method"))

beer3 <- window(ausbeer, start=2006)
accuracy(beerfit1, beer3)
accuracy(beerfit2, beer3)
accuracy(beerfit3, beer3)

#now make a really small daily dataset (Each day for two weeks)
forecast_datesequence = seq(from=as.Date("2013-05-06"), to=as.Date("2013-05-19"), by=1)
vals <- c(100,150,300,150,100,45,25,100,150,300,150,100,45,25)

dailyzoo_ts <- zoo(vals, forecast_datesequence)
print(daily_ts)

dailyfit1 <- meanf(coredata(dailyzoo_ts),h=7)
dailyfit2 <- rwf(coredata(dailyzoo_ts),h=7)
dailyfit3 <- snaive(coredata(dailyzoo_ts),h=7)

plot(dailyfit1, plot.conf=FALSE, main="Daily Data Over 2 Week Period")
lines(dailyfit2$mean,col=2)
lines(dailyfit3$mean,col=3)
lines(dailyzoo_ts)
legend("topright", lty=1, col=c(4,2,3), legend=c("Mean method","Naive method","Seasonal naive method"))

这是一个更新的 R 代码,但仍然无法正常工作

#now make a really small daily dataset (Each day for two weeks)
forecast_datesequence = seq(from=as.Date("2013-05-06"), to=as.Date("2013-05-19"), by=1)
vals <- c(100,150,300,150,100,45,25,100,150,300,150,100,45,25)

dailyzoo_ts <- zoo(vals, forecast_datesequence)
print(daily_ts)

z <- zoo(coredata(dailyzoo_ts), 1:14/7)
print(z)
plot(forecast(z))
#stl(z)

dailyfit1 <- meanf(z,h=7)
dailyfit2 <- rwf(z,h=7)
dailyfit3 <- snaive(z,h=7)

plot(dailyfit1, plot.conf=FALSE, main="Daily Data Over 2 Week Period")
lines(dailyfit2$mean,col=2)
lines(dailyfit3$mean,col=3)
lines(z)
legend("topright", lty=1, col=c(4,2,3), legend=c("Mean method","Naive method","Seasonal naive method"))

非常感谢

4

1 回答 1

1

forecast要求周期为 1 个时间单位,所以试试这个:

>     z <- zoo(coredata(dailyzoo_ts), 0:15/7)
>     plot(forecast(z))
>     meanf(z)
   Point Forecast    Lo 80    Hi 80     Lo 95    Hi 95
1         124.375 11.38063 237.3694 -55.27649 304.0265
2         124.375 11.38063 237.3694 -55.27649 304.0265
3         124.375 11.38063 237.3694 -55.27649 304.0265
4         124.375 11.38063 237.3694 -55.27649 304.0265
5         124.375 11.38063 237.3694 -55.27649 304.0265
6         124.375 11.38063 237.3694 -55.27649 304.0265
7         124.375 11.38063 237.3694 -55.27649 304.0265
8         124.375 11.38063 237.3694 -55.27649 304.0265
9         124.375 11.38063 237.3694 -55.27649 304.0265
10        124.375 11.38063 237.3694 -55.27649 304.0265
>     rwf(z)
         Point Forecast      Lo 80    Hi 80      Lo 95    Hi 95
16.14286            150   31.13735 268.8627  -31.78474 331.7847
16.28571            150  -18.09718 318.0972 -107.08245 407.0824
16.42857            150  -55.87615 355.8762 -164.86041 464.8604
16.57143            150  -87.72531 387.7253 -213.56948 513.5695
16.71429            150 -115.78497 415.7850 -256.48304 556.4830
16.85714            150 -141.15285 441.1529 -295.27986 595.2799
17.00000            150 -164.48102 464.4810 -330.95722 630.9572
17.14286            150 -186.19435 486.1944 -364.16489 664.1649
17.28571            150 -206.58796 506.5880 -395.35422 695.3542
17.42857            150 -225.87671 525.8767 -424.85383 724.8538
>     snaive(z)
         Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
2.285714            300   300   300   300   300
2.428571            150   150   150   150   150
2.571429            100   100   100   100   100
2.714286             45    45    45    45    45
2.857143             25    25    25    25    25
3.000000            100   100   100   100   100
3.142857            150   150   150   150   150
3.285714            300   300   300   300   300
3.428571            150   150   150   150   150
3.571429            100   100   100   100   100
3.714286             45    45    45    45    45
3.857143             25    25    25    25    25
4.000000            100   100   100   100   100
4.142857            150   150   150   150   150
>     stl(z, "periodic")
 Call:
 stl(x = z, s.window = "periodic")

Components
Time Series:
Start = c(0, 1) 
End = c(2, 2) 
Frequency = 7 
           seasonal    trend     remainder
0.0000000 -24.28571 124.2857  4.263256e-14
0.1428571  25.71429 124.2857 -2.842171e-14
0.2857143 175.71429 124.2857 -1.421085e-14
0.4285714  25.71429 124.2857 -1.421085e-14
0.5714286 -24.28571 124.2857 -1.421085e-14
0.7142857 -79.28571 124.2857 -1.421085e-14
0.8571429 -99.28571 124.2857  1.421085e-14
1.0000000 -24.28571 124.2857  4.263256e-14
1.1428571  25.71429 124.2857 -1.421085e-14
1.2857143 175.71429 124.2857  0.000000e+00
1.4285714  25.71429 124.2857  0.000000e+00
1.5714286 -24.28571 124.2857  1.421085e-14
1.7142857 -79.28571 124.2857  2.842171e-14
1.8571429 -99.28571 124.2857 -1.421085e-14
2.0000000 -24.28571 124.2857 -7.105427e-14
2.1428571  25.71429 124.2857  8.526513e-14
于 2013-05-08T00:25:06.847 回答