# # ########## Simple Time Series examples ########## # # # This script assumes you have worked through all the previous notes from # the web page and you have downloaded, installed, and updated all available # R packages. # Load the following libraries if you have not already. library(Rcmdr) # In Rcmdr: # Load the 'epack' plugin for Rcmdr by clicking on 'Tools' in Rcmdr, then 'Load Rcmdr plug-in(s)' and then # select 'RcmdrPlugin.epack'. # Rcmdr will need to close and reopen before the Plug-in will be available. Once Rcmdr closes and re-opens, # all code/syntax below should work when typed/pasted into the console. ################ # BP Stock time series; series data are 'by-month'. # The BP oil spill disaster began with the explosion of the Deepwater Horizon oil rig on April 20th, 2010. # The following (4) scripts create 'time series' (ts) data vectors for the stock price at "open" and "close" # for the dates specified. The data is downloaded from Yahoo -- which cataloges stock prices. # In Console: bp_open <- histprice2(inst1="BP",quot1="Open",start1="1998-01-01", end1="2010-12-1") bp_close <- histprice2(inst1="BP",quot1="Close",start1="1998-01-01", end1="2010-12-1") bp2_open <- histprice2(inst1="BP",quot1="Open",start1="2010-01-1", end1="2010-12-1") bp2_close <- histprice2(inst1="BP",quot1="Close",start1="2010-01-1", end1="2010-12-1") cor(bp_open, bp_close) cor(bp2_open, bp2_close) bp_total <- data.frame(bp_open, bp_close) rm(bp_open, bp_close) summary(bp_total) bp_spill <- data.frame(bp2_open, bp2_close) rm(bp2_open, bp2_close) summary(bp_spill) bp_total$ts # Open Price (ts) bp_total$ts.1 # Close Price (ts.1) bp_total plot(bp_total$ts, bp_total$ts.1) plot.ts(bp_total$ts) plot.ts(bp_total$ts.1) plot.ts(bp_total) plot.ts(bp_spill) # Fit an autoregresssive model to time series data (ts). ar(bp_total$ts) ar(bp_total$ts.1) # Fit an ARMA model to a univariate time series by conditional least squares. # For exact maximum likelihood estimation see 'arima0'. arma(bp_total$ts) arma(bp_total$ts.1) library(tseries) # KPSS test for stationarity. kpss.test(bp_total$ts) kpss.test(bp_total$ts.1) kpss.test(bp_spill$ts) kpss.test(bp_spill$ts.1) # Using Rcmdr 'epack' plugin. ArimaModel.1 <- Arima(bp_total$ts,order=c(0,0,0), include.mean=1, seasonal=list(order=c(0,0,0),period=12)) ArimaModel.1 ArimaModel.2 <- Arima(bp_total$ts.1,order=c(0,0,0), include.mean=1, seasonal=list(order=c(0,0,0),period=12)) ArimaModel.2 # Forcasting the next three values of BP stock 'open price' = ArimaModel 1 and BP stock 'close price' = ArimaModel 2. fore001 <- predar3(ArimaModel.1,fore1=3) fore001 fore002 <- predar3(ArimaModel.2,fore1=3) fore002 # Checking BP stock 'open price' and its ability to forecast accurately. accur001<- fore1('ts',bp_total$ts, bp_total) # Filtering with Exponential smoothing. HoltWintersModel.4 <- HoltWinters(bp_total$ts, gamma=0,beta=0) HoltWintersModel.4 # Filtering a non-seasonal model. HoltWintersModel.7 <- HoltWinters(bp_total$ts, gamma=0) HoltWintersModel.7 # Filering a seasonal model (additive). HoltWintersModel.8 <- HoltWinters(bp_total$ts, seasonal='add') HoltWintersModel.8 # Spectural density plot. spectrum(bp_total$ts) spectrum(bp_total$ts.1) # Autocorrelation plot. acf(bp_total$ts) acf(bp_total$ts.1) # END; Sep. 2010