Using R for Time Series Analysis R is

  • Slides: 19
Download presentation
Using R for Time Series Analysis R is a package that can be downloaded

Using R for Time Series Analysis R is a package that can be downloaded for free

When R is opened

When R is opened

To read in data from a file: Type > sun. Data<-read. table("C: /Users/User/Desktop/Sunspot. txt",

To read in data from a file: Type > sun. Data<-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 > sun. Data 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(sun. Data[, 2]) or

To plot a scatter plot of the data type: > plot(sun. Data[, 2]) or > plot(sun. Data[, ”no”])

To plot a line graph of the data type: plot(sun. Data[, ”no”], type =

To plot a line graph of the data type: plot(sun. Data[, ”no”], type = "l")

To plot a line graph of the data with “Year” along the horizontal axis

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

To plot the autococorrelaton function of “no” type: > acf(sun. Data[, ”no”])

To plot the autococorrelaton function of “no” type: > acf(sun. Data[, ”no”])

To plot the autococorrelaton function of “no”, up to lag 60 type: > acf(sun.

To plot the autococorrelaton function of “no”, up to lag 60 type: > acf(sun. Data[, ”no”], 60)

To plot the partial autococorrelaton function of “no”, up to lag 60 > pacf(sun.

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

To fit and ARIMA model type: > arima(sun. Data[, 2], order = c(2, 0,

To fit and ARIMA model type: > arima(sun. Data[, 2], order = c(2, 0, 0)) The output Call: arima(x = sun. Data[, "no"], order = c(2, 0, 0)) Coefficients: ar 1 ar 2 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(sun. Data[, ”no”],

To fit and ARIMA model using a specific method type: > arima(sun. Data[, ”no”], order = c(2, 0, 0), method ="ML") The output Call: arima(x = sun. Data[, "no"], order = c(2, 0, 0), method = "ML") Coefficients: ar 1 ar 2 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

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

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", "CSS"), n. cond, optim. method = "BFGS", optim. control = list(), kappa = 1 e 6)

Arguments x order seasonal xreg a univariate time series A specification of the non-seasonal

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

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

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.

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

Autocovariance, Autocorrelation function. acf(x, lag. max = NULL, type = c("correlation", "covariance", "partial"), plot = TRUE, na. action = na. fail, demean = TRUE, . . . ) Examples acf(arima(sun. Data[, ”no”], 60, type = "covariance") • produces the autocovariace function acf(arima(sun. Data[, ”no”], 60, type = “partial") • produces the partial autocorrelation function (same result as) pacf(arima(sun. Data[, ”no”], 60)

Cross-covariance, Cross-correlation function. ccf(x, y, lag. max = NULL, type = c("correlation", "covariance"), plot

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 Crosscovariance or the Cross-correlation function.