JSFiddle - React, Tailwind, and code Playground

by sberube

HTML

<!DOCTYPE html>
<html>
<head>
  <title>Autoregressive Time Series Forecasting Example with brain.js</title>
  <script src="https://unpkg.com/brain.js"></script>
</head>
<body>
  <h1>Autoregressive Time Series Forecasting Example with brain.js</h1>
  <div id="chart" style="width: 800px; height: 400px;"></div>
  <div id="forecastData"></div>

  <script>
    // Known data for May
    const knownDataMay = [
      30863.2913428889, 36604.9272690245, 38217.2127046351, 39554.3499133455,
      39628.0639046151, 38480.5997975987, 32060.3781323193, 31738.0255801688,
      36430.2190945386, 40227.781752682604, 42405.815307862504, 43281.9998768272,
      41472.9383109477, 33127.9946772232, 33072.1711896463, 40452.5328671444,
      43123.0619313069, 43430.6345396341, 43503.7409908656, 41504.2129443365,
      33749.5201259648, 33483.963425009, 40394.0950932122, 43467.660867457496,
      45429.3403448995, 45075.904593247404, 45953.0359744252, 36313.3605308205,
      16710.6459635559
    ];
 // Generate missing data for May
    for (let i = 1; i <= 31; i++) {
      const value = knownDataMay[i - 1] || (knownDataMay[i - 2] + knownDataMay[i] / 2); // Best guess value
      knownDataMay[i - 1] = value;
    }

    // Prepare the data for autoregression
    const trainingData = [];
    const trainingLabels = [];

    for (let i = 0; i < 20; i++) {
      trainingData.push([knownDataMay[i]]);
      trainingLabels.push([knownDataMay[i + 1]]);
    }

    // Create the neural network
    const net = new brain.recurrent.RNNTimeStep({
      inputSize: 1, // Number of input values
      hiddenLayers: [10], // Number of neurons in each hidden layer
      outputSize: 1, // Number of output values
    });

    

    // Train the network
    window.console.log(trainingData);
    window.console.log(trainingLabels);
    net.train([trainingData]);

    // Forecasting
    const forecastedData = net.forecast(trainingData, 30);

    // Output forecasted data to the DOM for debugging
    const...