Time Series Analysis
Time Series Analysis
Use R!
R is free R is a language, not just a statistical tool R makes graphics and visualization of the best quality A flexible statistical analysis toolkit Access to powerful, cutting-edge analytics A robust, vibrant community Unlimited possibilities
Where to Download R
To download R
Go to http://www.r-project.org/ Choose a CRAN Mirror, such as http://cran.cnr.berkeley.edu/ Click the link to download R according to your operating system (Linux, MacOS X, or Windows)
R References
An Introduction to R, W.N.Venables, D.M.Smith and R Development Core Team More Documents/Tutorials, go to
http://cran.cnr.berkeley.edu/other-docs.html
Start an R Project
Recommend using RStudio as the console (http://rstudio.org/download/) Create a project folder for storing R scripts, data, etc.
e.g. C:/Users/xie/Documents/SYST460/R projects/airline_timeseries
Min value
Window Function
Extract a part of the time series between specied start and end points > rpm.Feb <- window(rpm.ts, start = c(1996,02), freq = TRUE) > rpm.Aug <- window(rpm.ts, start = c(1996,08), freq = TRUE) > mean(rpm.Feb)/mean(rpm.Aug)
Auto-Correlation
Regression
Trends: stochastic trends, deterministic trends Deterministic trends and seasonal variation can be modeled using regression Deterministic trends are often used for prediction Time series regression differs from standard regression as time series tends to be serially correlated
Linear Models
Diagnostic Plots
> hist(resid(fit))
Coefficients
Random Walk
Let {xt} be a time series. Then {xt} is a random walk if xt = xt1 + wt where {wt} is a white noise series. Substituting xt1 = xt2+wt1 and then substituting for xt2, followed by xt3 and so on gives: xt = wt + wt1 + wt2 + . . . In practice, the series will start at some time t = 1. Hence, xt = w1 + w2 + . . . + wt
Stationarity
Fit an AR Model
> res.ar=ar(resid(rpm.lm),method="ols") > res.ar
> acf(res.ar$res[-(1:3)])
Simulate an MA Process
> set.seed(1) > b <- c(0.8, 0.6, 0.4) > x <- w <- rnorm(1000) > for (t in 4:1000) { for (j in 1:3) x[t] <- x[t] + b[j] * w[t - j] } > plot(x, type = "l") > acf(x)
Plot Results
Simulated MA Process
ARMA Model
> acf(as.numeric(rpm.arima$resid))
Forecast
Predicting future values of a time series, xn+m, using the set of present and past values of the time series, x={xn, xn-1, , x1} The minimum mean square error predictor of n xn+m is xn m E ( xn m x) predict(model, newdata) method
model: a model object used for prediction newdata: value of explanatory variables
Forecast in R
> new.t <- seq(2011.750, len = 2 * 12, by = 1/12) > new.dat <- data.frame(Time = new.t, Seas = rep(1:12, 2)) > rpm.pred=ts(predict(rpm.lm, new.dat)[1:24],start=c(2011,11),freq=12) > ts.plot(rpm.ts,rpm.pred,lty=1:2)
Plot of Forecast
Homework
Download and Install R with Rstudio Read An Introduction to R Download System Passenger - Revenue Aircraft Miles Flown (000) (Jan 1996 - Oct 2011) data from BTS Read the data into R using Rstudio Create a time series plot of the data, and plot its auto-correlation correlogram Decompose the time series and save the plot
Homework (Ctd.)
Construct a linear regression model without seasonal factors, and plot the correlogram of the models residual data Construct a linear regression model with seasonal factors, and identifies the characteristics of the model residual. Fit an AR model to the model residual of the above model Forecast the time series data into next 24 months using the seasonal model
Exam Questions
What are the data elements in a time series? What does auto-correlation mean? What are white noise and random walk? What are stationary models and non-stationary models?