rm(list=ls())

library(forecast)
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
library(ggplot2)

#Load data
dy.stock <- read.csv('DY.csv')
#Select adjusted price as time series
dy.price <- ts(dy.stock$Adj.Close)

#Plot adjusted close price
autoplot(dy.price, xlab = "Days", ylab = "Daily Adjusted Closing Price") +
  ggtitle("DY Stock Data from 3/31/2019 - 3/31/2021") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(caption = "Source: Dycom Industries, Inc. (DY) Stock Data, Yahoo Finance") +
  theme(plot.caption = element_text(hjust = 0))

#Arima
arima.model <- auto.arima(dy.price)
arima.model
## Series: dy.price 
## ARIMA(0,1,0) 
## 
## sigma^2 estimated as 4.691:  log likelihood=-1102.47
## AIC=2206.95   AICc=2206.95   BIC=2211.17
arima.predictions <- forecast(arima.model, h=30, level=95)
arima.predictions
##     Point Forecast    Lo 95     Hi 95
## 505           89.3 85.05483  93.54518
## 506           89.3 83.29642  95.30359
## 507           89.3 81.94714  96.65286
## 508           89.3 80.80965  97.79035
## 509           89.3 79.80750  98.79250
## 510           89.3 78.90149  99.69852
## 511           89.3 78.06832 100.53168
## 512           89.3 77.29283 101.30717
## 513           89.3 76.56448 102.03553
## 514           89.3 75.87558 102.72443
## 515           89.3 75.22035 103.37966
## 516           89.3 74.59428 104.00572
## 517           89.3 73.99381 104.60620
## 518           89.3 73.41601 105.18399
## 519           89.3 72.85851 105.74150
## 520           89.3 72.31930 106.28070
## 521           89.3 71.79670 106.80331
## 522           89.3 71.28925 107.31076
## 523           89.3 70.79571 107.80429
## 524           89.3 70.31500 108.28500
## 525           89.3 69.84617 108.75384
## 526           89.3 69.38837 109.21164
## 527           89.3 68.94086 109.65915
## 528           89.3 68.50298 110.09703
## 529           89.3 68.07413 110.52588
## 530           89.3 67.65377 110.94624
## 531           89.3 67.24142 111.35858
## 532           89.3 66.83665 111.76336
## 533           89.3 66.43903 112.16097
## 534           89.3 66.04822 112.55179
autoplot(arima.predictions,
         main ="DY Stock Data with Next 30 Days Forecast Using ARIMA",
         ylab="Daily Adjusted Closing Price", xlab ="Days") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(caption = "Source: Dycom Industries, Inc. (DY) Stock Data, Yahoo Finance") +
  theme(plot.caption = element_text(hjust = 0))

#Drift
drift.predictions <- rwf(dy.price, 30, level=95, drift=TRUE)
drift.predictions
##     Point Forecast    Lo 95     Hi 95
## 505       89.38322 85.13696  93.62949
## 506       89.46644 83.45536  95.47753
## 507       89.54967 82.18032  96.91901
## 508       89.63289 81.11507  98.15070
## 509       89.71611 80.18350  99.24871
## 510       89.79933 79.34658 100.25207
## 511       89.88255 78.58119 101.18391
## 512       89.96577 77.87225 102.05929
## 513       90.04899 77.20930 102.88868
## 514       90.13221 76.58475 103.67967
## 515       90.21543 75.99287 104.43799
## 516       90.29865 75.42920 105.16810
## 517       90.38187 74.89021 105.87354
## 518       90.46509 74.37303 106.55715
## 519       90.54831 73.87532 107.22131
## 520       90.63153 73.39509 107.86798
## 521       90.71475 72.93069 108.49881
## 522       90.79798 72.48071 109.11524
## 523       90.88120 72.04391 109.71849
## 524       90.96442 71.61923 110.30961
## 525       91.04764 71.20574 110.88954
## 526       91.13086 70.80262 111.45910
## 527       91.21408 70.40915 112.01901
## 528       91.29730 70.02467 112.56993
## 529       91.38052 69.64860 113.11244
## 530       91.46374 69.28043 113.64705
## 531       91.54696 68.91967 114.17425
## 532       91.63018 68.56591 114.69445
## 533       91.71340 68.21875 115.20806
## 534       91.79662 67.87783 115.71541
autoplot(drift.predictions, ylab = "Daily Adjusted Closing Price", xlab = "Days") +
  ggtitle("DY Stock Data with Next 30 Days Forecast Using Random Walk w/ Drift") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(caption = "Source: Dycom Industries, Inc. (DY) Stock Data, Yahoo Finance") +
  theme(plot.caption = element_text(hjust = 0))

#Mean, naive, and drift plot
autoplot(dy.price, xlab = "Days", ylab = "Adjusted Closing Price") +
  autolayer(meanf(dy.price, h=30), series = "Mean", PI = FALSE) +
  autolayer(naive(dy.price, h=30), series = "Naive", PI = FALSE) +
  autolayer(rwf(dy.price, h=30, drift=TRUE), series = "Drift", PI = FALSE) +
  ggtitle("DY Stock Data from 3/31/2019 - 3/31/2021 with Next 30 Days Forecast") +
  theme(plot.title = element_text(hjust = 0.5)) +
  labs(caption = "Source: Dycom Industries, Inc. (DY) Stock Data, Yahoo Finance") +
  theme(plot.caption = element_text(hjust = 0))