Discussion #5

Hello All,

Below are the steps I took to generate a model with one external regressor. I will be Harly Davidson stock for this example, and will pull the data from Yahoo’s rest api. This is acting as an addition to the first discussion post.

require(fpp)
require(forecast)
require(quantmod)
H <- new.env()
getSymbols("HOG", env = H, src = "yahoo",
          from = as.Date("2013-04-11"), to = as.Date("2018-04-11"))
## [1] "HOG"
HOG <- H$HOG
head(HOG)
##            HOG.Open HOG.High HOG.Low HOG.Close HOG.Volume HOG.Adjusted
## 2013-04-11    52.13    52.88   51.99     52.21    1764900     46.54485
## 2013-04-12    52.09    52.15   51.56     52.00     984600     46.35764
## 2013-04-15    51.55    52.04   49.92     49.95    2281300     44.53008
## 2013-04-16    50.43    51.69   50.43     51.63    1454300     46.02780
## 2013-04-17    51.28    51.47   50.59     51.15    1692300     45.59987
## 2013-04-18    51.08    51.52   50.62     51.05    1547500     45.51072
plot(HOG$HOG.Adjusted)

plot(HOG$HOG.Volume)

# Setup New Dataframe and Transform Variables
hog.clean <- data.frame(HOG$HOG.Adjusted, HOG$HOG.Volume)
colnames(hog.clean) <- c("Adjusted", "Volume")

hog.clean$Adjusted <- custom.log(hog.clean$Adjusted)
hog.clean$Volume <- custom.log(hog.clean$Volume)

# Plot Transformed Variables
hog.clean.ts <- ts(hog.clean, frequency=252, start=c(2013,4,11))
plot(hog.clean.ts, main="Harley Davidson {Log} Adjusted Closing & Volume")

# Setup Training and Test
train <- hog.clean.ts[2:1239]
test <- hog.clean.ts[1240:1259]

# Push Volume back by t-1
Volume1 <- data.frame(Volume=hog.clean$Volume[1:1238])
Volume2 <- data.frame(Volume=hog.clean$Volume[1239:1258])

# Fit Model
fit <- auto.arima(train, xreg=Volume1)
fcst <- forecast(fit, h=20, xreg=Volume2)

# Plot
plot(fcst)

accuracy(fcst, test)
##                         ME       RMSE        MAE          MPE      MAPE
## Training set -3.091513e-05 0.01727173 0.01173244 -0.001964148 0.2982566
## Test set     -4.538283e-02 0.04894576 0.04549393 -1.210676468 1.2135977
##                   MASE         ACF1
## Training set 0.9945281 -0.001561167
## Test set     3.8564022           NA