Reputation: 1033
I have been using the Forecast Package in R but have found it difficult to load my own daily time series into a ts object and then use this with the forecasting algorithms. I have instead used zoo to create my daily timeseries object, but I can not pass this directly to forecasting algorithms in the R Forecast package.
Any help in the correct direction would be much appreciated. I feel very stuck with this.
Thanks
Hi Here is some sample code. With a simple quarterly dataset, I can get some prediction working but with a daily dataset I can't get it working. Many Thanks
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"))
Here is an updated bit of R code that still does not work
#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"))
Many Thanks
Upvotes: 0
Views: 3781
Reputation: 270170
forecast
requires that the period be 1 time unit so try this:
> 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
Upvotes: 1