library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(xts)
## Loading required package: zoo
##
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
##
## as.Date, as.Date.numeric
library(TTR)
library(tseries)
library(quantmod)
## Version 0.4-0 included new data defaults. See ?getSymbols.
AMD=read.csv("C:/Users/yiq00/Downloads/AMD.csv",header=TRUE)
str(AMD)
## 'data.frame': 60 obs. of 7 variables:
## $ Date : chr "2015-11-01" "2015-12-01" "2016-01-01" "2016-02-01" ...
## $ Open : num 2.13 2.36 2.77 2.17 2.16 2.79 3.58 4.6 5.09 6.89 ...
## $ High : num 2.4 3.06 2.82 2.19 2.98 3.99 4.71 5.52 7.16 8 ...
## $ Low : num 1.94 2.2 1.75 1.81 2.12 2.6 3.45 4.07 4.82 6.15 ...
## $ Close : num 2.36 2.87 2.2 2.14 2.85 3.55 4.57 5.14 6.86 7.4 ...
## $ Adj.Close: num 2.36 2.87 2.2 2.14 2.85 3.55 4.57 5.14 6.86 7.4 ...
## $ Volume : num 1.36e+08 2.66e+08 3.55e+08 1.93e+08 3.33e+08 ...
head(AMD)
## Date Open High Low Close Adj.Close Volume
## 1 2015-11-01 2.13 2.40 1.94 2.36 2.36 136451200
## 2 2015-12-01 2.36 3.06 2.20 2.87 2.87 266450600
## 3 2016-01-01 2.77 2.82 1.75 2.20 2.20 355489300
## 4 2016-02-01 2.17 2.19 1.81 2.14 2.14 192749800
## 5 2016-03-01 2.16 2.98 2.12 2.85 2.85 333321700
## 6 2016-04-01 2.79 3.99 2.60 3.55 3.55 472576300
summary(AMD)
## Date Open High Low
## Length:60 Min. : 2.13 Min. : 2.19 Min. : 1.750
## Class :character 1st Qu.:10.31 1st Qu.:11.61 1st Qu.: 9.325
## Mode :character Median :14.29 Median :15.60 Median :12.300
## Mean :22.65 Mean :26.32 Mean :20.414
## 3rd Qu.:30.55 3rd Qu.:34.31 3rd Qu.:27.485
## Max. :91.92 Max. :94.28 Max. :76.100
## Close Adj.Close Volume
## Min. : 2.14 Min. : 2.14 Min. :1.365e+08
## 1st Qu.:10.35 1st Qu.:10.35 1st Qu.:9.883e+08
## Median :14.51 Median :14.51 Median :1.206e+09
## Mean :23.68 Mean :23.68 Mean :1.273e+09
## 3rd Qu.:30.56 3rd Qu.:30.56 3rd Qu.:1.592e+09
## Max. :90.82 Max. :90.82 Max. :3.063e+09
AMDts=ts(AMD[,2], start=c(2015,11,01), end=c(2020,10,01), frequency = 12)
plot(AMDts, main="Monthly AMD Volume (2018-2020)",ylab="Volume")
train.ts<-window(AMDts, end=c(2020,10))
test.ts<-window(AMDts,start=c(2015,11))
Build Neural Network Model
fit.nnet<-nnetar(train.ts)
fit.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 20.21
Neural Net Forecast
fcast.nnet<-forecast(fit.nnet,h=29)
plot(fcast.nnet)
lines(test.ts, col="red")
legend("topright",lty=1,col=c("red","blue"),c("actual values","forecast"))
Accuracy
accuracy(fit.nnet)
## ME RMSE MAE MPE MAPE MASE
## Training set -0.003884056 4.495846 3.2312 -2.745053 13.07869 0.2403124
## ACF1
## Training set -0.1985158
Residuals
res=residuals(fit.nnet);tsdisplay(res)