knitr::opts_chunk$set(echo = TRUE)
library(ggplot2)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(readxl)
library(tsibble)
##
## Attaching package: 'tsibble'
## The following objects are masked from 'package:base':
##
## intersect, setdiff, union
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✓ tibble 3.1.6 ✓ dplyr 1.0.8
## ✓ tidyr 1.2.0 ✓ stringr 1.4.0
## ✓ readr 2.0.2 ✓ forcats 0.5.1
## ✓ purrr 0.3.4
## Warning: package 'tidyr' was built under R version 4.1.2
## Warning: package 'dplyr' was built under R version 4.1.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(neuralnet)
##
## Attaching package: 'neuralnet'
## The following object is masked from 'package:dplyr':
##
## compute
crudeoil <- read_excel("~/Documents/BC 22Spr/Predict:Forcast/crudeoil.xlsx")
head(crudeoil)
## # A tibble: 6 × 2
## time crudeOilImport
## <chr> <dbl>
## 1 2009 01 317275
## 2 2009 02 262339
## 3 2009 03 303897
## 4 2009 04 285934
## 5 2009 05 281147
## 6 2009 06 284093
ts<-ts(crudeoil$crudeOilImport,frequency=12)
autoplot(ts)

crudeoil_train<-crudeoil[1:135,]
crudeoil_test<-crudeoil[136:145,]
fit<-nnetar(crudeoil_train$crudeOilImport,lambda=0.2)
fit
## Series: crudeoil_train$crudeOilImport
## Model: NNAR(14,8)
## Call: nnetar(y = crudeoil_train$crudeOilImport, lambda = 0.2)
##
## Average of 20 networks, each of which is
## a 14-8-1 network with 129 weights
## options were - linear output units
##
## sigma^2 estimated as 0.00327
fc <- forecast(fit, PI=TRUE, h=10)
fc
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 136 201033.0 199724.3 202292.3 198981.6 202870.0
## 137 191959.8 190591.3 193309.5 190021.8 193969.1
## 138 185242.3 183583.5 186942.8 182674.9 187746.9
## 139 194325.9 192793.1 195790.7 191755.8 196478.5
## 140 204124.6 202294.4 205817.0 201211.8 206972.8
## 141 210032.7 208173.7 211888.3 207395.9 212906.2
## 142 196414.6 194558.1 198345.3 193589.2 199297.7
## 143 206486.2 204501.4 208688.3 203618.4 209747.2
## 144 217873.7 215593.9 220106.1 214543.8 221022.2
## 145 219906.5 217317.9 222533.0 216144.6 224039.7
autoplot(fc)

accuracy(fc)
## ME RMSE MAE MPE MAPE MASE
## Training set 0.1661108 371.8141 235.2399 -0.002585021 0.09730316 0.01678908
## ACF1
## Training set 0.02717953
fit2<-nnetar(crudeoil_train$crudeOilImport,lambda=0.2,size=10)
fit2
## Series: crudeoil_train$crudeOilImport
## Model: NNAR(14,10)
## Call: nnetar(y = crudeoil_train$crudeOilImport, size = 10, lambda = 0.2)
##
## Average of 20 networks, each of which is
## a 14-10-1 network with 161 weights
## options were - linear output units
##
## sigma^2 estimated as 0.0002969
fc2 <- forecast(fit2, PI=TRUE, h=10)
fc2
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 136 190647.7 190268.8 191013.3 190100.3 191221.8
## 137 181886.2 181517.2 182246.9 181308.1 182407.9
## 138 168085.0 167694.2 168438.2 167532.3 168659.0
## 139 178487.5 178027.1 178946.8 177772.0 179180.1
## 140 173920.9 173501.8 174355.3 173282.3 174604.5
## 141 187861.3 187429.1 188311.2 187172.3 188569.6
## 142 171863.1 171456.2 172310.1 171205.7 172537.6
## 143 188880.0 188487.9 189245.6 188280.9 189437.2
## 144 183340.8 182884.8 183826.0 182670.4 184042.7
## 145 177345.7 176851.2 177858.0 176579.6 178146.2
autoplot(fc2)

accuracy(fc2)
## ME RMSE MAE MPE MAPE MASE
## Training set 0.06936573 88.81164 60.21759 -0.0001569682 0.0247292 0.004297733
## ACF1
## Training set -0.05584101
fit3<-nnetar(crudeoil_train$crudeOilImport,lambda=0.2,size=15)
fit3
## Series: crudeoil_train$crudeOilImport
## Model: NNAR(14,15)
## Call: nnetar(y = crudeoil_train$crudeOilImport, size = 15, lambda = 0.2)
##
## Average of 20 networks, each of which is
## a 14-15-1 network with 241 weights
## options were - linear output units
##
## sigma^2 estimated as 5.529e-06
fc3 <- forecast(fit3, PI=TRUE, h=10)
fc3
## Point Forecast Lo 80 Hi 80 Lo 95 Hi 95
## 136 192880.1 192829.5 192931.2 192803.0 192965.1
## 137 192172.2 192117.5 192223.5 192095.2 192256.7
## 138 172188.6 172132.0 172243.5 172101.7 172276.7
## 139 186877.1 186817.3 186936.4 186787.3 186968.8
## 140 184265.3 184193.3 184333.3 184154.5 184371.9
## 141 193089.8 193008.8 193162.0 192969.7 193215.7
## 142 180961.0 180882.4 181036.2 180845.4 181068.4
## 143 194370.7 194295.2 194446.5 194258.5 194482.2
## 144 200415.5 200311.8 200506.8 200263.6 200564.9
## 145 187012.5 186915.5 187104.1 186868.0 187157.1
autoplot(fc3)

accuracy(fc3)
## ME RMSE MAE MPE MAPE
## Training set -0.001146505 11.23312 8.305513 6.889164e-06 0.003476812
## MASE ACF1
## Training set 0.0005927649 0.01225526