Recurrent Neural Networks for Nonlinear Time Series Analysis

This algorithm explores the use of Elman networks for nonlinear time series analysis for neuroscience applications.(Work in Progress)

Step 1. Load the libraries

library(RSNNS)
## Loading required package: Rcpp
library(Metrics)

Step 2. Load the Dataset

data(snnsData)

Step 3. Create the Input and Output Layer

laser <- snnsData$laser_1000.pat
inputs <- laser[, inputColumns(laser)]
targets <- laser[, outputColumns(laser)]
patterns <- splitForTrainingAndTest(inputs, targets, ratio = 0.15)

Figure 1. Plot the Inputs and Outputs

plot(inputs, type = "l")

plot of chunk unnamed-chunk-4

plot(targets[1:100], type = "l")

plot of chunk unnamed-chunk-4

Step 4. Specify the number of units in the hidden layer(s) and estimate

modelElman <- elman(patterns$inputsTrain, patterns$targetsTrain, size = c(2, 
    8), learnFuncParams = c(0.1), maxit = 500, inputsTest = patterns$inputsTest, 
    targetsTest = patterns$targetsTest, linOut = FALSE)

modelElman
## Class: elman->rsnns
## Number of inputs: 1 
## Number of outputs: 1 
## Maximal iterations: 500 
## Initialization function: JE_Weights 
## Initialization function parameters: 1 -1 0.3 1 0.5 
## Learning function: JE_BP 
## Learning function parameters: 0.1 
## Update function:JE_Order
## Update function parameters: 0 
## Patterns are shuffled internally: TRUE 
## Compute error in every iteration: TRUE 
## Architecture Parameters:
## $size
## [1] 2 8
## 
## All members of model:
##  [1] "nInputs"               "maxit"                
##  [3] "initFunc"              "initFuncParams"       
##  [5] "learnFunc"             "learnFuncParams"      
##  [7] "updateFunc"            "updateFuncParams"     
##  [9] "shufflePatterns"       "computeIterativeError"
## [11] "snnsObject"            "archParams"           
## [13] "IterativeFitError"     "IterativeTestError"   
## [15] "fitted.values"         "fittedTestValues"     
## [17] "nOutputs"

Step 5. Examine the Model Results


