Reputation: 48
I'm trying to forecast using Hyndman and Athanasopoulos's Forecasting Principles and Practice, and I'm hitting some annoying issues when trying to use my own data to forecast (using the forecast
package).
My issues are:
Forecasts generated by meanf
have the index 1 rather than the end of the sample, hence plotting gets a bit messier (hence the x, y stuff in the code below). This doesn't appear to happen for H&A in their textbook...
Forecasts generated using snaive
and zoo
when I actually have the date as a date (rather than a number) seem to hit up on some problem, since once through the forecast (bikefit3) the data has six NA's followed by one weekly entry, rather than an entry for each day of the week.
Most minor - given I'm forecasting out of sample here, when I hit summary, I get forecast errors for a particular "training sample". Is there any way to determine what this period is? I tried to find out manually but the numbers just looked strange.
My R code (which should be reproducable since I load up the data from Dropbox) is:
no_f <- 21
bike <-repmis::source_DropboxData("bike_hires.csv","8s4bpjft6zrt4jz",sep = ",",header = TRUE)
bike$date <- as.Date(bike$Day,format="%d/%m/%Y")
dat.xts <- xts(bike$Number.of.Bicycle.Hires,order.by=bike$date)
bike.z <- zoo(dat.xts,order.by=index(dat.xts),frequency=7)
bikefit1 <- meanf(bike.z,h=no_f)
bikefit2 <- naive(bike.z, h=no_f)
bikefit3 <- snaive(bike.z, h=no_f)
x <- bikefit1$mean
y <- seq(1251/7,(1251+no_f-1)/7,1/7)
plot(bike.z[1100:1251],xlim=range(1100/7,(1251+no_f-1)/7),ylim=range(bike.z[1100:1251]),type="l",col=1,main="Forecasts for daily bike hires",xlab="",ylab="")
par(new=T)
plot(y,x,xlim=range(1100/7,(1251+no_f-1)/7),ylim=range(bike.z[1100:1251]),type="l",col=4,xaxt="n",yaxt="n",xlab="",ylab="")
lines(bikefit2$mean,col=2)
lines(bikefit3$mean,col=3)
mtext("Day",side=1,line=3,font=2)
mtext("Number of Bike Hires",side=2,line=3,font=2)
legend("topright",lty=1,col=c(4,2,3), legend=c("Mean method","Naive method","Seasonal naive method"))
Thanks in advance for any help you're able to provide me!
Upvotes: 0
Views: 1883
Reputation: 31820
The meanf
, naive
and snaive
functions from the forecast package are designed to work with ts
objects. You are using a zoo
object. The functions might work, but they might not. Convert your data to ts
objects before calling the functions.
The training data consists of all the data you use when you call the functions.
Upvotes: 1