An artificial neural network is composed of many artificial neurons that are linked together according to a specific network architecture. The objective of the nueral network is to transform the inputs into meaningful outputs. The tasks of the neural network are as follows:
* control
* classification
* prediction
* approximation (given a set of values of a function g(x) build a neural network that approximates the g(x) values for any input x)
These can be reformulated in general as function approximation tasks.
Another way to explain what a neural network does is to say that it has the ability to learn by example. It follows the non-linear path and process information in parallel thhroughout the nodes. It is adaptive in the sense that it has the ability to change its internal structure by adjusting weights of inputs.
There are two main tyoes of ANN (a standing for artificial, from my reading it seems irrelevant if its in the name or not): feedforward and feedback networks.
Because I will be looking at a univariate time series, I will be using the function nnetar, which creates a feed-forward nerual network with a single hidden layer and lagged intoputs for forecasting the univariate time series.
library(neuralnet)
library(forecast)
## Registered S3 method overwritten by 'quantmod':
## method from
## as.zoo.data.frame zoo
library(ggfortify)
## Loading required package: ggplot2
## Registered S3 methods overwritten by 'ggfortify':
## method from
## autoplot.Arima forecast
## autoplot.acf forecast
## autoplot.ar forecast
## autoplot.bats forecast
## autoplot.decomposed.ts forecast
## autoplot.ets forecast
## autoplot.forecast forecast
## autoplot.stl forecast
## autoplot.ts forecast
## fitted.ar forecast
## fortify.ts forecast
## residuals.ar forecast
#BYND daily
bynd <- read.csv("C:/Users/andre/Documents/Boston College/2. Summer 2020/Forecasting/week 5/BYND.csv")
bynd.ts <- ts(bynd$Adj.Close, frequency = 7) #can't get dates to work
autoplot(bynd.ts, main = "Beyond Meat Daily Adj. Closing Prices")
There is an option in the nnetar() function to add a lambda for a Box-Cox transformation. I want to first check what that lambda should be with the BoxCox.lamba() function.
#BoxCox
lambda <- BoxCox.lambda(bynd.ts)
bynd.bc <- BoxCox(bynd.ts, lambda = lambda)
autoplot(bynd.bc)
#neural net
nn <- nnetar(bynd.bc) #with transformation
autoplot(nn)
## Warning: Removed 7 row(s) containing missing values (geom_path).
checkresiduals(nn)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.
nn2 <- nnetar(bynd.ts) #withOUT transformation
autoplot(nn2)
## Warning: Removed 7 row(s) containing missing values (geom_path).
checkresiduals(nn2)
## Warning in modeldf.default(object): Could not find appropriate degrees of
## freedom for this model.
#forecasts
fc_1 <- forecast(nn, h = 14)
fc_2 <- forecast(nn2, h = 14)
acc_1 <- accuracy(fc_1)
acc_2 <- accuracy(fc_2)
acc_1
## ME RMSE MAE MPE MAPE MASE
## Training set -0.0006449659 1.050234 0.7075956 -0.151324 2.617671 0.3396416
## ACF1
## Training set 0.0002977277
acc_2
## ME RMSE MAE MPE MAPE MASE
## Training set 0.004724739 7.332692 4.826246 -0.3651129 4.11051 0.3430981
## ACF1
## Training set -0.002107638
Based on these results, the metrics are in fact better with the transformed data. The RMSE is 1.05 compared to the non-transformed value of 7.29, but the MASE values are very close. Both, however, perform better than a naive forecast.