summary(modelElman)
## SNNS network definition file V1.4-3D
## generated at Mon Jun 24 13:05:27 2013
## 
## network name : RSNNS_untitled
## source files :
## no. of units : 22
## no. of connections : 114
## no. of unit types : 0
## no. of site types : 0
## 
## 
## learning function : JE_BP
## update function   : JE_Order
## 
## 
## unit default section :
## 
## act      | bias     | st | subnet | layer | act func     | out func
## ---------|----------|----|--------|-------|--------------|-------------
##  1.00000 |  0.00000 | i  |      0 |     1 | Act_Logistic | Out_Identity 
## ---------|----------|----|--------|-------|--------------|-------------
## 
## 
## unit definition section :
## 
## no. | typeName | unitName | act      | bias     | st | position | act func     | out func | sites
## ----|----------|----------|----------|----------|----|----------|--------------|----------|-------
##   1 |          | inp1     |  0.09020 | -0.02119 | i  |  1, 1,32767 | Act_Identity |          | 
##   2 |          | hid11    |  0.30968 | -0.01596 | h  |  7, 1, 0 |||
##   3 |          | hid12    |  0.20888 | -1.74080 | h  |  7, 2, 0 |||
##   4 |          | hid21    |  0.46664 |  0.04469 | h  | 13, 1, 0 |||
##   5 |          | hid22    |  0.51463 | -1.20645 | h  | 13, 2, 0 |||
##   6 |          | hid23    |  0.44575 |  0.40729 | h  | 13, 3, 0 |||
##   7 |          | hid24    |  0.45016 |  0.80004 | h  | 13, 4, 0 |||
##   8 |          | hid25    |  0.40030 |  0.67618 | h  | 13, 5, 0 |||
##   9 |          | hid26    |  0.55678 | -0.70192 | h  | 13, 6, 0 |||
##  10 |          | hid27    |  0.52572 | -1.27268 | h  | 13, 7, 0 |||
##  11 |          | hid28    |  0.19948 | -0.39040 | h  | 13, 8, 0 |||
##  12 |          | out1     |  0.26994 |  0.45042 | o  | 19, 1,32767 |||
##  13 |          | con11    |  0.46408 |  0.50000 | sh  |  4,10, 1 | Act_Identity |          | 
##  14 |          | con12    |  0.31445 |  0.50000 | sh  |  4,11, 1 | Act_Identity |          | 
##  15 |          | con21    |  0.68102 |  0.50000 | sh  | 10,10, 1 | Act_Identity |          | 
##  16 |          | con22    |  0.76107 |  0.50000 | sh  | 10,11, 1 | Act_Identity |          | 
##  17 |          | con23    |  0.62036 |  0.50000 | sh  | 10,12, 1 | Act_Identity |          | 
##  18 |          | con24    |  0.63113 |  0.50000 | sh  | 10,13, 1 | Act_Identity |          | 
##  19 |          | con25    |  0.53929 |  0.50000 | sh  | 10,14, 1 | Act_Identity |          | 
##  20 |          | con26    |  0.81881 |  0.50000 | sh  | 10,15, 1 | Act_Identity |          | 
##  21 |          | con27    |  0.77545 |  0.50000 | sh  | 10,16, 1 | Act_Identity |          | 
##  22 |          | con28    |  0.27899 |  0.50000 | sh  | 10,17, 1 | Act_Identity |          | 
## ----|----------|----------|----------|----------|----|----------|--------------|----------|-------
## 
## 
## connection definition section :
## 
## target | site | source:weight
## -------|------|---------------------------------------------------------------------------------------------------------------------
##      2 |      | 14:-1.96160, 13:-0.04485,  1:-0.80167
##      3 |      | 14: 1.57281, 13: 1.13371,  1:-8.06892
##      4 |      | 22:-0.64351, 21: 0.09227, 20:-0.92815, 19: 1.71869, 18: 0.31350, 17:-1.22703, 16:-0.52040, 15: 0.56170,  3: 1.22199,
##                  2: 0.72889
##      5 |      | 22:-0.35438, 21:-0.31907, 20:-1.39945, 19: 1.72658, 18: 0.24476, 17:-0.14026, 16: 0.00429, 15: 1.34410,  3: 2.72724,
##                  2: 1.44400
##      6 |      | 22: 0.36264, 21:-0.42620, 20: 1.08274, 19:-0.94037, 18: 0.74338, 17: 0.01017, 16:-0.21346, 15:-1.05967,  3:-1.05466,
##                  2:-0.57466
##      7 |      | 22:-0.57321, 21: 0.13133, 20: 0.94163, 19:-1.35283, 18:-0.09861, 17:-0.19870, 16:-0.00645, 15:-0.73964,  3:-2.40766,
##                  2: 0.18975
##      8 |      | 22: 1.30029, 21:-0.18479, 20: 1.52784, 19:-2.21044, 18: 0.90824, 17: 0.61802, 16:-0.16392, 15:-1.94502,  3:-4.27347,
##                  2:-0.23237
##      9 |      | 22:-0.35908, 21:-0.08693, 20:-1.46974, 19: 1.33726, 18:-0.36975, 17:-0.00334, 16: 0.42290, 15: 1.12591,  3: 2.66005,
##                  2: 0.90120
##     10 |      | 22:-1.00084, 21:-1.02441, 20:-0.44084, 19: 1.95835, 18: 0.15043, 17:-0.24774, 16: 0.79996, 15: 0.56656,  3: 2.53500,
##                  2: 1.39921
##     11 |      | 22:-0.45796, 21: 0.50400, 20:-0.36568, 19:-0.97629, 18: 0.45994, 17: 0.40505, 16:-0.77192, 15:-0.43711,  3:-0.46044,
##                  2: 0.01234
##     12 |      | 11: 0.75518, 10:-3.47071,  9:-3.15481,  8: 5.70142,  7: 2.93322,  6: 2.52514,  5:-3.54791,  4:-1.96566
##     13 |      | 13: 0.30000,  2: 1.00000
##     14 |      | 14: 0.30000,  3: 1.00000
##     15 |      | 15: 0.30000,  4: 1.00000
##     16 |      | 16: 0.30000,  5: 1.00000
##     17 |      | 17: 0.30000,  6: 1.00000
##     18 |      | 18: 0.30000,  7: 1.00000
##     19 |      | 19: 0.30000,  8: 1.00000
##     20 |      | 20: 0.30000,  9: 1.00000
##     21 |      | 21: 0.30000, 10: 1.00000
##     22 |      | 22: 0.30000, 11: 1.00000
## -------|------|---------------------------------------------------------------------------------------------------------------------

Figure 1. Model Visualization for 100 Observations

plotIterativeError(modelElman)

plot of chunk unnamed-chunk-7

plot(targets[1:100], type = "l")
lines(modelElman$fitted.values[1:100], col = "red")

plot of chunk unnamed-chunk-7

Figure 2. Model Diagnostics

plotRegressionError(patterns$targetsTrain, modelElman$fitted.values)

plot of chunk unnamed-chunk-8

plotRegressionError(patterns$targetsTest, modelElman$fittedTestValues)

plot of chunk unnamed-chunk-8

hist(modelElman$fitted.values - patterns$targetsTrain)

plot of chunk unnamed-chunk-8

Step 6. Compute MAE, RMSE and RMSLE Metrics based on Output Sample

mae(targets[1:100], modelElman$fitted.values[1:100])
## [1] 0.02193
rmse(targets[1:100], modelElman$fitted.values[1:100])
## [1] 0.03408
rmsle(targets[1:100], modelElman$fitted.values[1:100])
## [1] 0.02399

Work in development at The Cromwell Workshop - www.cromwellworkshop.com by Jeff B. Cromwell, PhD