BRNN for forecasting bbry

#Load the library
library(brnn)
## Warning: package 'brnn' was built under R version 3.2.2
## Loading required package: Formula
## Warning: package 'Formula' was built under R version 3.2.2
##Your best bet is to use quantmod and store the results as a time series (in this case, it will be xts):

library(quantmod)
## Warning: package 'quantmod' was built under R version 3.2.2
## Loading required package: xts
## Warning: package 'xts' was built under R version 3.2.2
## Loading required package: zoo
## Warning: package 'zoo' was built under R version 3.2.2
## 
## Attaching package: 'zoo'
## 
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
## 
## Loading required package: TTR
## Warning: package 'TTR' was built under R version 3.2.2
## Version 0.4-0 included new data defaults. See ?getSymbols.
# Time Series Plotting
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.2.2
library(xts)
library(dygraphs)
## Warning: package 'dygraphs' was built under R version 3.2.2
##############################################################
# JPM BBRY


jpm <- getSymbols("JPM",src="yahoo",
                  from = '2011-01-27', to='2013-12-31',auto.assign = FALSE)
##     As of 0.4-0, 'getSymbols' uses env=parent.frame() and
##  auto.assign=TRUE by default.
## 
##  This  behavior  will be  phased out in 0.5-0  when the call  will
##  default to use auto.assign=FALSE. getOption("getSymbols.env") and 
##  getOptions("getSymbols.auto.assign") are now checked for alternate defaults
## 
##  This message is shown once per session and may be disabled by setting 
##  options("getSymbols.warning4.0"=FALSE). See ?getSymbols for more details.
chartSeries(jpm ,theme="white", name = "JP Morgan")

bbry <- getSymbols("BBRY",src="yahoo",
                  from = '2011-01-27', to='2013-12-31',auto.assign = FALSE)
chartSeries(bbry ,theme="white", name = "Black Berry")

################################################################
## data structure

str(jpm)
## An 'xts' object on 2011-01-27/2013-12-31 containing:
##   Data: num [1:737, 1:6] 45 45 44.4 45.2 45.8 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "JPM.Open" "JPM.High" "JPM.Low" "JPM.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2015-09-16 20:42:06"
names(jpm)
## [1] "JPM.Open"     "JPM.High"     "JPM.Low"      "JPM.Close"   
## [5] "JPM.Volume"   "JPM.Adjusted"
str(bbry)
## An 'xts' object on 2011-01-27/2013-12-31 containing:
##   Data: num [1:737, 1:6] 62 62 60.1 59.5 60.5 ...
##  - attr(*, "dimnames")=List of 2
##   ..$ : NULL
##   ..$ : chr [1:6] "BBRY.Open" "BBRY.High" "BBRY.Low" "BBRY.Close" ...
##   Indexed by objects of class: [Date] TZ: UTC
##   xts Attributes:  
## List of 2
##  $ src    : chr "yahoo"
##  $ updated: POSIXct[1:1], format: "2015-09-16 20:42:06"
names(bbry)
## [1] "BBRY.Open"     "BBRY.High"     "BBRY.Low"      "BBRY.Close"   
## [5] "BBRY.Volume"   "BBRY.Adjusted"
#################################################################
##  EMA
#install.packages("TTR")
library(TTR)

EMA_bbry <- EMA(bbry[, "BBRY.Close"], n =5)
###################################################################
## ROC

ROC_bbry <- ROC(bbry[,"BBRY.Close"], n=14)
names(bbry)
## [1] "BBRY.Open"     "BBRY.High"     "BBRY.Low"      "BBRY.Close"   
## [5] "BBRY.Volume"   "BBRY.Adjusted"
mydata <- merge(bbry, EMA_bbry, ROC_bbry )
names(mydata) 
## [1] "BBRY.Open"     "BBRY.High"     "BBRY.Low"      "BBRY.Close"   
## [5] "BBRY.Volume"   "BBRY.Adjusted" "EMA"           "BBRY.Close.1"
names(mydata) <-c("BBRY.Open"  ,   "BBRY.High"  ,   "BBRY.Low" ,  
                  "BBRY.Close"   , "BBRY.Volume"  , "BBRY.Adjusted" ,
                  "EMA" ,  "ROC" )

normailized

# colMax <- function(data) sapply(data, max, na.rm = TRUE)
# colSort <- function(data, ...) sapply(data, sort, ...)
# colMin <- function(data) sapply(data, min, na.rm = TRUE)
# 
# mydata2 <- normalize(mydata,base=colMin(mydata),spread=colMax(mydata))
# sum(is.na(mydata2))

Estimate and Forecast

#With the default S3 method the call is
mydata3 <- na.exclude(mydata)

out=brnn(y=as.vector(mydata3[,6]),x=as.matrix(mydata3[,-6]),
         neurons=6, normalize=TRUE, )
## Number of parameters (weights and biases) to estimate: 54 
## Nguyen-Widrow method
## Scaling factor= 0.7017369 
## gamma= 28.0794    alpha= 2.6105   beta= 54338.51
# with na, not working
#out=brnn(y=as.vector(mydata[,6]),x=as.matrix(mydata[,-6]),
#         neurons=6, normalize=TRUE, )

#With the default S3 method the call is
#out=brnn(y=as.vector(mydata2[,6]),x=as.matrix(mydata2[,-6]),neurons=6)



#length(y)
plot(index(mydata3),mydata3[,6],main="Bayesian Regularization for brry" )
lines(index(mydata3),predict(out),col="blue",lty=2)
legend("topright",legend="Fitted model",col="blue",lty=2,bty="n")

#dim(mydata)
#length(predict(out))