200 likes | 274 Views
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 file name and file path. The name of the data or table.
E N D
Using R for Time Series Analysis R is a package that can be downloaded for free
To read in data from a file:Type> sunData<-read.table("C:/Users/User/Desktop/Sunspot.txt",header=TRUE) The file name and file path The name of the data or table 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 datatype: > plot(sunData[,2]) or > plot(sunData[,”no”])
To plot a line graph of the datatype: plot(sunData[,”no”],type = "l")
To plot a line graph of the data with “Year” along the horizontal axistype: plot(sunData[,”Year”],sunData[,”no”],type = "l")
To plot the autococorrelaton function of “no”type: > acf(sunData[,”no”])
To plot the autococorrelaton function of “no”, up to lag 60type: > acf(sunData[,”no”],60)
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 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 methodtype: > 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: “CSS-ML“ - minimize conditional sum-of-squares to find starting values then maximum likelihood (the default method) “ML" - maximum likelihood “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 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.