Discussion #8(2)
Go to Data Market (https://datamarket.com/data/list/?q=cat:ecc%20provider:tsdl (Links to an external site.)Links to an external site.). Pick a time series of interest to you. For the time series data you selected, use neural nets to build forecasts. Interpret.

I picked - Weekly closings of the Dow-Jones industrial average, July 1971 – August 1974 once again.

# LOADING DATASET
dj<-read.csv("weekly-closings-of-the-dowjones.csv")

names(dj)
## [1] "Week"        "close.price"
names(dj)[2]<-c("close.price")
head(dj)
##       Week close.price
## 1 1971-W27      890.19
## 2 1971-W28      901.80
## 3 1971-W29      888.51
## 4 1971-W30      887.78
## 5 1971-W31      858.43
## 6 1971-W32      850.61
dim(dj)
## [1] 162   2
tail(dj)
##         Week close.price
## 157 1974-W27      802.41
## 158 1974-W28      791.77
## 159 1974-W29      787.23
## 160 1974-W30      787.94
## 161 1974-W31      784.57
## 162 1974-W32      752.58

Plotting data

# Plot original series
plot(dj$close.price,type="l",xlab="Week",ylab="closing price",main="historical data")

# boxplots by week 
dj$weeknum<-substr(dj$Week,6,8)

library(ggplot2)

ggplot(dj, aes(as.factor(dj$weeknum), dj$close.price)) +
    geom_boxplot()

dj.ts.m<-ts(dj,frequency = 12)
## Warning in data.matrix(data): NAs introduced by coercion

Forecasting data

train.ts<-window(dj.ts.m[1:150,2])
test.ts<-window(dj.ts.m[151:162,2])

 library(forecast); library(fma);
## Warning: package 'forecast' was built under R version 3.4.3
## Warning: package 'fma' was built under R version 3.4.4
## 
## Attaching package: 'fma'
## The following object is masked _by_ '.GlobalEnv':
## 
##     dj
#ses.fit <- ses(train.ts) # library forecast
ar.fit<-auto.arima(train.ts)
nnet.fit<-nnetar(train.ts)

Plotting forecasts

#accuracy(forecast(ses.fit,h=12),test.ts)
plot(forecast(ar.fit,h=12))

plot(forecast(nnet.fit,h=12))

Results
Which ones better ?

accuracy(forecast(ar.fit,h=12),test.ts)
##                       ME     RMSE     MAE         MPE     MAPE     MASE
## Training set  -0.2590654 19.50958 15.3308 -0.05308812 1.687965 0.993718
## Test set     -45.7433333 52.70187 46.2900 -5.79637283 5.860406 3.000444
##                     ACF1 Theil's U
## Training set -0.03720927        NA
## Test set      0.51304989  2.553576
accuracy(forecast(nnet.fit,h=12),test.ts)
##                         ME     RMSE      MAE         MPE     MAPE
## Training set -4.248992e-04 19.22373 15.25381 -0.04505324 1.680239
## Test set     -6.104233e+01 68.21423 61.04233 -7.71575843 7.715758
##                   MASE         ACF1 Theil's U
## Training set 0.9887278 -0.004775963        NA
## Test set     3.9566659  0.572395426  3.315077

Conclusion
In this given dataset, nnet doesnt seem to do any better than the earlier models.