Difference between revisions of "R TimeSeries"
Line 38: | Line 38: | ||
=== Differencing data === | === Differencing data === | ||
− | Often you will need a differenced time series <math | + | Often you will need a differenced time series <math>\delta y_t = y_{t} - y_{t-1}</math>. The command to create this is a straightforward: |
+ | |||
+ | dUKCPI <- diff(data$UKCPI) | ||
+ | |||
+ | You can actually have a quick look at your data by using the basic plotting function | ||
+ | |||
+ | plot(dUKCPI) | ||
+ | |||
+ | which will reveal | ||
+ | |||
== Additional Resources == | == Additional Resources == |
Revision as of 21:35, 11 February 2015
Contents
The forecast package
In this section we will demonstrate how to do basic univariate time-series modelling with R. We will use a package written by Rob Hyndman, called "forecast". So before you get started you need to go to R and
install.packages("forecast")
But note that this package requires R of version 3. Then at the beginning of your code you will have to import the library by adding
library(forecast)
to your code.
Importing Data
Here we will initially use a dataset on UK CPI UKCPI.xls. Doenload this and save it as a csv file as this will facilitate the upload to R.
setwd("YOUR DIRECTORY") # This sets the working directory data <- read.csv("UKCPI.csv") # Opens UKCPI.csv from working directory
which will produce a dataframe (data
) with two variables, one giving dates (DATE
) and the other containing the actual CPI data for 1988Q1 to 2013Q4 (UKCPI
).
Basic Time-Series Data Transformations
Forcing Time-Series
The first thing we need is to ensure that R knows that the data we are dealing with are actually time series data. There is a function in R you can use to check whether R realises that data are time series data or not:
print(is.ts(data$
UKCPI)) # check whether it is a time-series object
This will print either TRUE
or FALSE
. In this case the answer is FALSE
, data$UKCPI
is not recognised as a time-series. We can now force R to recognise this variable as a time-series. Use the following command:
data$UKCPI <- ts(data$
UKCPI, start=c(1988, 1), end=c(2013, 4), frequency=4)
Let's see what is happening here. ts()
is the function used and as usual you can use ?ts
to figure out some more details. The first input is the data series we want to be recognised as atime-series. The next input is the data of the first observation, start=c(1988, 1)
, and the date of the last observation, and importantly the data frequency, frequency=4
(4 representing quarterly data and 12 monthly data).
To confirm that the data are now time-series data you could use the is.ts()
function again which should now return a TRUE
.
Differencing data
Often you will need a differenced time series [math]\delta y_t = y_{t} - y_{t-1}[/math]. The command to create this is a straightforward:
dUKCPI <- diff(data$UKCPI)
You can actually have a quick look at your data by using the basic plotting function
plot(dUKCPI)
which will reveal
Additional Resources
- A very quick intro from Quick-R can be found here [1]
- We are using the package "forecast" authored by Rob Hyndman who has also written an online textbook on the topic of forecasting [2]
- To access some very useful data-series in a very convenient way we will also use the QUANDL package.