Calculate Variance and Standard Deviation Subflow
Introduction
This subflow is part of our Internal flows collection. Internal flows are used for manipulating and storing data inside Node-RED.
Prerequisites
This flow along with other data generating nodes and flows is available for user in the following package:
@senlab/node-red-statistics-senlab
To learn more about Verdaccio and how to install packages, please refer to this documentation: Verdaccio
Setting I/O nodes and environmental variables
When creating a subflow user needs to define input node and one or more output nodes, so the messages can pass through our custom implementation. In the top bar there are configuration options enabling us to do that.
image::subflows-config.png
Environmental variables can be added to the subflow using the edit properties window, where user can also change the name of the subflow. Environmental variables can be used to make subflow configurable (make subflow reusable in different configurations). For example user can specify environmental variable named addID which is of type boolean and build custom implementation of the code based on this environmental variable where subflow outputs JS object which does or doesnt include ID based on selected option when deploying the subflow.
Inside the subflow environmental variables are accessible with: env.get(’);
Prerequisites
This subflow calculates statistical operation based on incomming array, which can be constructed using saveData subflow, which using context saves array and passes it on in msg object. To read more about saveData subflow visit this link: …
Setting Function Node for Statistical operations
This subflow only calculates statistical operations on the last 1000 elements from the passed array:
const dataset = savedArray.length > 1000 ? savedArray.slice(-1000) : savedArray;
To understand this function, equations for variance and standard deviation are needed:
-
variance:
-standard deviation:
From equations it is clear we need to calculate average and then compare it to the specific value from the array. Standard deviation is just square root value of the variance.
function calculateAverage(array) {
const sum = array.reduce((accumulator, currentValue) => accumulator + currentValue, 0);
return sum / array.length;
}
function calculateVariance(array) {
let varianceArray = [];
let average = calculateAverage(array);
for (let i = 0; i < array.length; i++) {
varianceArray.push(Math.pow(array[i] - average, 2));
}
let variance = calculateAverage(varianceArray);
return variance;
}
This subflow returns JS object inside payload property, object has defined 4 properties named latestValue, which is the last element of the array or last measurement, average, variance and standard deviation.