Using R for Time Series Analysis

advertisement
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 no
1 1770 101
2 1771 82
3 1772 66
4 1773 35
95 1864 47
96 1865 30
97 1866 16
98 1867 7
99 1868 37
100 1869 74
To plot a scatter plot of the data
type:
> plot(sunData[,2])
or
> plot(sunData[,”no”])
To plot a line graph of the data
type:
100
50
0
sunData[, 2]
150
plot(sunData[,”no”],type = "l")
0
20
40
60
Index
80
100
100
50
0
sunData[, "no"]
150
To plot a line graph of the data with “Year” along the
horizontal axis
type: plot(sunData[,”Year”],sunData[,”no”],type = "l")
1780
1800
1820
sunData[, "Year"]
1840
1860
To plot the autococorrelaton function of “no”
type: > acf(sunData[,”no”])
0.4
0.2
0.0
-0.2
ACF
0.6
0.8
1.0
Series sunData[, "no"]
0
5
10
Lag
15
20
To plot the autococorrelaton function of “no”, up to lag 60
type:
> acf(sunData[,”no”],60)
0.4
0.2
0.0
-0.2
ACF
0.6
0.8
1.0
Series sunData[, "no"]
0
10
20
30
Lag
40
50
60
To plot the partial autococorrelaton function of “no”, up to
lag 60
> pacf(sunData[,”no”],60)
type:
To fit and ARIMA model
type:
> arima(sunData[,2],order = c(2,0,0))
The output
Call:
arima(x = sunData[, "no"], order = c(2, 0, 0))
Coefficients:
ar1 ar2 intercept
1.4087 -0.7137 48.2124
s.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 method
type:
> arima(sunData[,”no”],order = c(2,0,0), method ="ML")
The output
Call:
arima(x = sunData[, "no"], order = c(2, 0, 0), method = "ML")
Coefficients:
ar1 ar2 intercept
1.4088 -0.7137 48.2095
s.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-ofsquares to find starting values then maximum
likelihood (the default method)
2. “ML" - maximum likelihood
3. “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)
Arguments
x
order
seasonal
xreg
a univariate time series
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.
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.
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
method
n.cond
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.
Fitting method: maximum likelihood or minimize
conditional sum-of-squares. The default (unless
there are missing values) is to use conditional-sumof-squares to find starting values, then maximum
likelihood.
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
optim.control
kappa
The value passed as the method argument
to optim.
List of control parameters for optim.
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 Crosscovariance or the Cross-correlation function.
Download