Sunteți pe pagina 1din 2

Quick-R: Time Series

http://www.statmethods.net/advstats/timeseries.html

Home

Interface

Input

Manage

Stats

Adv Stats

Graphs

Adv Graphs

Quick-R

Blog

Search

accessing the power of R

Advanced Statistics

Time Series and Forecasting


R has extensive facilities for analyzing time series data. This section describes the creation of a time

Generalized Linear Models

series, seasonal decompostion, modeling with exponential and ARIMA models, and forecasting with the

Discriminant Function

forecast package.

Time Series
Factor Analysis

Creating a time series

Correspondence Analysis

The ts() function will convert a numeric vector into an R time series object. The format is ts(vector,

Multidimensional Scaling

start=, end=, frequency=) where start and end are the times of the first and last observation and

Cluster Analysis

frequency is the number of observations per unit time (1=annual, 4=quartly, 12=monthly, etc.).

Tree-Based Models
# save a numeric vector containing 48 monthly observations
Bootstrapping
Matrix Algebra

# from Jan 2009 to Dec 2014 as a time series object


myts <- ts(myvector, start=c(2009, 1), end=c(2014, 12), frequency=12)
# subset the time series (June 2014 to December 2014)

R in Action

myts2 <- window(myts, start=c(2014, 6), end=c(2014, 12))


# plot series
plot(myts)

Seasonal Decomposition
R in Action (2nd ed) significantly
expands upon this material. Use
promo code ria38 for a 38%

A time series with additive trend, seasonal, and irregular components can be decomposed using the stl()
function. Note that a series with multiplicative effects can often by transformed into series with additive
effects through a log transformation (i.e., newts <- log(myts)).

discount.
# Seasonal decomposition
fit <- stl(myts, s.window="period")

Top Menu
Home
The R Interface
Data Input

plot(fit)
# additional plots
monthplot(myts)
library(forecast)
seasonplot(myts)

Data Management
Basic Statistics

Exponential Models

Advanced Statistics

Both the HoltWinters() function in the base installation, and the ets() function in the forecast package,

Basic Graphs

can be used to fit exponential models.

Advanced Graphs
# simple exponential - models level
Blog

fit <- HoltWinters(myts, beta=FALSE, gamma=FALSE)


# double exponential - models level and trend
fit <- HoltWinters(myts, gamma=FALSE)
# triple exponential - models level, trend, and seasonal components
fit <- HoltWinters(myts)
# predictive accuracy
library(forecast)
accuracy(fit)
# predict next three future values

1 de 2

1/3/16 15:00

Quick-R: Time Series

http://www.statmethods.net/advstats/timeseries.html

library(forecast)
forecast(fit, 3)
plot(forecast(fit, 3))

ARIMA Models
The arima() function can be used to fit an autoregressive integrated moving averages model. Other
useful functions include:

lag(ts, k)

lagged version of time series, shifted back k observations

diff(ts, differences=d)

difference the time series d times

ndiffs(ts)

Number of differences required to achieve stationarity (from


the forecast package)

acf(ts)

autocorrelation function

pacf(ts)

partial autocorrelation function

adf.test(ts)

Augemented Dickey-Fuller test. Rejecting the null hypothesis


suggests that a time series is stationary (from the tseries
package)

Box.test(x,
type="Ljung-Box")

Pormanteau test that observations in vector or time series x


are independent

Note that the forecast package has somewhat nicer versions of acf() and pacf() called Acf() and Pacf()
respectively.

# fit an ARIMA model of order P, D, Q


fit <- arima(myts, order=c(p, d, q)
# predictive accuracy
library(forecast)
accuracy(fit)
# predict next 5 observations
library(forecast)
forecast(fit, 5)
plot(forecast(fit, 5))

Automated Forecasting
The forecast package provides functions for the automatic selection of exponential and ARIMA models.
The ets() function supports both additive and multiplicative models. The auto.arima() function can
handle both seasonal and nonseasonal ARIMA models. Models are chosen to maximize one of several fit
criteria.

library(forecast)
# Automated forecasting using an exponential model
fit <- ets(myts)
# Automated forecasting using an ARIMA model
fit <- auto.arima(myts)

Going Further
There are many good online resources for learning time series analysis with R. These include A little book
of R for time series by Avril Chohlan, and Forecasting: principles and practice by Rob Hyndman and
George Athanasopoulos. Vito Ricci has created a time series reference card. There are also a time series
tutorial by Walter Zucchini, Oleg Nenadic that is quite useful.
See also the comprehensive book Time Series Analysis and its Applications with R Examples by Robert
Shunway and David Stoffer.

Copyright 2014 Robert I. Kabacoff, Ph.D. | Sitemap


Designed by WebTemplateOcean.com

2 de 2

1/3/16 15:00

S-ar putea să vă placă și