Time series analysis functions to remember

Here’s a collection of not-to-forget R-functions related to time series analysis. At the end there is also my short-list of reading material related to this topic.

Note that you will need a couple of libraries to happily analyze time-series: TTR (Technical Trading Rules), forecast (Forecasting Functions for Time Series and Linear Models)

The data used to illustrate the functions is the Australian monthly gas production and is sample data sitting in the forecast package.

∇ ts turns your data into a time-series

∇ frequency returns the number of samples per unit time

frequency(gas) # gives 12

∇ window is a generic function which extracts the subset of the object ‘x’ observed between the times ‘start’ and ‘end’. If a frequency is specified, the series is then re-sampled at the new frequency.

window(gas, 1964, 1973, extend = TRUE)

∇ cycle gives the positions in the cycle of each observation, so for example if your cycle is ‘year’ you will see numbers in the [1, 12] range

cycle(gas) # 1956:   1   2   3   4   5   6   7   8   9  10  11  12

∇ deltat returns the time interval between observations

deltat(gas) # 0.08333333

∇ end the time/date the last observation was taken

end(gas) # 1995/8

∇ acf, pacf, ccf: auto-correlation, partial auto-correlation and cross-correlation (correlation between two separate series) function.

∇ diff.ts: returns suitably lagged and iterated differences

∇ lag: computes a lagged version of a time series, shifting the time base back by a given number of observations

lag(gas,k = 5)

∇ ar fits an autoregressive time series model to the data

f = ar(gas); plot(forecast(f));

∇ arima fits an ARIMA model to a univariate time series

f = arima(gas); plot(forecast(f));

∇ arima.sim: simulate from an ARIMA model

sim=arima.sim(model=list(ar=c(.2,.2,.3)), n=100)

∇ arma: fits an ARMA model to a univariate time series by conditional least squares

arma(gas)

∇ garch: fits a Generalized Autoregressive Conditional Heteroscedastic GARCH(p, q) time series model to the data by computing the maximum-likelihood estimates of the conditionally normal model

library(series)
data(EuStockMarkets)
dax dax.garch summary(dax.garch) # ARCH effects are filtered. However,
plot(dax.garch)

∇ lag.plot: plots time series against lagged versions of themselves. Helps visualizing “auto-dependence” even when auto-correlations vanish

lag.plot(gas)

∇ monthplot: plots a seasonal (or other) subseries of a time series

monthplot(gas)

∇ plot.ts: plotting time-series objects

∇ seasonplot plotting seasonal sub-series or profile

seasonplot(gas)

∇ decompose decomposes a time series into seasonal, trend and irregular components using moving averages

decompose(gas)

∇ filter applies a linear (smoothing) filter on a time series

par(mfrow = c(2,1),mar = c(0,0,0,0))
plot(filter(gas, rep(1, 3)))
plot(gas)

∇ HoltWinters Holt-Winters exponential smoothing with trend and additive seasonal component

plot(HoltWinters(gas))

∇ adf.test computes the augmented Dickey-Fuller test

∇ Box.test computea the Box–Pierce or Ljung–Box test statistic for examining the null hypothesis of independence in a given time series. These are sometimes known as ‘portmanteau’ tests.

∇ bds.test examines the “spatial dependence” of the observed series. To do this, the series is embedded in m-space and the dependence of x is examined by counting “near” points. Points for which the distance is less than eps are called “near”. The BDS test statistic is asymptotically standard Normal.

∇ jarque.bera.test Jarque-Bera test for normality

∇ kpss.test computes KPSS test for stationarity

∇ shapiro.test Shapiro-Wilk Normality Test

∇ spectrum estimates the spectral density of a time series

spectrum(gas)

∇ stl decomposes a time series into seasonal, trend and irregular components using LOESS

∇ tsr decomposes a time series into trend, seasonal and irregular. Deals with additive and multiplicative components

∇ seqplot.ts plots a two time series on the same plot frame