learning forecasting in r

Hi, I need some assistance in forecasting which am trying to learn in R. I have a data frame for 10 years where I would like to have a forecast for the next five years. I would like to produce a graph like the one below:
image
This is my script am trying to work on:

library(tidyverse)
library(lubridate)
library(ggplot2)
library(forecast)

river <- data.frame(stringsAsFactors = FALSE,
                    flow = c(3.4,	4.1,	3.6,	4.2,	7.7,	4.6,	3.2,	3.3, 4.4,
                             4.7, 5.2,	4.2,	3.1,	3.2,	3.9,	8.7,	8.6,	4.6,
                             3,	3.5, 3.6,	3.7,	9.2,	17.9,	9.8,	5.8,	4.6,	6.4,
                             6.3, 9.8,	4.2,	4.4,	14.4,	12.7,	17.9,	23,	9.6, 6.6,
                             14.8,	19.1,	7.4,	6.7,	5.2,	3.7,	4.7, 17,	8.6,
                             4.9,	8.2,	6.4,	8.3,	5.2,	3.9,	4.2,	3.9, 4.1,	5.1,
                             7.9,	11.1,	13.4,	10.5,	18.8,	25.9, 39.5, 11.3,	4.8, 3.7,
                             4.6,	7.5,	16.9,	13.3,	6.4, 2.2,	2.1,	4.1,	7.8,	3.8, 
                             2.7,	3.7,	19.9,	14,	25.1, 9.7,	9.2,	3,	2.7,	29,	39,
                             11.1,	4,	3.2,	6.6, 9.9, 19.9,	15.1,	20.9,	12.3,	10.6,
                             18.4,	10.4,	5.7,3.8,	4.7,	7.2,	11.6,	9.7,	12.4,	6.5,
                             3.8,	4.7,	9.2, 5.9,	2.9,	1.7,	1.8,	12.6,	6.8,	15,
                             39.8,	6.4,	10.9, 9.6,	11.2,	11.2,	6.4,	4.7,	2.8,
                             4.1,	6.4, 13.6,	13.6),
                    time = c("Jan-05",	"Feb-05",	"Mar-05",	"Apr-05",	"May-05",	"Jun-05",
                             "Jul-05",	"Aug-05",	"Sep-05",	"Oct-05",	"Nov-05",	"Dec-05",
                             "Jan-06",	"Feb-06",	"Mar-06",	"Apr-06",	"May-06",	"Jun-06",
                             "Jul-06",	"Aug-06",	"Sep-06",	"Oct-06",	"Nov-06",	"Dec-06",
                             "Jan-07",	"Feb-07", "Mar-07",	"Apr-07",	"May-07",	"Jun-07",
                             "Jul-07",	"Aug-07",	"Sep-07",	"Oct-07",	"Nov-07",	"Dec-07",
                             "Jan-08",	"Feb-08",	"Mar-08",	"Apr-08",	"May-08",	"Jun-08",
                             "Jul-08",	"Aug-08",	"Sep-08",	"Oct-08",	"Nov-08",	"Dec-08",
                             "Jan-09",	"Feb-09",	"Mar-09",	"Apr-09",	"May-09",	"Jun-09",
                             "Jul-09",	"Aug-09",	"Sep-09",	"Oct-09",	"Nov-09",	"Dec-09",
                             "Jan-10",	"Feb-10",	"Mar-10",	"Apr-10",	"May-10",	"Jun-10",
                             "Jul-10",	"Aug-10",	"Sep-10",	"Oct-10",	"Nov-10",	"Dec-10",
                             "Jan-11",	"Feb-11",	"Mar-11",	"Apr-11",	"May-11",	"Jun-11",
                             "Jul-11",	"Aug-11",	"Sep-11",	"Oct-11",	"Nov-11",	"Dec-11",
                             "Jan-12",	"Feb-12",	"Mar-12",	"Apr-12",	"May-12",	"Jun-12",
                             "Jul-12",	"Aug-12",	"Sep-12",	"Oct-12",	"Nov-12",	"Dec-12",
                             "Jan-13",	"Feb-13",	"Mar-13",	"Apr-13",	"May-13",	"Jun-13",
                             "Jul-13",	"Aug-13",	"Sep-13",	"Oct-13",	"Nov-13",	"Dec-13",
                             "Jan-14",	"Feb-14",	"Mar-14",	"Apr-14",	"May-14",	"Jun-14",
                             "Jul-14",	"Aug-14",	"Sep-14",	"Oct-14",	"Nov-14",	"Dec-14",
                             "Jan-15",	"Feb-15",	"Mar-15",	"Apr-15",	"May-15",	"Jun-15",
                             "Jul-15",	"Aug-15",	"Sep-15",	"Oct-15",	"Nov-15",	"Dec-15"))

