library(quantmod)
## Loading required package: xts
## Loading required package: zoo
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## Loading required package: TTR
## Registered S3 method overwritten by 'quantmod':
##   method            from
##   as.zoo.data.frame zoo
## Version 0.4-0 included new data defaults. See ?getSymbols.
library(fpp2)
## ── Attaching packages ───────────────────────────────────────────────────────────────────────────── fpp2 2.4 ──
## ✓ ggplot2   3.3.2     ✓ fma       2.4  
## ✓ forecast  8.13      ✓ expsmooth 2.3
## 
library(knitr)
Apple =  AAPL <- read.csv("~/Desktop/AAPL.csv", stringsAsFactors=TRUE)
Apple.ts = ts(Apple[,6], frequency = 12, start = c(1980, 12, 12), end = c(2020,12,04))
autoplot(Apple.ts) + ggtitle("Adjust close stock price for Apple from 1980 ~2020")

ggseasonplot(Apple.ts)

apple.train = window(Apple.ts,end=c(2020))

apple.nn1=nnetar(apple.train,lambda="auto",size=1)
apple.nn2=nnetar(apple.train,lambda="auto",size=2)
apple.nn3=nnetar(apple.train,lambda="auto",size=4)
apple.nn4=nnetar(apple.train,lambda="auto",size=5)
fc1 = forecast(apple.nn1)
checkresiduals((fc1))
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

kable(accuracy(fc1))
ME RMSE MAE MPE MAPE MASE ACF1
Training set 16.54176 332.7677 24.91484 -1.897802 12.83733 0.5192953 -0.1085749
fc2 = forecast(apple.nn2)
checkresiduals((fc2))
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

kable(accuracy(fc2))
ME RMSE MAE MPE MAPE MASE ACF1
Training set 16.74765 330.5825 21.54917 -0.5466519 8.673353 0.4491453 -0.0381746
fc3 = forecast(apple.nn3)
checkresiduals((fc3))
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

kable(accuracy(fc3))
ME RMSE MAE MPE MAPE MASE ACF1
Training set 15.8438 330.0214 19.9832 -0.2692968 6.588038 0.4165061 -0.0112324
fc4 = forecast(apple.nn4)
checkresiduals((fc4))
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.

kable(accuracy(fc4))
ME RMSE MAE MPE MAPE MASE ACF1
Training set 15.77473 329.6767 19.84791 -0.325272 6.207676 0.4136863 -0.0102904
autoplot(fc1)

autoplot(fc2)

autoplot(fc3)

autoplot(fc4)