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:
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.
- 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 typinglibrary(fpp)
in the Console panel. - Using the Workspace menu, choose "Import Dataset" and import the
data from the
tute1.csv
file. - 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).
R codehead(tute1)
tail(tute1)
tute1[,2]
tute1[,"Sales"]
tute1[5,]
tute1[1:10,3:4]
tute1[1:20,]
- 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.
The - Construct time series plots of each of the three series
R codeplot(tute1)
- Try the following plots and figure out what is being plotted in each
case:
R codeThe notationseasonplot(tute1[,"Sales"])
seasonplot(tute1[,"AdBudget"])
seasonplot(tute1[,"GDP"])
monthplot(tute1[,"Sales"])
monthplot(tute1[,"GDP"])
monthplot(tute1[,"AdBudget"])data[,"x"]
means the column namedx
in the data set nameddata
. - 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
pairs(tute1)
. Why? - Use the
summary
command to get summary information about the data:R codesummary(tute1) - Produce some more plots of the data:
R codehist(tute1[,"Sales"])
hist(tute1[,"AdBudget"])
boxplot(tute1[,"Sales"])
boxplot(as.data.frame(tute1)) - Also do a correlation test.
R codecor.test(tute1[,"Sales"],tute1[,"AdBudget"])
- 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) - Save the workfile, and exit RStudio.
[,-1]
removes the first column which contains the quarters as we don’t need them now.
More tutorials
There are dozens of R tutorials available on the web. Some of the best of them are listed below.