Jenny We
Jenny We

Reputation: 105

How to use function RNN in R for demand forecasting

I have been trying to construct a recurrent neural network in R for time series demand forecasting. Keras/tensorflow are just too confusing to use in R so I have tried the rnn package (trainr function) but I feel like this just does not work for demand forecasting. My aim is to use weekly demand in the last 6 weeks to predict the demand next week. My input array (= dependent variables) thus has a time dimension of 6, but my output array (=dependent variable) has a time dimension of 1. My sample size is 30. When I now run trainr, I get the following error message:

Error in trainr(y_train, X_train, learningrate = 0.1, hidden_dim = 2,  : 
  The time dimension of X is different from the time dimension of Y. seq_to_seq_unsync is set to FALSE

In the R documentation for function trainr, it says "Y = array of output values, dim 1: samples (must be equal to dim 1 of X), dim 2: time (must be equal to dim 2 of X) (...)". But how can my input and output ever have the same dim 2: time for demand forecasting, if I have several weeks as input and only one week as output (the week I´m trying to predict)?

Here my code and sample data:

  batch_size <- 32 # number of sequences to look at at one time during training
  total_epochs <- 15 
  X <- demand_matrix[,-ncol(demand_matrix)]
  y <- demand_matrix[,ncol(demand_matrix)]
  X_train <- array(X, dim = c(30, 6, 1))
  y_train <- array(y, dim=c(30,1))
  model <- trainr(y_train,
                  X_train,
                  learningrate = 0.1,
                  hidden_dim = 2,
                  network_type='rnn',
                  numepochs=15,
                  batch_size=6)

Here the sample data (the 7th column is the independent variable (demand next week) and columns 1-6 is the weekly demand in the last 6 weeks)

> demand_matrix
      [,1] [,2] [,3] [,4] [,5] [,6] [,7]
 [1,] 1350 3700 3900 4500 3350 2900 4800
 [2,] 3700 3900 4500 3350 2900 3200 5600
 [3,] 3900 4500 3350 2900 3200 4550 4800
 [4,] 4500 3350 2900 3200 4550 4250 4450
 [5,] 3350 2900 3200 4550 4250 4050 3600
 [6,] 2900 3200 4550 4250 4050 4800 2450
 [7,] 3200 4550 4250 4050 4800 5600 4400
 [8,] 4550 4250 4050 4800 5600 4800 3550
 [9,] 4250 4050 4800 5600 4800 4450 4500
[10,] 4050 4800 5600 4800 4450 3600 4600
[11,] 4800 5600 4800 4450 3600 2450 3600
[12,] 5600 4800 4450 3600 2450 4400 3550
[13,] 4800 4450 3600 2450 4400 3550 4000
[14,] 4450 3600 2450 4400 3550 4500 3000
[15,] 3600 2450 4400 3550 4500 4600 4100
[16,] 2450 4400 3550 4500 4600 3600 4050
[17,] 4400 3550 4500 4600 3600 3550 4100
[18,] 3550 4500 4600 3600 3550 4000 3700
[19,] 4500 4600 3600 3550 4000 3000 4200
[20,] 4600 3600 3550 4000 3000 4100 3700
[21,] 3600 3550 4000 3000 4100 4050 4200
[22,] 3550 4000 3000 4100 4050 4100 3050
[23,] 4000 3000 4100 4050 4100 3700 2800
[24,] 3000 4100 4050 4100 3700 4200 4400
[25,] 4100 4050 4100 3700 4200 3700 4850
[26,] 4050 4100 3700 4200 3700 4200 4700
[27,] 4100 3700 4200 3700 4200 3050 3900
[28,] 3700 4200 3700 4200 3050 2800 4550
[29,] 4200 3700 4200 3050 2800 4400 7200
[30,] 3700 4200 3050 2800 4400 4850 2700

Upvotes: 0

Views: 439

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24262

For your problem you can use the elman or jordan networks in the RSNNS package.

library(RSNNS)
fit <- jordan(X, y, size=20,
    learnFuncParams=c(0.1),
    maxit=1000)

See this link for more details.

Upvotes: 1

Related Questions