12 Using R

This book is designed to be used with R. It 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 "fpp" (make sure "install dependencies" is checked).

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

R examples in this book

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

R code
library(fpp)
plot(a10)

These examples assume that you have the fpp package loaded. So you should use the command library(fpp) before you try any examples provided here. 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

This is a quick tutorial for those who have not previously used R. Download the file robjhyndman.com/data/tute1.csv and review its contents in Excel. You should find four columns of information. Columns B through D each contain a quarterly data series, labelled Sales, AdBudget and GDP. Sales contains the quarterly sales for a small company over the period 1981-2005. AdBudget is the advertising budget and GDP is the gross domestic product. All series have been adjusted for inflation. Close the tute1.csv file and run RStudio.

  1. Load the fpp package using the Packages tab. (This needs to be done at the start of every R session.) This can also be done by typing library(fpp) in the Console panel.
  2. Using the Workspace menu, choose "Import Dataset" and import the data from the tute1.csv file.
  3. The data is now saved as an object in your workspace. Clicking the name of the object will cause it to be viewed. Typing the name of the object in the Console tab will cause it to be printed to the console.
  4. See what the following commands do (in the console tab).
    R code
      head(tute1)
      tail(tute1)
      tute1[,2]
      tute1[,"Sales"]
      tute1[5,]
      tute1[1:10,3:4]
      tute1[1:20,]
     
  5. Convert the data to time series
    R code
    tute1 <- ts(tute1[,-1], start=1981, frequency=4)
    Notice that <- means to assign the value on the right to the object on the left.
  6. The [,-1] removes the first column which contains the quarters as we don’t need them now.
  7. Construct time series plots of each of the three series
    R code
    plot(tute1)
  8. Try the following plots and figure out what is being plotted in each case:
    R code
    seasonplot(tute1[,"Sales"])
    seasonplot(tute1[,"AdBudget"])
    seasonplot(tute1[,"GDP"])
    monthplot(tute1[,"Sales"])
    monthplot(tute1[,"GDP"])
    monthplot(tute1[,"AdBudget"])
    The notation data[,"x"] means the column named x in the data set named data.
  9. What features do you notice about each of the series AdBudget, Sales and GDP?
  10. Construct scatterplots of (AdBudget,Sales) and (GDP,Sales), with Sales on the vertical axes.
    R code
    plot(Sales ~ AdBudget, data=tute1)
    plot(Sales ~ GDP, data=tute1)
  11. Do a scatterplot matrix of the three variables.
    R code
    pairs(as.data.frame(tute1))
    What is plotted in each panel?
  12. What happens if you use pairs(tute1). Why?
  13. Use the summary command to get summary information about the data:
    R code
    summary(tute1)
  14. Produce some more plots of the data:
    R code
    hist(tute1[,"Sales"])
    hist(tute1[,"AdBudget"])
    boxplot(tute1[,"Sales"])
    boxplot(as.data.frame(tute1))
  15. Also do a correlation test.
    R code
    cor.test(tute1[,"Sales"],tute1[,"AdBudget"])
  16. Now try using RStudio as a calculator. Figure out what each of the following is doing.
    R code
    (100+2)/3
    5*10^2
    1/0
    0/0
    (0i-9)^(1/2)
    sqrt(2 * max(-10, 0.2, 4.5))
    x <- sqrt(2 * max(-10, 0.2, 4.5)) + 100
    x
    log(100)
    log(100, base=10)
  17. Save the workfile, and exit RStudio.

More tutorials

There are dozens of R tutorials available on the web. Some of the best of them are listed below.