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
- Download and install R.
- Download and install RStudio.
- 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:
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
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.
- Load the
fpppackage 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.
- Using the Workspace menu, choose "Import Dataset" and import the
data from the
- 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.
- See what the following commands do (in the console tab).
- Convert the data to time series
R codeNotice thattute1 <- ts(tute1[,-1], start=1981, frequency=4)
<-means to assign the value on the right to the object on the left.
- Construct time series plots of each of the three series
- Try the following plots and figure out what is being plotted in each
R codeThe notationseasonplot(tute1[,"Sales"])
data[,"x"]means the column named
xin the data set named
- What features do you notice about each of the series AdBudget, Sales and GDP?
- Construct scatterplots of (AdBudget,Sales) and (GDP,Sales), with
Sales on the vertical axes.
R codeplot(Sales ~ AdBudget, data=tute1)
plot(Sales ~ GDP, data=tute1)
- Do a scatterplot matrix of the three variables.
R codeWhat is plotted in each panel?pairs(as.data.frame(tute1))
- What happens if you use
- Use the
summarycommand to get summary information about the data:R codesummary(tute1)
- Produce some more plots of the data:
- Also do a correlation test.
- Now try using RStudio as a calculator. Figure out what each of the following is doing.
sqrt(2 * max(-10, 0.2, 4.5))
x <- sqrt(2 * max(-10, 0.2, 4.5)) + 100
- Save the workfile, and exit RStudio.
[,-1]removes the first column which contains the quarters as we don’t need them now.
There are dozens of R tutorials available on the web. Some of the best of them are listed below.