Appendix: Using R

This book uses R and is designed to be used with R. R is free, available on almost every operating system, and there are thousands of add-on packages to do almost anything you could ever want to do. We recommend you use R with RStudio.

Installing R and RStudio

  1. Download and install R.

  2. Download and install RStudio.

  3. Run RStudio. On the “Packages” tab, click on “Install packages” and install the package remotes and tidyverse packages (make sure “install dependencies” is checked).

  4. Still within RStudio, to install the remaining packages, use the following code:

    remotes::install_github("robjhyndman/fpp3")

That’s it! You should now be ready to go.

R examples in this book

We provide R code for most examples in shaded boxes like this:

# Load required packages
library(fpp3)

# Plot one time series
aus_retail %>%
  filter(`Series ID`=="A3349640L") %>%
  autoplot(Turnover)

# Produce some forecasts
PBS %>%
  filter(ATC2=="A10") %>%
  summarise(Scripts = sum(Scripts)) %>%
  model(ETS(Scripts)) %>%
  forecast()

These examples assume that you have the fpp3 package loaded as shown above. This needs to be done at the start of every R session, but won’t be included in our examples.

Sometimes we also assume that the R code that appears earlier in the same section of the book has also been run; so it is best to work through the R code in the order provided within each section.

Getting started with R

If you have never previously used R, please work through the first section (chapters 1-8) of “R for Data Science” by Garrett Grolemund and Hadley Wickham. While this does not cover time series or forecasting, it will get you used to the basics of the R language, and the tidyverse packages. The Coursera R Programming course is also highly recommended.

You will learn how to use R for forecasting using the exercises in this book.