for this assignment, I picked the dataset, “PZZA” from https://finance.yahoo.com/quote/PZZA/history?period1=1439337600&period2=1597190400&interval=1mo&filter=history&frequency=1mo
#Reading our dataset
library(ggplot2)
library(fpp2)
## Loading required package: forecast
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
## Loading required package: fma
## Loading required package: expsmooth
library(forecast)
papa = read.csv("C:\\Users\\student\\Desktop\\Predictive Analytics - Forecasting\\Discussion 7 - Neural Nets\\PZZA.csv")
#Summary of our dataset
str(papa)
## 'data.frame': 61 obs. of 7 variables:
## $ Date : chr "2015-08-01" "2015-09-01" "2015-10-01" "2015-11-01" ...
## $ Open : num 69 65.8 68.5 70 57.6 ...
## $ High : num 69.5 71.7 74.5 72.7 59.2 ...
## $ Low : num 66.3 65.8 65.9 53.7 53.7 ...
## $ Close : num 68.4 68.5 70.2 57.5 55.9 ...
## $ Adj.Close: num 63.6 63.8 65.4 53.6 52.2 ...
## $ Volume : int 553600 5449500 7706000 13967000 7764500 10368700 16121200 9779700 7172500 9553500 ...
head(papa)
## Date Open High Low Close Adj.Close Volume
## 1 2015-08-01 68.96 69.47 66.32 68.40 63.60655 553600
## 2 2015-09-01 65.84 71.71 65.84 68.48 63.83500 5449500
## 3 2015-10-01 68.49 74.52 65.87 70.17 65.41037 7706000
## 4 2015-11-01 69.99 72.72 53.65 57.48 53.58112 13967000
## 5 2015-12-01 57.59 59.19 53.71 55.87 52.23329 7764500
## 6 2016-01-01 54.97 56.20 45.92 47.75 44.64185 10368700
tail(papa)
## Date Open High Low Close Adj.Close Volume
## 56 2020-03-01 57.56 59.86 28.55 53.37 53.09935 29222100
## 57 2020-04-01 52.91 74.13 51.82 71.92 71.55527 18089300
## 58 2020-05-01 70.37 84.30 69.21 77.89 77.49500 18632600
## 59 2020-06-01 78.42 85.00 75.75 79.41 79.22789 17893500
## 60 2020-07-01 80.40 96.43 80.22 94.67 94.45289 12234300
## 61 2020-08-01 94.94 100.69 89.27 93.65 93.43523 5621300
#Creating our time series
papa.ts = ts(papa$Adj.Close, frequency = 12, start = c(2015,8))
autoplot(papa.ts) +xlab("Year") + ylab("Adjusting Closing Price in USD")
#Splitting our time series into training and testing
train.ts = window(papa.ts, end=c(2020,7))
test.ts = window(papa.ts, start=c(2015,8))
#Building Neural Network Model
#Fitting neural network model
papa.nnet = nnetar(train.ts)
papa.nnet
## Series: train.ts
## Model: NNAR(1,1,2)[12]
## Call: nnetar(y = train.ts)
##
## Average of 20 networks, each of which is
## a 2-2-1 network with 9 weights
## options were - linear output units
##
## sigma^2 estimated as 28.12
fcast.papa = forecast(papa.nnet,h=12)
plot(fcast.papa)
lines(test.ts, col="red")
legend("bottomright",lty=1,col=c("red","blue"),c("actual values","forecast"))
#The NNAR(1,1,2) {12} is the best neural netwoek model with our current dataset.
#Same graph with a different prospective
autoplot(fcast.papa) +xlab("Year") + ylab("Monthly Adjusting Closing Price in USD")
checkresiduals(fcast.papa)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.
acc.nnet = accuracy(fcast.papa, test.ts)
acc.nnet
## ME RMSE MAE MPE MAPE MASE
## Training set 0.001535062 5.303068 4.027578 -0.8526443 6.977252 0.2450020
## Test set 9.379390505 9.379391 9.379391 10.0383880 10.038388 0.5705585
## ACF1
## Training set 0.008006064
## Test set NA
#Overall, the model does a solid job at predicting the future stock prices because as the accuracy displays, the MAPE is ony 10.56%. As a wise man once said, "what goes up must come down" and that is exactly the philosophy that our model is following.
#It would be quite interesting to know the core reason behind the expected declined.