R - Forecasting with ARIMA

From Training Material
Revision as of 16:52, 25 November 2014 by Cesar Chew (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Moving Average

Smoothing with Moving Average
 plot(ma(rawdata$Price,order=2),type="l")
 lines(ma(rawdata$Price,order=4),type="l",col="blue")
 lines(ma(rawdata$Price,order=12),type="l",col="red")

Autocorrelation Function

 acf(rawdata$Price)

Seasonality

 price.ts <- ts(rawdata$Price,frequency=12) 
 seasonplot(price.ts)

Autoregression

  • AR(p) - autoregressive model of order p
  • - parameters of the model
  • a constant
  • white noise


 rawdata <- read.table("http://training-course-material.com/images/1/19/Sales-time-series.txt",h=T)
 rawdata$Date <- as.Date(rawdata$Date)

 plot(rawdata$Price,type="l",xlim=c(1,200))

 # Build a model (try different methods)
 model <- ar(rawdata$Price)

 # Predict
 pr = predict(model, n.ahead=100)

 # add prediction to the plot
 lines(pr$pred,col="red")

 # Increase the order
 model <- ar(rawdata$Price,order.max=40,aic=FALSE)
 pr = predict(model, n.ahead=100)
 lines(pr$pred,col="blue")

 # Change the method
 model <- ar(rawdata$Price,order.max=40,aic=FALSE,method="burg")
 pr = predict(model, n.ahead=100)
 lines(pr$pred,col="green")

See Also