Using R for Time Series Analysis

Post on 05-Jan-2016

26 views 3 download

description

Using R for Time Series Analysis. R is a package that can be downloaded for free. When R is opened. To read in data from a file: Type > sunData

Transcript of Using R for Time Series Analysis

Using R for Time Series Analysis

R is a package that can be downloaded for free

When R is opened

To read in data from a file:Type> sunData<-read.table("C:/Users/User/Desktop/Sunspot.txt",header=TRUE)

The name of the data or table

The file name and file path Read the

variable names

The following line causes the data to be printed> sunData

Year no1 1770 1012 1771 823 1772 664 1773 35

95 1864 4796 1865 3097 1866 1698 1867 799 1868 37100 1869 74

To plot a scatter plot of the datatype:> plot(sunData[,2]) or > plot(sunData[,”no”])

To plot a line graph of the datatype:plot(sunData[,”no”],type = "l")

0 20 40 60 80 100

05

01

00

15

0

Index

sun

Da

ta[,

2]

To plot a line graph of the data with “Year” along the horizontal axistype: plot(sunData[,”Year”],sunData[,”no”],type = "l")

1780 1800 1820 1840 1860

05

01

00

15

0

sunData[, "Year"]

sun

Da

ta[,

"no

"]

To plot the autococorrelaton function of “no”type:

> acf(sunData[,”no”])

0 5 10 15 20

-0.2

0.0

0.2

0.4

0.6

0.8

1.0

Lag

AC

FSeries sunData[, "no"]

To plot the autococorrelaton function of “no”, up to lag 60type:

> acf(sunData[,”no”],60)

0 10 20 30 40 50 60

-0.2

0.0

0.2

0.4

0.6

0.8

1.0

Lag

AC

F

Series sunData[, "no"]

To plot the partial autococorrelaton function of “no”, up to lag 60type: > pacf(sunData[,”no”],60)

To fit and ARIMA modeltype:

> arima(sunData[,2],order = c(2,0,0))

The outputCall:arima(x = sunData[, "no"], order = c(2, 0, 0))

Coefficients: ar1 ar2 intercept 1.4087 -0.7137 48.2124s.e. 0.0704 0.0700 4.9553

sigma^2 estimated as 227.2: log likelihood = -414.46, aic = 836.91

To fit and ARIMA model using a specific methodtype:

> arima(sunData[,”no”],order = c(2,0,0), method ="ML")

The outputCall:arima(x = sunData[, "no"], order = c(2, 0, 0), method = "ML")

Coefficients: ar1 ar2 intercept 1.4088 -0.7137 48.2095s.e. 0.0704 0.0700 4.9557

sigma^2 estimated as 227.2: log likelihood = -414.46, aic = 836.91

The Methods:

1. “CSS-ML“ - minimize conditional sum-of-squares to find starting values then maximum likelihood (the default method)

2. “ML" - maximum likelihood3. “CSS" - minimize conditional sum-of-squares

ARIMA modelling of a univariate time series.

arima(x, order = c(0, 0, 0), seasonal = list(order = c(0, 0, 0), period = NA), xreg = NULL, include.mean = TRUE, transform.pars = TRUE, fixed = NULL, init = NULL, method = c("CSS-ML", "ML", "CSS"), n.cond, optim.method = "BFGS", optim.control = list(), kappa = 1e6)

Argumentsx a univariate time series order A specification of the non-seasonal part of the

ARIMA model: the three components (p, d, q) are the AR order, the degree of differencing, and the MA order.

seasonal A specification of the seasonal part of the ARIMA model, plus the period (which defaults to frequency(x)). This should be a list with components order and period, but a specification of just a numeric vector of length 3 will be turned into a suitable list with the specification as the order.

xreg Optionally, a vector or matrix of external regressors, which must have the same number of rows as x.

include.mean Should the ARMA model include a mean/intercept term? The default is TRUE for undifferenced series, and it is ignored for ARIMA models with differencing.

transform.pars Logical. If true, the AR parameters are transformed to ensure that they remain in the region of stationarity. Not used for method = "CSS".

fixed optional numeric vector of the same length as the total number of parameters. If supplied, only NA entries in fixed will be varied. transform.pars = TRUE will be overridden (with a warning) if any AR parameters are fixed. It may be wise to set transform.pars = FALSE when fixing MA parameters, especially near non-invertibility.

init optional numeric vector of initial parameter values. Missing values will be filled in, by zeroes except for regression coefficients. Values already specified in fixed will be ignored.

method Fitting method: maximum likelihood or minimize conditional sum-of-squares. The default (unless there are missing values) is to use conditional-sum-of-squares to find starting values, then maximum likelihood.

n.cond Only used if fitting by conditional-sum-of-squares: the number of initial observations to ignore. It will be ignored if less than the maximum lag of an AR term.

optim.method The value passed as the method argument to optim.

optim.control List of control parameters for optim.kappa the prior variance (as a multiple of the

innovations variance) for the past observations in a differenced model. Do not reduce this.

Autocovariance, Autocorrelation function.

acf(x, lag.max = NULL, type = c("correlation", "covariance", "partial"), plot = TRUE, na.action = na.fail, demean = TRUE, ...)

Examples acf(arima(sunData[,”no”], 60, type = "covariance")•produces the autocovariace function

acf(arima(sunData[,”no”], 60, type = “partial")•produces the partial autocorrelation function (same result as)pacf(arima(sunData[,”no”], 60)

Cross-covariance, Cross-correlation function.

ccf(x, y, lag.max = NULL, type = c("correlation", "covariance"), plot = TRUE, na.action = na.fail, ...)

This R function plots the Cross Cross-covariance or the Cross-correlation function.