The examples below use time series data.
load(url("http://www.unige.ch/ses/sococ/cl/edat/unemp1991-2012.RData"))
Time series data structure
R has a special time series data structure and methods for it, i.e. time series specific features will be available with
many basic functions (summary, plot, ...) if the data used is time based. In addition R offers many spezialised fonctions
for time series analysis. This document provides just a few examples to have a first look...
If you have look at the Swiss unemployment rates to show information on the object using str(CHUnemp)
R replies with:
Time-Series [1:264] from 1991 to 2013: 0.796 0.853 0.891 0.929 0.968 ...
(str() compact information on R objects, listing a few actual values).
If you type CHUnemp
you will see a time series table arranged by year and month;
plot(CHUnemp)
R will automatically produce a time series plot.
Some examples of what you can do with time series data
- plot(cbind(CHUnemp, FranceUnemp, GermanyUnemp))
plots separate time series in separate panels
- plot(cbind(CHUnemp, FranceUnemp, GermanyUnemp),plot.type="single",lty=c(1:3))
Plot several time series in the same plot, lty is used to get a different line style for each series.
cbind creates a matrix combining the three variables.
- ts.plot(CHUnemp, FranceUnemp, GermanyUnemp, gpars=list(xlab="years", ylab="Unemployement rate", lty=c(1:3)) )
Same results but using the ts.plot that lets you specify the three series directly.
- plot(decompose(CHUnemp)) Plot of a time series decomposition
- plot(CHUnemp[cycle(CHUnemp)==12], type="l" plot the data for december only; the cycle
function is used set a condition for the 12 month. type="l" is needed to get a line plot, as the result of the
cycle function is no a time series.
- plot( window(CHUnemp,2011,c(2011,12))) plot only year 2011. window is used
to subset a time series object. To make sure that we only get twelve months we need to specify c(2011,12)
as window(CHUnemp,2011,2012) includes all months of 2011, but also January 2013
Related documents