river %>%
  mutate(time = dmy(paste("01-", time)), flow =  flow / 5) %>% 
  gather(variable, flow, -time) %>%
df<-ts(river, start=c(2005))
df<-forecast.HoltWinters(df,beta=F, gamma=F,h=10)

I've a few problems running your code.

First of all, flow and time are of different length. So, I excluded time completely in my answer.

Secondly, I didn't understand why you need to use gather. I considered the division by 5 in my answer, though.

Third, forecast.HoltWinters will not be available if you just use library(forecast). You need to use :::. Also, it doesn't accept a ts object.

In any case, I didn't get the graph you showed. There's really no such pattern in the provided data. But I think that the following may be a starting point of what you're trying to do. I didn't want to use forecast at all, as not required. But it's easier to get a shaded plot using it, and hence I included that at the end.

flow <- c(3.4, 4.1, 3.6, 4.2, 7.7, 4.6, 3.2, 3.3, 4.4,
          4.7, 5.2, 4.2, 3.1, 3.2, 3.9, 8.7, 8.6, 4.6,
          3, 3.5, 3.6, 3.7, 9.2, 17.9, 9.8, 5.8, 4.6, 6.4,
          6.3, 9.8, 4.2, 4.4, 14.4, 12.7, 17.9, 23, 9.6, 6.6,
          14.8, 19.1, 7.4, 6.7, 5.2, 3.7, 4.7, 17, 8.6,
          4.9, 8.2, 6.4, 8.3, 5.2, 3.9, 4.2, 3.9, 4.1, 5.1,
          7.9, 11.1, 13.4, 10.5, 18.8, 25.9, 39.5, 11.3, 4.8, 3.7,
          4.6, 7.5, 16.9, 13.3, 6.4, 2.2, 2.1, 4.1, 7.8, 3.8,
          2.7, 3.7, 19.9, 14, 25.1, 9.7, 9.2, 3, 2.7, 29, 39,
          11.1, 4, 3.2, 6.6, 9.9, 19.9, 15.1, 20.9, 12.3, 10.6,
          18.4, 10.4, 5.7,3.8, 4.7, 7.2, 11.6, 9.7, 12.4, 6.5,
          3.8, 4.7, 9.2, 5.9, 2.9, 1.7, 1.8, 12.6, 6.8, 15,
          39.8, 6.4, 10.9, 9.6, 11.2, 11.2, 6.4, 4.7, 2.8,
          4.1, 6.4, 13.6, 13.6)

ts_flow <- ts(data = (flow / 5),
              start = c(2005, 1),
              end = c(2015, 11),
              frequency = 12)

HW_model <- HoltWinters(x = ts_flow,
                        beta = FALSE,
                        gamma = FALSE)

fc <- predict(object = HW_model,
              n.ahead = 10,
              prediction.interval = TRUE)

plot(x = HW_model,
     predicted.values = fc)


fc_HW <- forecast:::forecast.HoltWinters(object = HW_model,
                                         h = 10)

plot(x = fc_HW) # modify to make it look nicer

Created on 2019-05-06 by the reprex package (v0.2.1)

Good luck!

1 Like

Great thanks Yarnabrina once again for your help, this is indeed helpful. I had copied the code from some other source and was trying to run it with the data.

In case you (or anyone stumbling across this thread) don't already know about it, two authors of the forecast package offer a great online textbook for learning forecasting techniques, using R for code:

Forecasting: Principles and Practice by Rob J Hyndman and George Athanasopoulos.

2 Likes

Thanks nwerth for this resource.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.