R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

Time:2022-5-14

Original link: http://tecdat.cn/?p=23902

Recurrent neural networks are used to analyze sequence data. It establishes a recursive connection between hidden units and predicts the output after learning the sequence.
In this tutorial, we will briefly learn how to use the keras RNN model in R to fit and predict multi output sequence data. You can also apply the same method to time series data. We will use keras r interface to implement neural network in R:
 

  1. Prepare data
  2. Define model
  3. Prediction and visualization results

We’ll start by loading the necessary packages for R.

library(keras)

Prepare data

First, we will create a multi output dataset for this tutorial. It is randomly generated data. Here are some rules. There are three input variables and two output variables in this dataset. We will draw the generated data to visually examine it.

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

plot(s, df$y1, ylim = c(min(df), max(df)), type = "l")
lines(s, df$y2, type = "l")
lines(s, df$x1, type = "l")
lines(s, df$x2, type = "l")
lines(s, df$x3, type = "l")

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

Next, we will divide the data into two parts: training and testing. The last 50 elements will be test data.
 

train = df\[1:(n-tsize), \]
test = df\[(n-tsize+1):n, \]

We will create x input and Y output data to train the model and convert them into matrix types.
 

xtrain = as.matrix(data.frame(train$x1, train$x2, train$x3))
ytrain = as.matrix(data.frame(train$y1, train$y2))

Next, we will segment the input and output values by a given step value to prepare the data. In this example, the step value is 2. We will take the first and second lines of X and the second line of Y as a label value. The next element becomes the second and third lines of X and the third line of Y, and the sequence continues until the end. The following table explains how to create a sequence of X and Y data.
 

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

If the step value is 3, we will take 3 rows of X data, and the third row of Y data will be output.
 

dim(trains$x)
\[1\] 798   3   2
dim(trains$y)
\[1\] 798   2

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

Define model

We will define the sequence model by adding a simple RNN layer, a sense layer for output, and an Adam optimizer with an MSE loss function. We will set the input dimension in the first layer of the model and the output dimension in the last layer.

model %>% summary()

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

We will use training data to fit this model.
 

fit(trains$x, trains$y)

And check the accuracy of the training.
 

evaluate(trains$x, trains$y, verbose = 0)
print(scores)

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

Prediction and visualization results

Finally, we will predict the test data and check the accuracy of Y1 and Y2 with RMSE index.
 

 
cat("y1 RMSE:", RMSE(tests$y\[, 1\], ypred\[, 1\]))

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

   

``````
cat("y2 RMSE:", RMSE(tests$y\[, 2\], ypred\[, 2\]))

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

We can visually check the results in the diagram.
``````
plot(x_axes, tests$y\[, 1\], ylim = c(min(tests$y), max(tests$y))type = "l", lwd = 2,

R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series

In this tutorial, we have briefly learned how to use the keras RNN model in R to fit and predict multi output sequential data.  
 


R language deep learning keras recurrent neural network (RNN) model for predicting multi output variable time series 

Most popular insights

1.Analysis of fitting yield curve with Nelson Siegel model improved by neural network in R language

2.R language to realize fitting neural network prediction and result visualization

3.Python uses genetic algorithm neural network fuzzy logic control algorithm to control the lottoanalysis

4.Python for NLP: multi label text LSTM neural network classification using keras

5.An example of using R language to realize neural network to predict stock

6.Deep learning image classification of small data set based on keras in R language

7.An example of seq2seq model for NLP uses keras to realize neural machine translation

8.Analysis of deep learning model based on grid search algorithm optimization in Python

9.Deep learning using Bayesian Optimization in MATLAB