Individuals stock prices tend to exhibit high amounts of non-constant variance, and thus ARIMA models build upon that data would likely exhibit non-constant variance in residuals. In this problem we are going to analyze the Tesla stock price data from January 2015 through end of December 2023. We will use the ARIMA-GARCH to model daily and weekly stock price (adjusted close price at the end of a day for daily data or at the end of the week for weekly data), with a focus on the behavior of its volatility as well as forecasting both the price and the volatility.
##Data import and cleaning
## Libraries used within this homework are uploaded here
library(zoo,warn.conflicts=FALSE)
library(lubridate,warn.conflicts=FALSE)
library(mgcv,warn.conflicts=FALSE)
library(rugarch,warn.conflicts=FALSE)
#importing the data
dailydata <- read.csv("TSLA_Daily (1).csv", head = TRUE)
weeklydata <- read.csv("TSLA_Weekly (1).csv", head = TRUE)
#cleaning the data
#dates to date format
weeklydata$Date<-as.Date(weeklydata$Date,format='%m/%d/%y')
dailydata$Date<-as.Date(dailydata$Date,format='%m/%d/%y')
#prices to timeseries format
TWeekly <- ts(weeklydata$AdjClose,start=c(2015),freq=52)
TDaily <- ts(dailydata$AdjClose,start=c(2015),freq=252)
1a. Based on your intuition, when would you use daily vs weekly stock price data?
1b. Plot the time series plots comparing daily vs weekly data. How do the daily vs weekly time series data compare?
ts.plot (TWeekly, main="Weekly Tesla Stock Price")
ts.plot (TDaily, main="Daily Tesla Stock Price")
Response: Weekly vs Monthly Time Series data comparison Weekly charts seem to smooth out the daily price fluctuations, thereby facilitating the identification of longer-term trends. On the other hand, the daily charts can pinpoint more precise points. By leveraging weekly charts for overarching trend analysis and daily charts for granular decision-making, one can navigate the fluctuations in Tesla’s stock price.
1c. Fit a non-parametric trend using splines regression to both the daily and weekly time series data. Overlay the fitted trends. How do the trends compare?
Analyzing weekly and daily data with trend fitting
ts.plot (TWeekly, main="Weekly Tesla Stock Price")
timeweekly.pts = c(1:length(TWeekly))
timeweekly.pts = c(timeweekly.pts-min(timeweekly.pts))/max(timeweekly.pts)
gamweekly.fit = gam(TWeekly~s(timeweekly.pts))
ts.fit.gamweekly = ts(fitted(gamweekly.fit), start = 2015, frequency = 52)
lines(ts.fit.gamweekly, lwd = 2, col = "red")
ts.plot (TDaily, main="Daily Tesla Stock Price")
timedaily.pts = c(1:length(TDaily))
timedaily.pts = c(timedaily.pts-min(timedaily.pts))/max(timedaily.pts)
gamdaily.fit = gam(TDaily~s(timedaily.pts))
ts.fit.gamdaily = ts(fitted(gamdaily.fit), start = 2015, frequency = 252)
lines(ts.fit.gamdaily, lwd = 2, col = "blue")
lines(ts.fit.gamweekly, lwd = 2, col = "red")
Response: Weekly vs Monthly Time Series data trend fit Both models are very similar. The trends in both follow a similar pattern increasing just before 2022, decreasing in 2023 and increasing again in 2024.
1d. Consider the return stock price computed as provided in the canvas homework assignment. Apply the formula described in Canvas to compute the return price based on the daily and weekly time series data. Plot the return time series and their corresponding ACF plots. How do the return time series compare in terms of stationarity and serial dependence?
Analyzing weekly and daily return data and comparing with original data
#(Pt - P(t-1))/P(t-1)
daily = numeric(length(TDaily))
for (i in 2 : length (TDaily)) {
daily[i] = (TDaily[i] - TDaily[i-1]) / TDaily[i-1] }
daily.ts <- ts (daily, start= 2012 , freq= 252 )
plot (daily.ts, main= "Daily Return Stock Price" , ylab = "Return Stock Price")
acf(daily.ts,na.action =na.pass,main ="Daily Return Stock price ACF")
weekly = numeric(length(TWeekly))
for (i in 2 : length (TWeekly)) {
weekly[i] = (TWeekly[i] - TWeekly[i-1]) / TWeekly[i-1] }
weekly.ts <- ts (weekly, start= 2012 , freq= 52 )
plot (weekly.ts, main= "Weekly Return Stock Price" , ylab = "Return Stock Price")
acf(weekly.ts,na.action =na.pass,main ="Weekly Return Stock Price ACF")
Response: Return series vs price series analysis There is
plausible stationarity since all values of the daily return data are
within the limit bounds except slightly at around lag .07. The same is
for the weekly return stock data. Most values are within the limit
bounds and there is no seasonality pattern, therefor, showing some
stationarity.
#Question 2: ARIMA(p,d,q) for Stock Price
2a. Divide the data into training and testing data set, where the training data exclude the last two weeks of data: December 21th-December 28th for weekly data, and December 18th-December 28th for daily data, with the testing data including the last 2 weeks of data. Apply the iterative model to fit an ARIMA(p,d,q) model with max AR and MA orders of 10 and difference orders 1 and 2 separately to the training datasets of the daily and weekly data. Display the summary of the final model fit. (If the AIC difference between d=1 and d=2 models is smaller than 2.5, use the simpler model).
dailydays <- 14
weeklydays <- 2
traindaily <- TDaily[1:(length(TDaily)-dailydays)]
testdaily <- TDaily[(length(TDaily)-(dailydays-1)):length(TDaily)]
trainweekly <- TWeekly[1:(length(TWeekly)-weeklydays)]
testweekly <- TWeekly[(length(TWeekly)-(weeklydays-1)):length(TWeekly)]
get_AIC_ARIMA_model <- function (dataset, p, d, q){
mod <- arima(dataset, order=c(p,d,q), method="ML")
aic <- AIC(mod)
df <- data.frame(p,d,q,aic)
names(df) <- c("p", "d", "q", "AIC")
print(paste(p,d,q,aic,sep=" "))
return(df)
}
ARIMA_loop <- function(dataset, maxp, maxd, maxq) {
orders <- data.frame(Inf, Inf, Inf, Inf)
names(orders) <- c("p", "d", "q", "AIC")
for (p in 0:maxp) {
for (d in 0:maxd) {
for (q in 0:maxq) {
possibleError <- tryCatch(
orders <- rbind(orders, get_AIC_ARIMA_model(dataset, p,d,q)),
error=function(e) e
)
if (inherits(possibleError, "error")) next
}
}
}
tail(orders[order(-orders$AIC),])
}
#Difference Order 1 - Daily
ARIMA_loop(traindaily, 10, 1, 10)
## [1] "0 0 0 27554.6837254341"
## [1] "0 0 1 24609.0894799577"
## [1] "0 0 2 22221.0179358179"
## [1] "0 0 3 20426.0427341139"
## [1] "0 0 4 19361.5985093306"
## [1] "0 0 5 18344.9561596289"
## [1] "0 0 6 17853.9319464904"
## [1] "0 0 7 17164.9376073605"
## [1] "0 0 8 16817.7248387941"
## [1] "0 0 9 16519.4188210764"
## [1] "0 0 10 16430.3781284464"
## [1] "0 1 0 14215.2713499592"
## [1] "0 1 1 14215.4135640018"
## [1] "0 1 2 14216.9637411538"
## [1] "0 1 3 14218.8930119808"
## [1] "0 1 4 14210.2857692145"
## [1] "0 1 5 14208.5991191378"
## [1] "0 1 6 14210.1490813706"
## [1] "0 1 7 14193.3372552416"
## [1] "0 1 8 14191.5601479782"
## [1] "0 1 9 14182.74674238"
## [1] "0 1 10 14183.4259566141"
## [1] "1 0 0 14230.1428964396"
## [1] "1 0 1 14230.3681739933"
## [1] "1 0 2 14228.4089719923"
## [1] "1 0 3 14236.629803516"
## [1] "1 0 4 14245.8371098131"
## [1] "1 0 5 14230.1024711308"
## [1] "1 0 6 14234.6469452026"
## [1] "1 0 7 14220.5257626249"
## [1] "1 0 8 14225.3581493455"
## [1] "1 0 9 14202.0329034404"
## [1] "1 0 10 14204.5139823003"
## [1] "1 1 0 14215.3586921384"
## [1] "1 1 1 14215.624885088"
## [1] "1 1 2 14218.9581423921"
## [1] "1 1 3 14218.0848270714"
## [1] "1 1 4 14209.9825424657"
## [1] "1 1 5 14210.5119802574"
## [1] "1 1 6 14206.941827234"
## [1] "1 1 7 14185.0058568263"
## [1] "1 1 8 14186.9788494964"
## [1] "1 1 9 14184.1113624857"
## [1] "1 1 10 14184.3885176721"
## [1] "2 0 0 15718.0334651698"
## [1] "2 0 1 14234.0608248723"
## [1] "2 0 2 14240.7299623645"
## [1] "2 0 4 14240.9168379587"
## [1] "2 0 8 14233.5119649928"
## [1] "2 0 9 14210.4578154014"
## [1] "2 1 0 14216.8818216952"
## [1] "2 1 1 14218.8894438246"
## [1] "2 1 2 14217.7932566178"
## [1] "2 1 3 14220.2026146503"
## [1] "2 1 4 14174.4648447756"
## [1] "2 1 5 14176.4511996967"
## [1] "2 1 6 14205.2424078329"
## [1] "2 1 7 14186.5517376187"
## [1] "2 1 8 14188.0003869694"
## [1] "2 1 9 14166.6182261531"
## [1] "2 1 10 14182.4038191633"
## [1] "3 0 0 14231.792170117"
## [1] "3 0 1 14234.2992059569"
## [1] "3 0 4 14214.8516548857"
## [1] "3 0 6 14210.1007576298"
## [1] "3 0 7 14198.5743956028"
## [1] "3 0 10 14166.7391343308"
## [1] "3 1 0 14218.6988856414"
## [1] "3 1 1 14218.556795229"
## [1] "3 1 2 14219.6308152413"
## [1] "3 1 3 14223.5218755564"
## [1] "3 1 4 14176.0256194442"
## [1] "3 1 5 14176.6893788944"
## [1] "3 1 6 14176.8765566979"
## [1] "3 1 7 14186.8587264807"
## [1] "3 1 8 14156.7378422108"
## [1] "3 1 9 14167.8849950647"
## [1] "3 1 10 14184.4288270292"
## [1] "4 0 0 14233.6339020465"
## [1] "4 0 1 14235.6861507626"
## [1] "4 0 2 14192.8670273794"
## [1] "4 0 4 14226.6422780728"
## [1] "4 0 7 14188.6023393451"
## [1] "4 0 8 14197.952856764"
## [1] "4 1 0 14211.4169114413"
## [1] "4 1 1 14212.9723611375"
## [1] "4 1 2 14174.496575926"
## [1] "4 1 3 14174.8961084154"
## [1] "4 1 4 14179.3320182413"
## [1] "4 1 5 14178.3751883702"
## [1] "4 1 6 14176.6918383366"
## [1] "4 1 7 14186.993691922"
## [1] "4 1 8 14189.0512919565"
## [1] "4 1 9 14169.8998849249"
## [1] "4 1 10 14171.7978851078"
## [1] "5 0 0 14226.156849215"
## [1] "5 0 1 14237.5544204504"
## [1] "5 0 2 14189.377337544"
## [1] "5 0 4 14179.5067354776"
## [1] "5 0 6 14205.4756540242"
## [1] "5 0 7 14197.9936502918"
## [1] "5 0 8 14206.7644008679"
## [1] "5 0 9 14197.3160974868"
## [1] "5 1 0 14212.6909072625"
## [1] "5 1 1 14214.6876952663"
## [1] "5 1 2 14176.2558332916"
## [1] "5 1 3 14176.256567173"
## [1] "5 1 4 14178.689471416"
## [1] "5 1 5 14180.2179421053"
## [1] "5 1 6 14177.1260499859"
## [1] "5 1 7 14168.2186924636"
## [1] "5 1 8 14170.4357642554"
## [1] "5 1 9 14139.8400920693"
## [1] "5 1 10 14170.9521043131"
## [1] "6 0 0 14227.4920875571"
## [1] "6 0 1 14222.4152633447"
## [1] "6 0 2 14202.5909666398"
## [1] "6 0 4 14195.5935748385"
## [1] "6 0 5 14212.2784246754"
## [1] "6 0 6 14171.6753230474"
## [1] "6 0 8 14206.0351487124"
## [1] "6 0 9 14209.1814673117"
## [1] "6 0 10 14186.1595316461"
## [1] "6 1 0 14214.6523369981"
## [1] "6 1 1 14199.9092289959"
## [1] "6 1 2 14216.5971366173"
## [1] "6 1 3 14177.9596471611"
## [1] "6 1 4 14175.4739741422"
## [1] "6 1 5 14177.381984279"
## [1] "6 1 6 14179.2386984226"
## [1] "6 1 7 14163.8908153691"
## [1] "6 1 8 14163.7721665381"
## [1] "6 1 9 14154.2811470414"
## [1] "6 1 10 14152.652168943"
## [1] "7 0 0 14229.4609381574"
## [1] "7 0 1 14231.1064816338"
## [1] "7 0 2 14193.1040304564"
## [1] "7 0 3 14187.8954152077"
## [1] "7 0 4 14184.4974402696"
## [1] "7 0 5 14181.6462317953"
## [1] "7 0 6 14184.7376190503"
## [1] "7 0 7 14184.0003517869"
## [1] "7 0 8 14151.9468732993"
## [1] "7 0 9 14184.0186744596"
## [1] "7 0 10 14167.0543811133"
## [1] "7 1 0 14199.1174382505"
## [1] "7 1 1 14186.9318357078"
## [1] "7 1 2 14185.3779728127"
## [1] "7 1 3 14186.2003495469"
## [1] "7 1 4 14187.3106778139"
## [1] "7 1 5 14163.2577563972"
## [1] "7 1 6 14162.6778832698"
## [1] "7 1 7 14155.0407955582"
## [1] "7 1 8 14156.8380052163"
## [1] "7 1 9 14142.0628210044"
## [1] "7 1 10 14147.5175987337"
## [1] "8 0 0 14213.6162360007"
## [1] "8 0 1 14201.5330646953"
## [1] "8 0 2 14202.1913932856"
## [1] "8 0 3 14194.5149313861"
## [1] "8 0 4 14185.0574040938"
## [1] "8 0 5 14204.1486846741"
## [1] "8 0 6 14186.0204426152"
## [1] "8 0 7 14162.2820742263"
## [1] "8 0 8 14127.4298956269"
## [1] "8 0 9 14140.2523155952"
## [1] "8 0 10 14163.512575652"
## [1] "8 1 0 14196.3768894162"
## [1] "8 1 1 14188.5348188577"
## [1] "8 1 2 14190.7415349392"
## [1] "8 1 3 14188.1304716765"
## [1] "8 1 4 14187.2471665707"
## [1] "8 1 5 14172.57529215"
## [1] "8 1 6 14149.6692867676"
## [1] "8 1 7 14154.9529183656"
## [1] "8 1 8 14106.9121028055"
## [1] "8 1 9 14152.6058221979"
## [1] "8 1 10 14155.2289819333"
## [1] "9 0 0 14211.0356583951"
## [1] "9 0 1 14203.1845953508"
## [1] "9 0 2 14204.3116623731"
## [1] "9 0 3 14205.5761073488"
## [1] "9 0 4 14186.2491212626"
## [1] "9 0 5 14184.360411935"
## [1] "9 0 6 14170.640528332"
## [1] "9 0 7 14144.5268281639"
## [1] "9 0 8 14150.8169513347"
## [1] "9 0 9 14165.2148527848"
## [1] "9 0 10 14125.2309824132"
## [1] "9 1 0 14182.1626396508"
## [1] "9 1 1 14182.5247010325"
## [1] "9 1 2 14164.9137759496"
## [1] "9 1 3 14166.6455005342"
## [1] "9 1 4 14167.6345514102"
## [1] "9 1 5 14168.0040939845"
## [1] "9 1 6 14169.771257832"
## [1] "9 1 7 14172.3340484061"
## [1] "9 1 8 14157.6231973509"
## [1] "9 1 10 14162.6760637426"
## [1] "10 0 0 14196.5102358143"
## [1] "10 0 1 14196.93651571"
## [1] "10 0 2 14179.4585568721"
## [1] "10 0 3 14178.6253445471"
## [1] "10 0 4 14182.1479567814"
## [1] "10 0 5 14157.8550314442"
## [1] "10 0 6 14163.2081105907"
## [1] "10 0 7 14159.8632642078"
## [1] "10 0 8 14125.8807362173"
## [1] "10 0 9 14160.6517206741"
## [1] "10 0 10 14125.8346017473"
## [1] "10 1 0 14180.9492935121"
## [1] "10 1 1 14181.7743853392"
## [1] "10 1 2 14166.7658125868"
## [1] "10 1 3 14166.8786793649"
## [1] "10 1 4 14169.9098963561"
## [1] "10 1 5 14168.3349844606"
## [1] "10 1 6 14145.5741705742"
## [1] "10 1 7 14144.6468169311"
## [1] "10 1 8 14142.8087983919"
## [1] "10 1 9 14154.5603637829"
## [1] "10 1 10 14126.171912689"
## p d q AIC
## 166 8 0 8 14127.43
## 222 10 1 10 14126.17
## 209 10 0 8 14125.88
## 211 10 0 10 14125.83
## 190 9 0 10 14125.23
## 177 8 1 8 14106.91
#Difference Order 2 - Daily
ARIMA_loop(traindaily, 10, 2, 10)
## [1] "0 0 0 27554.6837254341"
## [1] "0 0 1 24609.0894799577"
## [1] "0 0 2 22221.0179358179"
## [1] "0 0 3 20426.0427341139"
## [1] "0 0 4 19361.5985093306"
## [1] "0 0 5 18344.9561596289"
## [1] "0 0 6 17853.9319464904"
## [1] "0 0 7 17164.9376073605"
## [1] "0 0 8 16817.7248387941"
## [1] "0 0 9 16519.4188210764"
## [1] "0 0 10 16430.3781284464"
## [1] "0 1 0 14215.2713499592"
## [1] "0 1 1 14215.4135640018"
## [1] "0 1 2 14216.9637411538"
## [1] "0 1 3 14218.8930119808"
## [1] "0 1 4 14210.2857692145"
## [1] "0 1 5 14208.5991191378"
## [1] "0 1 6 14210.1490813706"
## [1] "0 1 7 14193.3372552416"
## [1] "0 1 8 14191.5601479782"
## [1] "0 1 9 14182.74674238"
## [1] "0 1 10 14183.4259566141"
## [1] "0 2 0 15831.8860434113"
## [1] "0 2 1 14218.9573406719"
## [1] "0 2 2 14219.1157115023"
## [1] "0 2 3 14220.6587398013"
## [1] "0 2 4 14222.590923377"
## [1] "0 2 5 14213.9531501152"
## [1] "0 2 6 14212.2850147996"
## [1] "0 2 7 14213.8257940299"
## [1] "0 2 8 14196.9525717899"
## [1] "0 2 9 14195.2106715105"
## [1] "0 2 10 14186.3310196099"
## [1] "1 0 0 14230.1428964396"
## [1] "1 0 1 14230.3681739933"
## [1] "1 0 2 14228.4089719923"
## [1] "1 0 3 14236.629803516"
## [1] "1 0 4 14245.8371098131"
## [1] "1 0 5 14230.1024711308"
## [1] "1 0 6 14234.6469452026"
## [1] "1 0 7 14220.5257626249"
## [1] "1 0 8 14225.3581493455"
## [1] "1 0 9 14202.0329034404"
## [1] "1 0 10 14204.5139823003"
## [1] "1 1 0 14215.3586921384"
## [1] "1 1 1 14215.624885088"
## [1] "1 1 2 14218.9581423921"
## [1] "1 1 3 14218.0848270714"
## [1] "1 1 4 14209.9825424657"
## [1] "1 1 5 14210.5119802574"
## [1] "1 1 6 14206.941827234"
## [1] "1 1 7 14185.0058568263"
## [1] "1 1 8 14186.9788494964"
## [1] "1 1 9 14184.1113624857"
## [1] "1 1 10 14184.3885176721"
## [1] "1 2 0 15120.5254479848"
## [1] "1 2 1 14219.0608479764"
## [1] "1 2 2 14219.3364801964"
## [1] "1 2 3 14221.1720496417"
## [1] "1 2 4 14222.4086485511"
## [1] "1 2 5 14213.6503010454"
## [1] "1 2 6 14214.1938635612"
## [1] "1 2 7 14202.7485138152"
## [1] "1 2 8 14198.2622277378"
## [1] "1 2 9 14192.2533361921"
## [1] "1 2 10 14187.7045537054"
## [1] "2 0 0 15718.0334651698"
## [1] "2 0 1 14234.0608248723"
## [1] "2 0 2 14240.7299623645"
## [1] "2 0 4 14240.9168379587"
## [1] "2 0 8 14233.5119649928"
## [1] "2 0 9 14210.4578154014"
## [1] "2 1 0 14216.8818216952"
## [1] "2 1 1 14218.8894438246"
## [1] "2 1 2 14217.7932566178"
## [1] "2 1 3 14220.2026146503"
## [1] "2 1 4 14174.4648447756"
## [1] "2 1 5 14176.4511996967"
## [1] "2 1 6 14205.2424078329"
## [1] "2 1 7 14186.5517376187"
## [1] "2 1 8 14188.0003869694"
## [1] "2 1 9 14166.6182261531"
## [1] "2 1 10 14182.4038191633"
## [1] "2 2 0 14868.206067104"
## [1] "2 2 1 14220.5761949831"
## [1] "2 2 2 14221.1941837097"
## [1] "2 2 3 14223.2547399282"
## [1] "2 2 4 14183.0978105807"
## [1] "2 2 5 14178.153901688"
## [1] "2 2 6 14180.1405349913"
## [1] "2 2 7 14192.5560959607"
## [1] "2 2 8 14194.6457774679"
## [1] "2 2 9 14182.881350836"
## [1] "2 2 10 14182.7878038641"
## [1] "3 0 0 14231.792170117"
## [1] "3 0 1 14234.2992059569"
## [1] "3 0 4 14214.8516548857"
## [1] "3 0 6 14210.1007576298"
## [1] "3 0 7 14198.5743956028"
## [1] "3 0 10 14166.7391343308"
## [1] "3 1 0 14218.6988856414"
## [1] "3 1 1 14218.556795229"
## [1] "3 1 2 14219.6308152413"
## [1] "3 1 3 14223.5218755564"
## [1] "3 1 4 14176.0256194442"
## [1] "3 1 5 14176.6893788944"
## [1] "3 1 6 14176.8765566979"
## [1] "3 1 7 14186.8587264807"
## [1] "3 1 8 14156.7378422108"
## [1] "3 1 9 14167.8849950647"
## [1] "3 1 10 14184.4288270292"
## [1] "3 2 0 14656.675565109"
## [1] "3 2 1 14222.3982316971"
## [1] "3 2 2 14222.2619101561"
## [1] "3 2 3 14181.5621805058"
## [1] "3 2 4 14180.3745641756"
## [1] "3 2 5 14179.7810697802"
## [1] "3 2 6 14180.9327037427"
## [1] "3 2 7 14180.8551248198"
## [1] "3 2 8 14194.9626467602"
## [1] "3 2 9 14179.2477356672"
## [1] "3 2 10 14178.5207578992"
## [1] "4 0 0 14233.6339020465"
## [1] "4 0 1 14235.6861507626"
## [1] "4 0 2 14192.8670273794"
## [1] "4 0 4 14226.6422780728"
## [1] "4 0 7 14188.6023393451"
## [1] "4 0 8 14197.952856764"
## [1] "4 1 0 14211.4169114413"
## [1] "4 1 1 14212.9723611375"
## [1] "4 1 2 14174.496575926"
## [1] "4 1 3 14174.8961084154"
## [1] "4 1 4 14179.3320182413"
## [1] "4 1 5 14178.3751883702"
## [1] "4 1 6 14176.6918383366"
## [1] "4 1 7 14186.993691922"
## [1] "4 1 8 14189.0512919565"
## [1] "4 1 9 14169.8998849249"
## [1] "4 1 10 14171.7978851078"
## [1] "4 2 0 14594.9037895988"
## [1] "4 2 1 14215.0763216897"
## [1] "4 2 2 14216.6367911541"
## [1] "4 2 3 14178.1864754821"
## [1] "4 2 4 14178.5072889085"
## [1] "4 2 5 14179.8424759013"
## [1] "4 2 6 14181.8045468442"
## [1] "4 2 7 14181.5220108533"
## [1] "4 2 8 14181.7779433432"
## [1] "4 2 9 14178.7868504604"
## [1] "4 2 10 14183.1634863057"
## [1] "5 0 0 14226.156849215"
## [1] "5 0 1 14237.5544204504"
## [1] "5 0 2 14189.377337544"
## [1] "5 0 4 14179.5067354776"
## [1] "5 0 6 14205.4756540242"
## [1] "5 0 7 14197.9936502918"
## [1] "5 0 8 14206.7644008679"
## [1] "5 0 9 14197.3160974868"
## [1] "5 1 0 14212.6909072625"
## [1] "5 1 1 14214.6876952663"
## [1] "5 1 2 14176.2558332916"
## [1] "5 1 3 14176.256567173"
## [1] "5 1 4 14178.689471416"
## [1] "5 1 5 14180.2179421053"
## [1] "5 1 6 14177.1260499859"
## [1] "5 1 7 14168.2186924636"
## [1] "5 1 8 14170.4357642554"
## [1] "5 1 9 14139.8400920693"
## [1] "5 1 10 14170.9521043131"
## [1] "5 2 0 14542.1466817832"
## [1] "5 2 1 14216.3623348756"
## [1] "5 2 2 14219.0468003705"
## [1] "5 2 3 14179.9409970711"
## [1] "5 2 4 14179.9429741306"
## [1] "5 2 5 14181.8656512063"
## [1] "5 2 6 14183.7817465971"
## [1] "5 2 7 14199.7108094994"
## [1] "5 2 8 14196.1563006363"
## [1] "5 2 9 14183.2882132146"
## [1] "5 2 10 14183.1149778579"
## [1] "6 0 0 14227.4920875571"
## [1] "6 0 1 14222.4152633447"
## [1] "6 0 2 14202.5909666398"
## [1] "6 0 4 14195.5935748385"
## [1] "6 0 5 14212.2784246754"
## [1] "6 0 6 14171.6753230474"
## [1] "6 0 8 14206.0351487124"
## [1] "6 0 9 14209.1814673117"
## [1] "6 0 10 14186.1595316461"
## [1] "6 1 0 14214.6523369981"
## [1] "6 1 1 14199.9092289959"
## [1] "6 1 2 14216.5971366173"
## [1] "6 1 3 14177.9596471611"
## [1] "6 1 4 14175.4739741422"
## [1] "6 1 5 14177.381984279"
## [1] "6 1 6 14179.2386984226"
## [1] "6 1 7 14163.8908153691"
## [1] "6 1 8 14163.7721665381"
## [1] "6 1 9 14154.2811470414"
## [1] "6 1 10 14152.652168943"
## [1] "6 2 0 14438.9466875848"
## [1] "6 2 1 14218.3263219197"
## [1] "6 2 2 14203.6010511466"
## [1] "6 2 3 14191.6327752723"
## [1] "6 2 4 14195.2724483071"
## [1] "6 2 5 14178.3207919285"
## [1] "6 2 6 14199.7397426065"
## [1] "6 2 7 14188.9708013089"
## [1] "6 2 8 14155.7058460999"
## [1] "6 2 9 14190.1757849905"
## [1] "6 2 10 14176.2917942828"
## [1] "7 0 0 14229.4609381574"
## [1] "7 0 1 14231.1064816338"
## [1] "7 0 2 14193.1040304564"
## [1] "7 0 3 14187.8954152077"
## [1] "7 0 4 14184.4974402696"
## [1] "7 0 5 14181.6462317953"
## [1] "7 0 6 14184.7376190503"
## [1] "7 0 7 14184.0003517869"
## [1] "7 0 8 14151.9468732993"
## [1] "7 0 9 14184.0186744596"
## [1] "7 0 10 14167.0543811133"
## [1] "7 1 0 14199.1174382505"
## [1] "7 1 1 14186.9318357078"
## [1] "7 1 2 14185.3779728127"
## [1] "7 1 3 14186.2003495469"
## [1] "7 1 4 14187.3106778139"
## [1] "7 1 5 14163.2577563972"
## [1] "7 1 6 14162.6778832698"
## [1] "7 1 7 14155.0407955582"
## [1] "7 1 8 14156.8380052163"
## [1] "7 1 9 14142.0628210044"
## [1] "7 1 10 14147.5175987337"
## [1] "7 2 0 14432.1914263077"
## [1] "7 2 1 14202.7287719812"
## [1] "7 2 2 14190.567172262"
## [1] "7 2 3 14193.0316153564"
## [1] "7 2 4 14189.9348308769"
## [1] "7 2 5 14192.6581580244"
## [1] "7 2 6 14175.4286191033"
## [1] "7 2 7 14152.7949321431"
## [1] "7 2 8 14144.3022203889"
## [1] "7 2 9 14131.0708797173"
## [1] "7 2 10 14164.0066676367"
## [1] "8 0 0 14213.6162360007"
## [1] "8 0 1 14201.5330646953"
## [1] "8 0 2 14202.1913932856"
## [1] "8 0 3 14194.5149313861"
## [1] "8 0 4 14185.0574040938"
## [1] "8 0 5 14204.1486846741"
## [1] "8 0 6 14186.0204426152"
## [1] "8 0 7 14162.2820742263"
## [1] "8 0 8 14127.4298956269"
## [1] "8 0 9 14140.2523155952"
## [1] "8 0 10 14163.512575652"
## [1] "8 1 0 14196.3768894162"
## [1] "8 1 1 14188.5348188577"
## [1] "8 1 2 14190.7415349392"
## [1] "8 1 3 14188.1304716765"
## [1] "8 1 4 14187.2471665707"
## [1] "8 1 5 14172.57529215"
## [1] "8 1 6 14149.6692867676"
## [1] "8 1 7 14154.9529183656"
## [1] "8 1 8 14106.9121028055"
## [1] "8 1 9 14152.6058221979"
## [1] "8 1 10 14155.2289819333"
## [1] "8 2 0 14361.8209313802"
## [1] "8 2 1 14200.0257825258"
## [1] "8 2 2 14192.1816712727"
## [1] "8 2 3 14179.0986978999"
## [1] "8 2 4 14191.7423720224"
## [1] "8 2 5 14194.296111781"
## [1] "8 2 6 14175.8955279336"
## [1] "8 2 7 14153.4776862689"
## [1] "8 2 8 14143.3964532956"
## [1] "8 2 9 14122.3319672203"
## [1] "8 2 10 14164.9768703772"
## [1] "9 0 0 14211.0356583951"
## [1] "9 0 1 14203.1845953508"
## [1] "9 0 2 14204.3116623731"
## [1] "9 0 3 14205.5761073488"
## [1] "9 0 4 14186.2491212626"
## [1] "9 0 5 14184.360411935"
## [1] "9 0 6 14170.640528332"
## [1] "9 0 7 14144.5268281639"
## [1] "9 0 8 14150.8169513347"
## [1] "9 0 9 14165.2148527848"
## [1] "9 0 10 14125.2309824132"
## [1] "9 1 0 14182.1626396508"
## [1] "9 1 1 14182.5247010325"
## [1] "9 1 2 14164.9137759496"
## [1] "9 1 3 14166.6455005342"
## [1] "9 1 4 14167.6345514102"
## [1] "9 1 5 14168.0040939845"
## [1] "9 1 6 14169.771257832"
## [1] "9 1 7 14172.3340484061"
## [1] "9 1 8 14157.6231973509"
## [1] "9 1 10 14162.6760637426"
## [1] "9 2 0 14359.4067062785"
## [1] "9 2 1 14185.7377746481"
## [1] "9 2 2 14186.1126129553"
## [1] "9 2 3 14168.5378827004"
## [1] "9 2 4 14170.2582689672"
## [1] "9 2 5 14171.2542300613"
## [1] "9 2 6 14171.5667410382"
## [1] "9 2 7 14145.5136774483"
## [1] "9 2 8 14145.5239139825"
## [1] "9 2 9 14147.5230343506"
## [1] "9 2 10 14166.3689477355"
## [1] "10 0 0 14196.5102358143"
## [1] "10 0 1 14196.93651571"
## [1] "10 0 2 14179.4585568721"
## [1] "10 0 3 14178.6253445471"
## [1] "10 0 4 14182.1479567814"
## [1] "10 0 5 14157.8550314442"
## [1] "10 0 6 14163.2081105907"
## [1] "10 0 7 14159.8632642078"
## [1] "10 0 8 14125.8807362173"
## [1] "10 0 9 14160.6517206741"
## [1] "10 0 10 14125.8346017473"
## [1] "10 1 0 14180.9492935121"
## [1] "10 1 1 14181.7743853392"
## [1] "10 1 2 14166.7658125868"
## [1] "10 1 3 14166.8786793649"
## [1] "10 1 4 14169.9098963561"
## [1] "10 1 5 14168.3349844606"
## [1] "10 1 6 14145.5741705742"
## [1] "10 1 7 14144.6468169311"
## [1] "10 1 8 14142.8087983919"
## [1] "10 1 9 14154.5603637829"
## [1] "10 1 10 14126.171912689"
## [1] "10 2 0 14356.2202069154"
## [1] "10 2 1 14184.5593001607"
## [1] "10 2 2 14185.4572708063"
## [1] "10 2 3 14170.3830005969"
## [1] "10 2 4 14172.2012793196"
## [1] "10 2 5 14174.2533919579"
## [1] "10 2 6 14173.5734976164"
## [1] "10 2 7 14151.2098875824"
## [1] "10 2 8 14147.8849547037"
## [1] "10 2 9 14149.6374416882"
## [1] "10 2 10 14130.9990210705"
## p d q AIC
## 332 10 1 10 14126.17
## 319 10 0 8 14125.88
## 321 10 0 10 14125.83
## 289 9 0 10 14125.23
## 277 8 2 9 14122.33
## 265 8 1 8 14106.91
#Difference Order 1 - Weekly
ARIMA_loop(trainweekly, 10, 1, 10)
## [1] "0 0 0 5741.39026284353"
## [1] "0 0 1 5150.04699150449"
## [1] "0 0 2 4741.61081594301"
## [1] "0 0 3 4450.42774374519"
## [1] "0 0 4 4260.39110618474"
## [1] "0 0 5 4149.4685460978"
## [1] "0 0 6 4061.58761155059"
## [1] "0 0 7 3994.2822737179"
## [1] "0 0 8 3932.96550268205"
## [1] "0 0 9 3889.23444233943"
## [1] "0 0 10 3868.50302321664"
## [1] "0 1 0 3646.40835898256"
## [1] "0 1 1 3644.55917634191"
## [1] "0 1 2 3645.6548954191"
## [1] "0 1 3 3647.49676975673"
## [1] "0 1 4 3646.94380164394"
## [1] "0 1 5 3646.82759740676"
## [1] "0 1 6 3648.15834045951"
## [1] "0 1 7 3647.57223284553"
## [1] "0 1 8 3645.63372737762"
## [1] "0 1 9 3647.61558506546"
## [1] "0 1 10 3649.52022112556"
## [1] "1 0 0 3661.44876154144"
## [1] "1 0 1 3659.3723323198"
## [1] "1 0 2 3661.82348748319"
## [1] "1 0 3 3662.86368118921"
## [1] "1 0 4 3659.66204246414"
## [1] "1 0 5 3660.24043552471"
## [1] "1 0 6 3667.63705449794"
## [1] "1 0 7 3667.3135349253"
## [1] "1 0 8 3671.54404304002"
## [1] "1 0 9 3672.68313597214"
## [1] "1 0 10 3670.60455186437"
## [1] "1 1 0 3644.26391042757"
## [1] "1 1 1 3645.81160432955"
## [1] "1 1 2 3647.61607969643"
## [1] "1 1 3 3644.70906386564"
## [1] "1 1 4 3645.55783383481"
## [1] "1 1 5 3648.53637810902"
## [1] "1 1 6 3646.62744243317"
## [1] "1 1 7 3647.22391413279"
## [1] "1 1 8 3647.61454456132"
## [1] "1 1 9 3649.6132918915"
## [1] "1 1 10 3647.09940924336"
## [1] "2 0 0 3659.02179613807"
## [1] "2 0 1 3660.01040942501"
## [1] "2 0 2 3663.76390203723"
## [1] "2 0 3 3668.00877603391"
## [1] "2 0 4 3667.26675391408"
## [1] "2 0 6 3669.75604707746"
## [1] "2 0 7 3667.36567226017"
## [1] "2 0 8 3672.67172777812"
## [1] "2 0 9 3665.50080164943"
## [1] "2 0 10 3677.02286804452"
## [1] "2 1 0 3645.68290668982"
## [1] "2 1 1 3647.68663108103"
## [1] "2 1 2 3631.14189974138"
## [1] "2 1 3 3640.12857363614"
## [1] "2 1 4 3646.50042672166"
## [1] "2 1 5 3633.74980067715"
## [1] "2 1 6 3645.73934079881"
## [1] "2 1 7 3647.60479790915"
## [1] "2 1 8 3647.32972295945"
## [1] "2 1 9 3649.2978037459"
## [1] "2 1 10 3636.13288787037"
## [1] "3 0 0 3820.91290308436"
## [1] "3 0 1 3660.47493580613"
## [1] "3 0 3 3658.68769300573"
## [1] "3 0 4 3652.60862112194"
## [1] "3 0 5 3664.28346373553"
## [1] "3 0 7 3646.92552610338"
## [1] "3 0 8 3638.75176655156"
## [1] "3 0 9 3665.7986962029"
## [1] "3 0 10 3668.718578707"
## [1] "3 1 0 3647.67371731687"
## [1] "3 1 1 3645.11360659969"
## [1] "3 1 2 3632.39913943743"
## [1] "3 1 3 3645.97968015349"
## [1] "3 1 4 3649.72839650666"
## [1] "3 1 5 3631.42748887677"
## [1] "3 1 6 3646.93299327888"
## [1] "3 1 7 3647.79345697517"
## [1] "3 1 8 3649.30760426666"
## [1] "3 1 9 3651.0956161632"
## [1] "3 1 10 3638.12443317282"
## [1] "4 0 0 3662.28929779116"
## [1] "4 0 1 3659.67089298282"
## [1] "4 0 2 3653.01777790836"
## [1] "4 0 3 3658.55899651738"
## [1] "4 0 4 3666.09243083452"
## [1] "4 0 8 3665.59945496896"
## [1] "4 0 9 3654.2047804532"
## [1] "4 0 10 3654.25850015611"
## [1] "4 1 0 3647.7696396943"
## [1] "4 1 1 3646.08472987088"
## [1] "4 1 2 3646.54549044162"
## [1] "4 1 3 3639.79697035468"
## [1] "4 1 4 3649.05669950234"
## [1] "4 1 5 3633.55341468462"
## [1] "4 1 6 3633.79299746448"
## [1] "4 1 7 3651.57443920028"
## [1] "4 1 8 3627.36470066796"
## [1] "4 1 9 3627.41998452188"
## [1] "4 1 10 3641.05914286261"
## [1] "5 0 0 3662.58405414437"
## [1] "5 0 1 3660.78475052804"
## [1] "5 0 2 3651.81522250471"
## [1] "5 0 3 3662.69791927401"
## [1] "5 0 4 3659.57926650594"
## [1] "5 0 6 3649.85534331916"
## [1] "5 0 8 3654.38535136664"
## [1] "5 0 9 3843.64473570825"
## [1] "5 0 10 3662.7358131063"
## [1] "5 1 0 3647.9685803544"
## [1] "5 1 1 3649.6586020007"
## [1] "5 1 2 3634.19787811191"
## [1] "5 1 3 3631.65690727996"
## [1] "5 1 4 3650.27535164315"
## [1] "5 1 5 3641.89209530364"
## [1] "5 1 6 3649.07448711346"
## [1] "5 1 7 3640.15172379276"
## [1] "5 1 8 3647.34637558805"
## [1] "5 1 9 3628.25653654932"
## [1] "5 1 10 3643.09019675032"
## [1] "6 0 0 3662.56772900718"
## [1] "6 0 1 3662.73899728048"
## [1] "6 0 2 3666.37527771522"
## [1] "6 0 3 3661.57449679424"
## [1] "6 0 4 3662.91337655488"
## [1] "6 0 5 3655.44285600768"
## [1] "6 0 6 3665.64866845118"
## [1] "6 0 7 3645.44951450055"
## [1] "6 0 8 3654.92360962047"
## [1] "6 0 9 3663.015825075"
## [1] "6 0 10 3652.19287565822"
## [1] "6 1 0 3648.79450671543"
## [1] "6 1 1 3647.27592381013"
## [1] "6 1 2 3646.61306472429"
## [1] "6 1 3 3634.45668006911"
## [1] "6 1 4 3633.68739144559"
## [1] "6 1 5 3631.91972460358"
## [1] "6 1 6 3632.98692450256"
## [1] "6 1 7 3642.12016876221"
## [1] "6 1 8 3629.00139738315"
## [1] "6 1 9 3657.21898839892"
## [1] "6 1 10 3623.77860854847"
## [1] "7 0 0 3663.54659821252"
## [1] "7 0 1 3664.39676401378"
## [1] "7 0 2 3668.21639221719"
## [1] "7 0 3 3668.01671832043"
## [1] "7 0 4 3652.92044517496"
## [1] "7 0 5 3652.03672253035"
## [1] "7 0 6 3639.85613758365"
## [1] "7 0 7 3641.84639623981"
## [1] "7 0 8 3655.46793143922"
## [1] "7 0 9 3651.26981111496"
## [1] "7 0 10 3638.23960616319"
## [1] "7 1 0 3646.32191662439"
## [1] "7 1 1 3647.62981065949"
## [1] "7 1 2 3648.58630735223"
## [1] "7 1 3 3648.87558846906"
## [1] "7 1 4 3650.43122081651"
## [1] "7 1 5 3638.73030515022"
## [1] "7 1 6 3652.80004122118"
## [1] "7 1 7 3643.04835690663"
## [1] "7 1 8 3657.60066264719"
## [1] "7 1 9 3640.59254859933"
## [1] "7 1 10 3628.08354413961"
## [1] "8 0 0 3661.32466906092"
## [1] "8 0 1 3666.23735975453"
## [1] "8 0 2 3656.32798046861"
## [1] "8 0 3 3666.50993729308"
## [1] "8 0 4 3654.61047631371"
## [1] "8 0 5 3653.46311339189"
## [1] "8 0 6 3642.04348269102"
## [1] "8 0 7 3639.9138362811"
## [1] "8 0 8 3656.99555787402"
## [1] "8 0 9 3649.49660976963"
## [1] "8 0 10 3642.70077092662"
## [1] "8 1 0 3647.34952716581"
## [1] "8 1 1 3644.37925211352"
## [1] "8 1 2 3651.30621740791"
## [1] "8 1 3 3635.03034855045"
## [1] "8 1 4 3632.04382428224"
## [1] "8 1 5 3644.65260930048"
## [1] "8 1 6 3645.2315247736"
## [1] "8 1 7 3646.74264759812"
## [1] "8 1 8 3627.82226161583"
## [1] "8 1 9 3632.41451784965"
## [1] "8 1 10 3629.31360275834"
## [1] "9 0 0 3662.45513398472"
## [1] "9 0 1 3659.46584776929"
## [1] "9 0 2 3658.44052084441"
## [1] "9 0 3 3641.82488746801"
## [1] "9 0 4 3643.77738093444"
## [1] "9 0 5 3641.91444394635"
## [1] "9 0 6 3640.24703028538"
## [1] "9 0 7 3644.39457123185"
## [1] "9 0 8 3633.01169732795"
## [1] "9 0 9 3646.64127263505"
## [1] "9 0 10 3641.31400577593"
## [1] "9 1 0 3649.2861010245"
## [1] "9 1 1 3651.28643957768"
## [1] "9 1 2 3648.37391088208"
## [1] "9 1 3 3630.59407683839"
## [1] "9 1 4 3628.31022023195"
## [1] "9 1 5 3631.34072416038"
## [1] "9 1 6 3647.05579155902"
## [1] "9 1 7 3648.81963338879"
## [1] "9 1 8 3632.27763442581"
## [1] "9 1 9 3631.52402730169"
## [1] "9 1 10 3631.60172475457"
## [1] "10 0 0 3664.35839737418"
## [1] "10 0 1 3661.279437832"
## [1] "10 0 2 3658.75144278935"
## [1] "10 0 3 3644.18441701262"
## [1] "10 0 4 3642.81347182292"
## [1] "10 0 5 3661.40072862691"
## [1] "10 0 6 3657.26536427762"
## [1] "10 0 7 3663.42610634882"
## [1] "10 0 8 3645.27218353488"
## [1] "10 0 9 3657.19431117468"
## [1] "10 0 10 3646.85016906273"
## [1] "10 1 0 3651.28266958853"
## [1] "10 1 1 3653.28602454772"
## [1] "10 1 2 3649.93899091703"
## [1] "10 1 3 3641.89879556637"
## [1] "10 1 4 3629.80698848156"
## [1] "10 1 5 3629.87803463767"
## [1] "10 1 6 3649.13658077892"
## [1] "10 1 7 3650.19889538862"
## [1] "10 1 8 3628.72587736181"
## [1] "10 1 9 3627.27701216168"
## [1] "10 1 10 3647.46790169301"
## p d q AIC
## 169 7 1 10 3628.084
## 189 8 1 8 3627.822
## 104 4 1 9 3627.420
## 103 4 1 8 3627.365
## 234 10 1 9 3627.277
## 147 6 1 10 3623.779
#Difference Order 2 - Weekly
ARIMA_loop(trainweekly, 10, 2, 10)
## [1] "0 0 0 5741.39026284353"
## [1] "0 0 1 5150.04699150449"
## [1] "0 0 2 4741.61081594301"
## [1] "0 0 3 4450.42774374519"
## [1] "0 0 4 4260.39110618474"
## [1] "0 0 5 4149.4685460978"
## [1] "0 0 6 4061.58761155059"
## [1] "0 0 7 3994.2822737179"
## [1] "0 0 8 3932.96550268205"
## [1] "0 0 9 3889.23444233943"
## [1] "0 0 10 3868.50302321664"
## [1] "0 1 0 3646.40835898256"
## [1] "0 1 1 3644.55917634191"
## [1] "0 1 2 3645.6548954191"
## [1] "0 1 3 3647.49676975673"
## [1] "0 1 4 3646.94380164394"
## [1] "0 1 5 3646.82759740676"
## [1] "0 1 6 3648.15834045951"
## [1] "0 1 7 3647.57223284553"
## [1] "0 1 8 3645.63372737762"
## [1] "0 1 9 3647.61558506546"
## [1] "0 1 10 3649.52022112556"
## [1] "0 2 0 3916.33902839628"
## [1] "0 2 1 3646.93985466492"
## [1] "0 2 2 3645.04768993328"
## [1] "0 2 3 3646.11249720868"
## [1] "0 2 4 3647.94059985331"
## [1] "0 2 5 3647.4523326471"
## [1] "0 2 6 3647.27373925027"
## [1] "0 2 7 3648.64440395513"
## [1] "0 2 8 3648.12338632455"
## [1] "0 2 9 3646.22515475484"
## [1] "0 2 10 3648.20723926851"
## [1] "1 0 0 3661.44876154144"
## [1] "1 0 1 3659.3723323198"
## [1] "1 0 2 3661.82348748319"
## [1] "1 0 3 3662.86368118921"
## [1] "1 0 4 3659.66204246414"
## [1] "1 0 5 3660.24043552471"
## [1] "1 0 6 3667.63705449794"
## [1] "1 0 7 3667.3135349253"
## [1] "1 0 8 3671.54404304002"
## [1] "1 0 9 3672.68313597214"
## [1] "1 0 10 3670.60455186437"
## [1] "1 1 0 3644.26391042757"
## [1] "1 1 1 3645.81160432955"
## [1] "1 1 2 3647.61607969643"
## [1] "1 1 3 3644.70906386564"
## [1] "1 1 4 3645.55783383481"
## [1] "1 1 5 3648.53637810902"
## [1] "1 1 6 3646.62744243317"
## [1] "1 1 7 3647.22391413279"
## [1] "1 1 8 3647.61454456132"
## [1] "1 1 9 3649.6132918915"
## [1] "1 1 10 3647.09940924336"
## [1] "1 2 0 3801.05998841338"
## [1] "1 2 1 3644.74264868498"
## [1] "1 2 2 3646.2652030281"
## [1] "1 2 3 3648.06621099368"
## [1] "1 2 4 3645.14859765356"
## [1] "1 2 5 3646.03843041875"
## [1] "1 2 6 3648.99990629005"
## [1] "1 2 7 3650.98973129381"
## [1] "1 2 8 3647.80375894353"
## [1] "1 2 9 3646.48843842058"
## [1] "1 2 10 3648.34347866776"
## [1] "2 0 0 3659.02179613807"
## [1] "2 0 1 3660.01040942501"
## [1] "2 0 2 3663.76390203723"
## [1] "2 0 3 3668.00877603391"
## [1] "2 0 4 3667.26675391408"
## [1] "2 0 6 3669.75604707746"
## [1] "2 0 7 3667.36567226017"
## [1] "2 0 8 3672.67172777812"
## [1] "2 0 9 3665.50080164943"
## [1] "2 0 10 3677.02286804452"
## [1] "2 1 0 3645.68290668982"
## [1] "2 1 1 3647.68663108103"
## [1] "2 1 2 3631.14189974138"
## [1] "2 1 3 3640.12857363614"
## [1] "2 1 4 3646.50042672166"
## [1] "2 1 5 3633.74980067715"
## [1] "2 1 6 3645.73934079881"
## [1] "2 1 7 3647.60479790915"
## [1] "2 1 8 3647.32972295945"
## [1] "2 1 9 3649.2978037459"
## [1] "2 1 10 3636.13288787037"
## [1] "2 2 0 3759.38720387889"
## [1] "2 2 1 3646.13575785136"
## [1] "2 2 2 3648.13624467166"
## [1] "2 2 3 3650.24757020054"
## [1] "2 2 4 3637.6468364768"
## [1] "2 2 5 3649.14438395027"
## [1] "2 2 6 3644.93599019464"
## [1] "2 2 7 3646.34068062974"
## [1] "2 2 8 3648.25256542316"
## [1] "2 2 9 3647.91616532057"
## [1] "2 2 10 3649.96780049399"
## [1] "3 0 0 3820.91290308436"
## [1] "3 0 1 3660.47493580613"
## [1] "3 0 3 3658.68769300573"
## [1] "3 0 4 3652.60862112194"
## [1] "3 0 5 3664.28346373553"
## [1] "3 0 7 3646.92552610338"
## [1] "3 0 8 3638.75176655156"
## [1] "3 0 9 3665.7986962029"
## [1] "3 0 10 3668.718578707"
## [1] "3 1 0 3647.67371731687"
## [1] "3 1 1 3645.11360659969"
## [1] "3 1 2 3632.39913943743"
## [1] "3 1 3 3645.97968015349"
## [1] "3 1 4 3649.72839650666"
## [1] "3 1 5 3631.42748887677"
## [1] "3 1 6 3646.93299327888"
## [1] "3 1 7 3647.79345697517"
## [1] "3 1 8 3649.30760426666"
## [1] "3 1 9 3651.0956161632"
## [1] "3 1 10 3638.12443317282"
## [1] "3 2 0 3746.49641625062"
## [1] "3 2 1 3648.12299166416"
## [1] "3 2 2 3645.55236693342"
## [1] "3 2 3 3636.97736245594"
## [1] "3 2 4 3631.51795735479"
## [1] "3 2 5 3633.51881806652"
## [1] "3 2 6 3631.88186938206"
## [1] "3 2 7 3633.87990110549"
## [1] "3 2 8 3648.37235138007"
## [1] "3 2 9 3649.91581132483"
## [1] "3 2 10 3651.89317506561"
## [1] "4 0 0 3662.28929779116"
## [1] "4 0 1 3659.67089298282"
## [1] "4 0 2 3653.01777790836"
## [1] "4 0 3 3658.55899651738"
## [1] "4 0 4 3666.09243083452"
## [1] "4 0 8 3665.59945496896"
## [1] "4 0 9 3654.2047804532"
## [1] "4 0 10 3654.25850015611"
## [1] "4 1 0 3647.7696396943"
## [1] "4 1 1 3646.08472987088"
## [1] "4 1 2 3646.54549044162"
## [1] "4 1 3 3639.79697035468"
## [1] "4 1 4 3649.05669950234"
## [1] "4 1 5 3633.55341468462"
## [1] "4 1 6 3633.79299746448"
## [1] "4 1 7 3651.57443920028"
## [1] "4 1 8 3627.36470066796"
## [1] "4 1 9 3627.41998452188"
## [1] "4 1 10 3641.05914286261"
## [1] "4 2 0 3717.49113471055"
## [1] "4 2 1 3648.26552301734"
## [1] "4 2 2 3648.76991924096"
## [1] "4 2 3 3637.66723285501"
## [1] "4 2 4 3648.45229445197"
## [1] "4 2 5 3650.47203245889"
## [1] "4 2 6 3633.88351536896"
## [1] "4 2 7 3635.87169984862"
## [1] "4 2 8 3650.44317406219"
## [1] "4 2 9 3625.23050438516"
## [1] "4 2 10 3639.24113579613"
## [1] "5 0 0 3662.58405414437"
## [1] "5 0 1 3660.78475052804"
## [1] "5 0 2 3651.81522250471"
## [1] "5 0 3 3662.69791927401"
## [1] "5 0 4 3659.57926650594"
## [1] "5 0 6 3649.85534331916"
## [1] "5 0 8 3654.38535136664"
## [1] "5 0 9 3843.64473570825"
## [1] "5 0 10 3662.7358131063"
## [1] "5 1 0 3647.9685803544"
## [1] "5 1 1 3649.6586020007"
## [1] "5 1 2 3634.19787811191"
## [1] "5 1 3 3631.65690727996"
## [1] "5 1 4 3650.27535164315"
## [1] "5 1 5 3641.89209530364"
## [1] "5 1 6 3649.07448711346"
## [1] "5 1 7 3640.15172379276"
## [1] "5 1 8 3647.34637558805"
## [1] "5 1 9 3628.25653654932"
## [1] "5 1 10 3643.09019675032"
## [1] "5 2 0 3713.75021134966"
## [1] "5 2 1 3648.42274542887"
## [1] "5 2 2 3650.11982923522"
## [1] "5 2 3 3634.62473239659"
## [1] "5 2 4 3632.10965713287"
## [1] "5 2 5 3634.10494140213"
## [1] "5 2 6 3636.67475532924"
## [1] "5 2 7 3651.56852892015"
## [1] "5 2 8 3651.01843112481"
## [1] "5 2 9 3626.61313379351"
## [1] "5 2 10 3636.34548398346"
## [1] "6 0 0 3662.56772900718"
## [1] "6 0 1 3662.73899728048"
## [1] "6 0 2 3666.37527771522"
## [1] "6 0 3 3661.57449679424"
## [1] "6 0 4 3662.91337655488"
## [1] "6 0 5 3655.44285600768"
## [1] "6 0 6 3665.64866845118"
## [1] "6 0 7 3645.44951450055"
## [1] "6 0 8 3654.92360962047"
## [1] "6 0 9 3663.015825075"
## [1] "6 0 10 3652.19287565822"
## [1] "6 1 0 3648.79450671543"
## [1] "6 1 1 3647.27592381013"
## [1] "6 1 2 3646.61306472429"
## [1] "6 1 3 3634.45668006911"
## [1] "6 1 4 3633.68739144559"
## [1] "6 1 5 3631.91972460358"
## [1] "6 1 6 3632.98692450256"
## [1] "6 1 7 3642.12016876221"
## [1] "6 1 8 3629.00139738315"
## [1] "6 1 9 3657.21898839892"
## [1] "6 1 10 3623.77860854847"
## [1] "6 2 0 3713.96231242053"
## [1] "6 2 1 3649.28902834262"
## [1] "6 2 2 3647.84624981302"
## [1] "6 2 3 3647.18620214757"
## [1] "6 2 4 3634.10094759803"
## [1] "6 2 5 3636.1053086789"
## [1] "6 2 6 3651.56497721149"
## [1] "6 2 7 3653.55952177864"
## [1] "6 2 8 3624.99515086767"
## [1] "6 2 9 3630.47446049113"
## [1] "6 2 10 3658.39736513615"
## [1] "7 0 0 3663.54659821252"
## [1] "7 0 1 3664.39676401378"
## [1] "7 0 2 3668.21639221719"
## [1] "7 0 3 3668.01671832043"
## [1] "7 0 4 3652.92044517496"
## [1] "7 0 5 3652.03672253035"
## [1] "7 0 6 3639.85613758365"
## [1] "7 0 7 3641.84639623981"
## [1] "7 0 8 3655.46793143922"
## [1] "7 0 9 3651.26981111496"
## [1] "7 0 10 3638.23960616319"
## [1] "7 1 0 3646.32191662439"
## [1] "7 1 1 3647.62981065949"
## [1] "7 1 2 3648.58630735223"
## [1] "7 1 3 3648.87558846906"
## [1] "7 1 4 3650.43122081651"
## [1] "7 1 5 3638.73030515022"
## [1] "7 1 6 3652.80004122118"
## [1] "7 1 7 3643.04835690663"
## [1] "7 1 8 3657.60066264719"
## [1] "7 1 9 3640.59254859933"
## [1] "7 1 10 3628.08354413961"
## [1] "7 2 0 3710.21973379394"
## [1] "7 2 1 3646.88685674859"
## [1] "7 2 2 3648.21207827286"
## [1] "7 2 3 3649.16242984474"
## [1] "7 2 4 3649.5038463846"
## [1] "7 2 5 3649.34997421977"
## [1] "7 2 6 3648.13136707079"
## [1] "7 2 7 3642.3033890873"
## [1] "7 2 8 3624.9959933961"
## [1] "7 2 9 3629.91165014236"
## [1] "7 2 10 3643.88860279001"
## [1] "8 0 0 3661.32466906092"
## [1] "8 0 1 3666.23735975453"
## [1] "8 0 2 3656.32798046861"
## [1] "8 0 3 3666.50993729308"
## [1] "8 0 4 3654.61047631371"
## [1] "8 0 5 3653.46311339189"
## [1] "8 0 6 3642.04348269102"
## [1] "8 0 7 3639.9138362811"
## [1] "8 0 8 3656.99555787402"
## [1] "8 0 9 3649.49660976963"
## [1] "8 0 10 3642.70077092662"
## [1] "8 1 0 3647.34952716581"
## [1] "8 1 1 3644.37925211352"
## [1] "8 1 2 3651.30621740791"
## [1] "8 1 3 3635.03034855045"
## [1] "8 1 4 3632.04382428224"
## [1] "8 1 5 3644.65260930048"
## [1] "8 1 6 3645.2315247736"
## [1] "8 1 7 3646.74264759812"
## [1] "8 1 8 3627.82226161583"
## [1] "8 1 9 3632.41451784965"
## [1] "8 1 10 3629.31360275834"
## [1] "8 2 0 3701.63892878684"
## [1] "8 2 1 3647.93537349236"
## [1] "8 2 2 3644.96423354514"
## [1] "8 2 3 3627.2789589679"
## [1] "8 2 4 3627.20654652603"
## [1] "8 2 5 3630.79006709211"
## [1] "8 2 6 3631.37519360805"
## [1] "8 2 7 3631.29612336036"
## [1] "8 2 8 3632.10764123408"
## [1] "8 2 9 3628.664842465"
## [1] "8 2 10 3630.67368787414"
## [1] "9 0 0 3662.45513398472"
## [1] "9 0 1 3659.46584776929"
## [1] "9 0 2 3658.44052084441"
## [1] "9 0 3 3641.82488746801"
## [1] "9 0 4 3643.77738093444"
## [1] "9 0 5 3641.91444394635"
## [1] "9 0 6 3640.24703028538"
## [1] "9 0 7 3644.39457123185"
## [1] "9 0 8 3633.01169732795"
## [1] "9 0 9 3646.64127263505"
## [1] "9 0 10 3641.31400577593"
## [1] "9 1 0 3649.2861010245"
## [1] "9 1 1 3651.28643957768"
## [1] "9 1 2 3648.37391088208"
## [1] "9 1 3 3630.59407683839"
## [1] "9 1 4 3628.31022023195"
## [1] "9 1 5 3631.34072416038"
## [1] "9 1 6 3647.05579155902"
## [1] "9 1 7 3648.81963338879"
## [1] "9 1 8 3632.27763442581"
## [1] "9 1 9 3631.52402730169"
## [1] "9 1 10 3631.60172475457"
## [1] "9 2 0 3696.57784420403"
## [1] "9 2 1 3649.870034193"
## [1] "9 2 2 3646.8276923159"
## [1] "9 2 3 3648.88531819696"
## [1] "9 2 4 3644.18832490129"
## [1] "9 2 5 3632.4264795317"
## [1] "9 2 6 3643.42323839726"
## [1] "9 2 7 3634.89399852607"
## [1] "9 2 8 3627.47082028182"
## [1] "9 2 10 3632.49021463645"
## [1] "10 0 0 3664.35839737418"
## [1] "10 0 1 3661.279437832"
## [1] "10 0 2 3658.75144278935"
## [1] "10 0 3 3644.18441701262"
## [1] "10 0 4 3642.81347182292"
## [1] "10 0 5 3661.40072862691"
## [1] "10 0 6 3657.26536427762"
## [1] "10 0 7 3663.42610634882"
## [1] "10 0 8 3645.27218353488"
## [1] "10 0 9 3657.19431117468"
## [1] "10 0 10 3646.85016906273"
## [1] "10 1 0 3651.28266958853"
## [1] "10 1 1 3653.28602454772"
## [1] "10 1 2 3649.93899091703"
## [1] "10 1 3 3641.89879556637"
## [1] "10 1 4 3629.80698848156"
## [1] "10 1 5 3629.87803463767"
## [1] "10 1 6 3649.13658077892"
## [1] "10 1 7 3650.19889538862"
## [1] "10 1 8 3628.72587736181"
## [1] "10 1 9 3627.27701216168"
## [1] "10 1 10 3647.46790169301"
## [1] "10 2 0 3688.31950971153"
## [1] "10 2 1 3651.86640714514"
## [1] "10 2 2 3647.42906333913"
## [1] "10 2 3 3627.9171193655"
## [1] "10 2 4 3652.55760240984"
## [1] "10 2 5 3628.82132072955"
## [1] "10 2 6 3626.15126788546"
## [1] "10 2 7 3626.05929592071"
## [1] "10 2 8 3627.20038902496"
## [1] "10 2 9 3631.38725285955"
## [1] "10 2 10 3629.6000074906"
## p d q AIC
## 351 10 2 6 3626.151
## 352 10 2 7 3626.059
## 159 4 2 9 3625.231
## 255 7 2 8 3624.996
## 222 6 2 8 3624.995
## 213 6 1 10 3623.779
arimadaily <- arima(traindaily, method = "ML", order = c(8,1,8))
arimadaily
##
## Call:
## arima(x = traindaily, order = c(8, 1, 8), method = "ML")
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5 ar6 ar7 ar8
## -0.1602 0.6777 -0.0263 -0.315 0.1392 0.5463 -0.2062 -0.8810
## s.e. 0.0363 0.0679 0.0307 0.077 0.0845 0.0283 0.0662 0.0457
## ma1 ma2 ma3 ma4 ma5 ma6 ma7 ma8
## 0.1979 -0.6943 0.0164 0.3580 -0.1625 -0.5816 0.2957 0.9045
## s.e. 0.0510 0.0636 0.0286 0.0885 0.0679 0.0360 0.0653 0.0280
##
## sigma^2 estimated as 30.5: log likelihood = -7036.46, aic = 14106.91
arimaweekly <- arima(trainweekly, method = "ML", order = c(6,1,10))
arimaweekly
##
## Call:
## arima(x = trainweekly, order = c(6, 1, 10), method = "ML")
##
## Coefficients:
## ar1 ar2 ar3 ar4 ar5 ar6 ma1 ma2
## -0.5073 -0.1046 -1.0407 -0.1855 0.2833 -0.3673 0.6607 0.1901
## s.e. 0.2572 0.2832 0.2255 0.2716 0.2256 0.1881 0.2532 0.3139
## ma3 ma4 ma5 ma6 ma7 ma8 ma9 ma10
## 1.1419 0.2877 -0.2522 0.3885 -0.1556 -0.0736 -0.0656 -0.2260
## s.e. 0.2605 0.3190 0.2767 0.2387 0.0816 0.0611 0.0612 0.0483
##
## sigma^2 estimated as 125.6: log likelihood = -1794.89, aic = 3623.78
Response: Analysis of the ARIMA Fit for the Weekly and Monthly Data The model summary is displayed within the code for p=8, d=1, q=8 for the daily data and p=6, d=1, q=10 for the weekly data, which had the lowest AIC value.
2b. Evaluate the model residuals and squared residuals using the ACF and PACF plots as well as hypothesis testing for serial correlation for both daily and weekly data. What would you conclude based on this analysis?
#Daily Data
par (mfrow = c ( 2 , 2 ))
dailyresiduals <- resid (arimadaily)
plot (dailyresiduals, ylab = 'Residuals' , main = "Daily Residual Plot for arimadaily" )
hist (dailyresiduals, xlab = 'Residuals' , main = 'Daily Residual Histogram Plot' )
acf (dailyresiduals, main = "Daily ACF Residual:arimadaily")
pacf (dailyresiduals, main = "Daily PACF Residual:arimadaily")
qqnorm(dailyresiduals)
qqline(dailyresiduals)
#Weekly Data
par (mfrow = c ( 2 , 2 ))
weeklyresiduals <- resid (arimaweekly)
plot (weeklyresiduals, ylab = 'Residuals' , main = "Weekly Residual Plot for arimaweekly" )
hist (weeklyresiduals, xlab = 'Residuals' , main = 'Weekly Residual Histogram Plot' )
acf (weeklyresiduals, main = "Weekly ACF Residual:arimaweekly")
pacf (weeklyresiduals, main = "Weekly PACF Residual:arimaweekly")
qqnorm(weeklyresiduals)
qqline(weeklyresiduals)
L.box.testing <- function(model,p,d,q){
lag <- p+q+1
fitdf <- p+q
Box.test(resid(model), lag=lag, type="Ljung-Box", fitdf = fitdf)
}
P.box.testing<- function(model,p,d,q){
lag <- p+q+1
fitdf <- p+q
Box.test(resid(model), lag=lag, type="Box-Pierce", fitdf = fitdf)
}
L.box.testing(arimadaily, 8,1,8)
##
## Box-Ljung test
##
## data: resid(model)
## X-squared = 26.527, df = 1, p-value = 2.599e-07
P.box.testing(arimadaily, 8,1,8)
##
## Box-Pierce test
##
## data: resid(model)
## X-squared = 26.434, df = 1, p-value = 2.727e-07
L.box.testing(arimaweekly, 6,1,10)
##
## Box-Ljung test
##
## data: resid(model)
## X-squared = 6.7857, df = 1, p-value = 0.009189
P.box.testing(arimaweekly, 6,1,10)
##
## Box-Pierce test
##
## data: resid(model)
## X-squared = 6.535, df = 1, p-value = 0.01058
Response:ARIMA residual analysis for the Weekly and Monthly
Data P-values obtained from the data are small (less than 0.05),
suggesting that there is significant evidence to reject the null
hypothesis. In other words, there is evidence of autocorrelation present
in the residuals. Residuals are not adequately capturing all the
temporal dependencies present in the data.
Q-Q plots show that the data is not normal.
2c. Apply the models identified in (2a) and forecast the last two weeks of data using both daily and weekly data. Plot the predicted data to compare the predicted values to the actual observed ones. Include 95% confidence intervals for the forecasts in the corresponding plots.
data.ts = ts(weeklydata$AdjClose,start=c(2015),freq=52)
n=length(data.ts)
nfit = n-2
testweekly
## [1] 261.44 238.45
arimapredweekly = as.vector(predict(arimaweekly, n.ahead=2))
vol=time(data.ts)
ubound=arimapredweekly$pred+1.96*arimapredweekly$se
lbound=arimapredweekly$pred-1.96*arimapredweekly$se
ymin=min(lbound)
ymax=max(ubound)
par(mar = c(5, 4, 4, 2) + 0.1)
par(mfrow = c(1, 1)) # Reset number of plots per row and column to 1
par(mfcol = c(1, 1)) # Reset number of plots per column and row to 1
plot(vol[nfit:n], weeklydata$AdjClose[nfit:n], type="l", ylim=c(ymin, ymax), xlab="Time", ylab="Weekly Price", main="ARIMA Weekly Predictions")
points(vol[(nfit+1):n], arimapredweekly$pred, col="red")
lines(vol[(nfit+1):n], ubound, lty=3, lwd=2, col="blue")
lines(vol[(nfit+1):n], lbound, lty=3, lwd=2, col="blue")
data.ts.daily = ts(dailydata$AdjClose,start=c(2015),freq=252)
ndaily=length(data.ts.daily)
nfitdaily = ndaily-14
testdaily
## [1] 243.84 239.74 237.01 239.29 251.05 253.50 252.08 257.22 247.14 254.50
## [11] 252.54 256.61 261.44 253.18
arimapreddaily = as.vector(predict(arimadaily, n.ahead=14))
voldaily=time(data.ts.daily)
ubound2=arimapreddaily$pred+1.96*arimapreddaily$se
lbound2=arimapreddaily$pred-1.96*arimapreddaily$se
ymin2=min(lbound2)
ymax2=max(ubound2)
par(mar = c(5, 4, 4, 2) + 0.1)
par(mfrow = c(1, 1)) # Reset number of plots per row and column to 1
par(mfcol = c(1, 1)) # Reset number of plots per column and row to 1
plot(voldaily[nfitdaily:ndaily], dailydata$AdjClose[nfitdaily:ndaily], type="l", ylim=c(ymin2, ymax2), xlab="Time", ylab="Daily Price", main="ARIMA Daily Predictions")
points(voldaily[(nfitdaily+1):ndaily], arimapreddaily$pred, col="red")
lines(voldaily[(nfitdaily+1):ndaily], ubound2, lty=3, lwd=2, col="blue")
lines(voldaily[(nfitdaily+1):ndaily], lbound2, lty=3, lwd=2, col="blue")
#daily.pred = as.vector(predict(arimadaily), n.ahead=14)
Response: Predictions ARIMA daily predictions seem better than the weekly. Both are within the 95% confidence bounds.
2d. Calculate Mean Absolute Percentage Error (MAPE) and Precision Measure (PM). How many observations are within the prediction bands? Compare the accuracy of the predictions for the daily and weekly time series using these two measures.
predsweekly=arimapredweekly$pred
obsweekly=weeklydata$AdjClose[(nfit+1):n]
predsweekly
## Time Series:
## Start = 469
## End = 470
## Frequency = 1
## [1] 240.9178 244.3324
obsweekly
## [1] 261.44 238.45
#Precision
sum((predsweekly-obsweekly)^2)/sum((obsweekly-mean(obsweekly))^2)
## [1] 1.724614
#MAPE
100*mean(abs(predsweekly-obsweekly)/obsweekly)
## [1] 5.158308
predsdaily = arimapreddaily$pred
obsdaily=dailydata$AdjClose[(nfitdaily+1):ndaily]
predsdaily
## Time Series:
## Start = 2250
## End = 2263
## Frequency = 1
## [1] 242.5347 244.4782 245.2138 245.5854 245.9849 245.9173 245.3194 244.0460
## [9] 243.8664 241.4640 241.4332 239.6861 239.2233 238.3332
obsdaily
## [1] 243.84 239.74 237.01 239.29 251.05 253.50 252.08 257.22 247.14 254.50
## [11] 252.54 256.61 261.44 253.18
#Precision
sum((predsdaily-obsdaily)^2)/sum((obsdaily-mean(obsdaily))^2)
## [1] 2.427396
#MAPE
100*mean(abs(predsdaily-obsdaily)/obsdaily)
## [1] 3.801309
Response: Prediction Comparison
Dally precision is 7.40 and the daily MAPE is 6.62%. Weekly precision is 1.72 and the weekly MAPE is 5.16%. The accuracy of the predictions is better for the weekly model.
3a. Divide the data into training and testing data set, where the training data exclude the last two weeks of data : December 21th-December 28th for weekly data, and December 18th-December 28th for daily data. Apply the iterative model to fit an ARMA(p,q)-GARCH(m,n) model by selecting the orders for p & q up to 5 and orders for m & n up to 2. Display the summary of the final model fit. Write up the equation of the estimated model.
ARIMA_loop(traindaily, 5, 0, 5)
## [1] "0 0 0 27554.6837254341"
## [1] "0 0 1 24609.0894799577"
## [1] "0 0 2 22221.0179358179"
## [1] "0 0 3 20426.0427341139"
## [1] "0 0 4 19361.5985093306"
## [1] "0 0 5 18344.9561596289"
## [1] "1 0 0 14230.1428964396"
## [1] "1 0 1 14230.3681739933"
## [1] "1 0 2 14228.4089719923"
## [1] "1 0 3 14236.629803516"
## [1] "1 0 4 14245.8371098131"
## [1] "1 0 5 14230.1024711308"
## [1] "2 0 0 15718.0334651698"
## [1] "2 0 1 14234.0608248723"
## [1] "2 0 2 14240.7299623645"
## [1] "2 0 4 14240.9168379587"
## [1] "3 0 0 14231.792170117"
## [1] "3 0 1 14234.2992059569"
## [1] "3 0 4 14214.8516548857"
## [1] "4 0 0 14233.6339020465"
## [1] "4 0 1 14235.6861507626"
## [1] "4 0 2 14192.8670273794"
## [1] "4 0 4 14226.6422780728"
## [1] "5 0 0 14226.156849215"
## [1] "5 0 1 14237.5544204504"
## [1] "5 0 2 14189.377337544"
## [1] "5 0 4 14179.5067354776"
## p d q AIC
## 24 4 0 4 14226.64
## 25 5 0 0 14226.16
## 20 3 0 4 14214.85
## 23 4 0 2 14192.87
## 27 5 0 2 14189.38
## 28 5 0 4 14179.51
ARIMA_loop(trainweekly, 5, 0, 5)
## [1] "0 0 0 5741.39026284353"
## [1] "0 0 1 5150.04699150449"
## [1] "0 0 2 4741.61081594301"
## [1] "0 0 3 4450.42774374519"
## [1] "0 0 4 4260.39110618474"
## [1] "0 0 5 4149.4685460978"
## [1] "1 0 0 3661.44876154144"
## [1] "1 0 1 3659.3723323198"
## [1] "1 0 2 3661.82348748319"
## [1] "1 0 3 3662.86368118921"
## [1] "1 0 4 3659.66204246414"
## [1] "1 0 5 3660.24043552471"
## [1] "2 0 0 3659.02179613807"
## [1] "2 0 1 3660.01040942501"
## [1] "2 0 2 3663.76390203723"
## [1] "2 0 3 3668.00877603391"
## [1] "2 0 4 3667.26675391408"
## [1] "3 0 0 3820.91290308436"
## [1] "3 0 1 3660.47493580613"
## [1] "3 0 3 3658.68769300573"
## [1] "3 0 4 3652.60862112194"
## [1] "3 0 5 3664.28346373553"
## [1] "4 0 0 3662.28929779116"
## [1] "4 0 1 3659.67089298282"
## [1] "4 0 2 3653.01777790836"
## [1] "4 0 3 3658.55899651738"
## [1] "4 0 4 3666.09243083452"
## [1] "5 0 0 3662.58405414437"
## [1] "5 0 1 3660.78475052804"
## [1] "5 0 2 3651.81522250471"
## [1] "5 0 3 3662.69791927401"
## [1] "5 0 4 3659.57926650594"
## p d q AIC
## 14 2 0 0 3659.022
## 21 3 0 3 3658.688
## 27 4 0 3 3658.559
## 26 4 0 2 3653.018
## 22 3 0 4 3652.609
## 31 5 0 2 3651.815
#daily 5,5
#weekly 5,2
library(rugarch)
#Initial GARCH Order
#ARIMA-GARCH: Select GARCH order
test_modelAGGdaily <- function(m,n){
spec = ugarchspec(variance.model=list(garchOrder=c(m,n)),
mean.model=list(armaOrder=c(5,5),
include.mean=T),
distribution.model="std")
fit = ugarchfit(spec, traindaily, solver = 'hybrid')
current.bic = infocriteria(fit)[2]
df = data.frame(m,n,current.bic)
names(df) <- c("m","n","BIC")
print(paste(m,n,current.bic,sep=" "))
return(df)
}
ordersAGGdaily = data.frame(Inf,Inf,Inf)
names(ordersAGGdaily) <- c("m","n","BIC")
for (m in 0:2){
for (n in 0:2){
possibleError <- tryCatch(
ordersAGGdaily<-rbind(ordersAGGdaily,test_modelAGGdaily(m,n)),
error=function(e) e
)
if(inherits(possibleError, "error")) next
}
}
## [1] "0 1 5.32492441010628"
## [1] "0 2 5.16319324597862"
## [1] "1 0 4.51669415017213"
## [1] "1 1 3.80207485186964"
## [1] "1 2 3.80343957541006"
## [1] "2 0 4.11946806166941"
## [1] "2 1 3.80202999725364"
## [1] "2 2 3.80505811670573"
ordersAGGdaily <- ordersAGGdaily[order(-ordersAGGdaily$BIC),]
tail(ordersAGGdaily)
## m n BIC
## 4 1 0 4.516694
## 7 2 0 4.119468
## 9 2 2 3.805058
## 6 1 2 3.803440
## 5 1 1 3.802075
## 8 2 1 3.802030
# 2,2
#daily 1,1
test_modelAGGweekly <- function(m,n){
spec = ugarchspec(variance.model=list(garchOrder=c(m,n)),
mean.model=list(armaOrder=c(5,2),
include.mean=T),
distribution.model="std")
fit = ugarchfit(spec, trainweekly, solver = 'hybrid')
current.bic = infocriteria(fit)[2]
df = data.frame(m,n,current.bic)
names(df) <- c("m","n","BIC")
print(paste(m,n,current.bic,sep=" "))
return(df)
}
ordersAGGweekly = data.frame(Inf,Inf,Inf)
names(ordersAGGweekly) <- c("m","n","BIC")
for (m in 0:2){
for (n in 0:2){
possibleError <- tryCatch(
ordersAGGweekly<-rbind(ordersAGGweekly,test_modelAGGweekly(m,n)),
error=function(e) e
)
if(inherits(possibleError, "error")) next
}
}
## [1] "0 0 7.06536666459738"
## [1] "0 1 6.77903928440492"
## [1] "0 2 6.78414733157833"
## [1] "1 0 6.26396043958399"
## [1] "1 1 5.72331522097766"
## [1] "1 2 5.74824797042694"
## [1] "2 0 5.91408620427476"
## [1] "2 1 5.74470626689426"
## [1] "2 2 5.73376143896432"
ordersAGGweekly <- ordersAGGweekly[order(-ordersAGGweekly$BIC),]
tail(ordersAGGweekly)
## m n BIC
## 5 1 0 6.263960
## 8 2 0 5.914086
## 7 1 2 5.748248
## 9 2 1 5.744706
## 10 2 2 5.733761
## 6 1 1 5.723315
spec.1 = ugarchspec(variance.model=list(garchOrder=c(1,1)),
mean.model=list(armaOrder=c(5, 5),
include.mean=T), distribution.model="std")
final.model.1 = ugarchfit(spec.1, traindaily, solver = 'hybrid')
spec.2 = ugarchspec(variance.model=list(garchOrder=c(1,1)),
mean.model=list(armaOrder=c(5, 2),
include.mean=T), distribution.model="std")
final.model.2 = ugarchfit(spec.2, trainweekly, solver = 'hybrid')
infocriteria(final.model.1)
##
## Akaike 3.763936
## Bayes 3.802075
## Shibata 3.763848
## Hannan-Quinn 3.777857
infocriteria(final.model.2)
##
## Akaike 5.616944
## Bayes 5.723315
## Shibata 5.615673
## Hannan-Quinn 5.658801
final.model.1
##
## *---------------------------------*
## * GARCH Model Fit *
## *---------------------------------*
##
## Conditional Variance Dynamics
## -----------------------------------
## GARCH Model : sGARCH(1,1)
## Mean Model : ARFIMA(5,0,5)
## Distribution : std
##
## Optimal Parameters
## ------------------------------------
## Estimate Std. Error t value Pr(>|t|)
## mu 14.157637 0.005606 2525.2696 0.000000
## ar1 -1.379994 0.007331 -188.2363 0.000000
## ar2 -0.641426 0.007204 -89.0403 0.000000
## ar3 0.762021 0.005412 140.8092 0.000000
## ar4 1.352082 0.009614 140.6430 0.000000
## ar5 0.922929 0.006926 133.2637 0.000000
## ma1 2.376347 0.000278 8534.2539 0.000000
## ma2 3.024506 0.006318 478.7247 0.000000
## ma3 2.247134 0.000180 12475.2445 0.000000
## ma4 0.889357 0.000101 8804.1700 0.000000
## ma5 -0.019340 0.000553 -34.9517 0.000000
## omega 0.005825 0.001613 3.6111 0.000305
## alpha1 0.122081 0.010616 11.4995 0.000000
## beta1 0.876919 0.009841 89.1089 0.000000
## shape 4.293257 0.322655 13.3060 0.000000
##
## Robust Standard Errors:
## Estimate Std. Error t value Pr(>|t|)
## mu 14.157637 0.014582 970.9293 0.000000
## ar1 -1.379994 0.008223 -167.8282 0.000000
## ar2 -0.641426 0.016561 -38.7318 0.000000
## ar3 0.762021 0.018523 41.1397 0.000000
## ar4 1.352082 0.011054 122.3198 0.000000
## ar5 0.922929 0.010358 89.1071 0.000000
## ma1 2.376347 0.000682 3485.5583 0.000000
## ma2 3.024506 0.014535 208.0785 0.000000
## ma3 2.247134 0.000278 8085.3479 0.000000
## ma4 0.889357 0.000276 3227.2186 0.000000
## ma5 -0.019340 0.001261 -15.3390 0.000000
## omega 0.005825 0.002581 2.2569 0.024013
## alpha1 0.122081 0.022158 5.5095 0.000000
## beta1 0.876919 0.024956 35.1384 0.000000
## shape 4.293257 0.334886 12.8201 0.000000
##
## LogLikelihood : -4217.546
##
## Information Criteria
## ------------------------------------
##
## Akaike 3.7639
## Bayes 3.8021
## Shibata 3.7638
## Hannan-Quinn 3.7779
##
## Weighted Ljung-Box Test on Standardized Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 3.155 0.0756975
## Lag[2*(p+q)+(p+q)-1][29] 28.270 0.0000000
## Lag[4*(p+q)+(p+q)-1][49] 40.202 0.0002041
## d.o.f=10
## H0 : No serial correlation
##
## Weighted Ljung-Box Test on Standardized Squared Residuals
## ------------------------------------
## statistic p-value
## Lag[1] 0.07808 0.7799
## Lag[2*(p+q)+(p+q)-1][5] 0.62111 0.9375
## Lag[4*(p+q)+(p+q)-1][9] 1.73110 0.9346
## d.o.f=2
##
## Weighted ARCH LM Tests
## ------------------------------------
## Statistic Shape Scale P-Value
## ARCH Lag[3] 0.1427 0.500 2.000 0.7057
## ARCH Lag[5] 0.7723 1.440 1.667 0.8015
## ARCH Lag[7] 1.6700 2.315 1.543 0.7867
##
## Nyblom stability test
## ------------------------------------
## Joint Statistic: 12.1511
## Individual Statistics:
## mu 0.005415
## ar1 0.064116
## ar2 0.070579
## ar3 0.035357
## ar4 0.049483
## ar5 0.118945
## ma1 0.124116
## ma2 0.064252
## ma3 0.031756
## ma4 0.046453
## ma5 0.058887
## omega 0.256344
## alpha1 1.174185
## beta1 1.840920
## shape 1.711273
##
## Asymptotic Critical Values (10% 5% 1%)
## Joint Statistic: 3.26 3.54 4.07
## Individual Statistic: 0.35 0.47 0.75
##
## Sign Bias Test
## ------------------------------------
## t-value prob sig
## Sign Bias 1.1193 0.2631
## Negative Sign Bias 0.5064 0.6126
## Positive Sign Bias 0.3858 0.6997
## Joint Effect 2.2042 0.5311
##
##
## Adjusted Pearson Goodness-of-Fit Test:
## ------------------------------------
## group statistic p-value(g-1)
## 1 20 20.68 0.35501
## 2 30 32.59 0.29469
## 3 40 53.77 0.05805
## 4 50 67.12 0.04372
##
##
## Elapsed time : 24.47159
Response: Analysis of the ARMA GARCH Fit for the Weekly and Daily Data The best fit for the daily data was ARMA (5,5) and GARCH (1,1). The best fit for the weekly data was ARMA(5,2) and GARCH (1,1). Between both, the one with the lowest AIC, BIC, Shibata and Hannan-Quinn values is the daily data. Therefore ARMA (5,5) and GARCH (1,1) is the better fit.
Model Mean Equation - Yt = .6495(Yt-1)-.3751(Yt-2)+.4139(Yt-3)-.5353(Yt-4)+.8517(Yt-5)+.3489(Zt-1)+.7526(Zt-2)+.3007(Zt-3)+.8651(Zt-4)-.0344(Zt-5)+14.3958 Model Variance Equation - Sigma^2(t) = .0058 + .1236(Z2t-1)+.8754(Sigma2(t-1))
3b. Evaluate the model residuals and squared residuals using the ACF and PACF plots as well as hypothesis testing for serial correlation. What would you conclude based on this analysis?
resids.final.model = residuals(final.model.1)
acf(resids.final.model,main="ACF of ARMA - GARCH Residuals")
acf(resids.final.model^2,main="ACF of Squared ARMA-GARCH Residuals")
pacf(resids.final.model,main="pACF of ARMA-GARCH Residuals")
pacf(resids.final.model^2,main="pACF of Squared ARMA-GARCH Residuals")
Box.test(resids.final.model,lag=10,type='Ljung')
##
## Box-Ljung test
##
## data: resids.final.model
## X-squared = 48.284, df = 10, p-value = 5.507e-07
Box.test(resids.final.model^2,lag=10,type='Ljung')
##
## Box-Ljung test
##
## data: resids.final.model^2
## X-squared = 720.77, df = 10, p-value < 2.2e-16
qqnorm(resids.final.model)
Response
The ACF of the ARMA-GARCH Residual for the daily data looks like white noise with some lags over the bounds. The ACF of the Squared Residuals is outside the bounds. The pACF is outside the bounds on some of the lags for hte Residuals. The p-values are <.05, suggesting that there is significant evidence to reject the null hypothesis. In other words, there is evidence of autocorrelation present in the residuals.
3c. Apply the models identified in (3a) and forecast the mean and the variance of the last two weeks of data. Plot the predicted data to compare the predicted values to the actual observed ones. Include 95% confidence intervals for the forecasts for the mean only in the corresponding plots. Interpret the results, particularly comparing forecast using daily versus weekly data.
data.ts.daily = ts(dailydata$AdjClose,start=c(2015),freq=252)
ndaily=length(data.ts.daily)
nfitdaily = ndaily-14
nfore = 14
fore.series.1 = NULL
fore.sigma.1 = NULL
for(f in 1: nfore){
## Fit models
data = traindaily
if(f>2)
data = c(traindaily,testdaily[1:(f-1)])
final.model.1 = ugarchfit(spec.1, dailydata$AdjClose, solver = 'hybrid')
fore = ugarchforecast(final.model.1, n.ahead=14)
fore.series.1 = c(fore.series.1, fore@forecast$seriesFor)
fore.sigma.1 = c(fore.sigma.1, fore@forecast$sigmaFor)
}
Response: Interpretation of the results
3d. Calculate Mean Absolute Percentage Error (MAPE) and Precision Measure (PM) for the mean forecasts. Compare the accuracy of the predictions for the daily and weekly time series using these two measures. Compare the accuracy of the forecasts with those obtained in (2d). Interpret the results.
###MAPE
100*mean(abs(fore.series.1 - testdaily)/abs(testdaily))
## [1] 2.85801
### Precision Measure (PM)
sum((fore.series.1 - testdaily)^2)/sum((testdaily-mean(testdaily))^2)
## [1] 21.21581
Response: Model comparison
Based on the analysis above, discuss the application of ARIMA on the stock price versus the application of ARMA-GARCH on the stock return. How do the models fit the data? How well do the models predict? How do the models perform when using daily versus weekly data? Would you use one approach over another for different settings? What are some specific points of caution one would need to consider when applying those models?
Response: Final considerations Daily data seemed to do better in the ARMA GARCH model. Weekly data seemed to do better with the ARIMA model. They both do not predict that well.