I want to calculate the standardDeviation from value in a generated JSON file. The data in my JSON file looks like this.:
[{"metingen":"metingen","data":[73.55,74.95,73.51,74.78,74.8,74.71,75.65,75.21,74.96,74.3,73.85,73.88,74.24,74.26,74.12,74.24,74.56,74.7,74.91,75.3,75.23,74.56,73.27,73.34,72.72,73.05,72.48,73.58,73.87,73.77,75.85,74.07,72.89,72.92,73.15,73.19,72.64,75.14,76.1,76.22,76.1,76.11,75.78,72.88,74.37,75.17,73.08,72.9,72.82,72.12,71.65,72.88,72.3,72.61,73.26,74,74.17,74.13,72.01,74.93,74.39,73.26,74.87,76.04,76.75,76.24,76.03,76.49,76.35,76.2,76.05,76.37,75.18,75.65,75.43,75.9,75.49,75.23,75.33,74.87,74.84,75,73.13,73.45,73.68,73.41,73.53,74.11,73.56,74.24,73.93,75.83,75.62,74.62,75.56,75.5,75.21,74.21,75.82,76.35,75.31,75.89,76.56,76.91,76.03,75.65,75.89,74.65,75.92,75.38,75.47,76.31,76.45,76.63,76.04,76.32,75.51,75.57,75.91,76.38,76.38,76.27,76.32,75.53,74.63,75.72,76.4,76.19,75.82,75.91,75.23,74.94,75.63,76.04,75.37,75.16,74.67,76.38,76.12,75.91,77.1,76.73,76.76,76.3,76.63,74.41,73.95,73.88,73.61,68.85,68.23,68.96,67.14,66.56,69.49,69.72,71.68,70.57,70.69,70.51,71.91,70.81,70.73,75.73,73.03,72.99,74.34,73.77,73.85,76.89,77.4,76.72,76.2,75.83,75.56,74.71,75.21,75.04,74.54,74.61,77.66,74.78,77.36,77.1,76.78,76.3,76.12,76.16,76.06,76.18,75.27,74.24,74.25,72.51,76.38,76.27,76.15,76.11,74.9,73.28,73.32,73.3,71.35,74.01,74.94,74.76,75.48,74.3,74.44,73.53,73.51,75.38,75.9,75.98,75.27,75.56,74.99,75.57,75.79,76.51,77.34,77.27,76.9,77.39,75.09,75.4,75.97,76.23,76.11,75.99,75.91,75.48,76.84,76.17,75.88,76.01,74.71,75.13,75.11,75.37,75.87,76.99,77.37,76.99,76.79,76.18,74.83,74.47,74.74,75.02,74.99,75.27,76.32,76.06,76.32,75.93,75.7,76.2,76.06,76.17,75.13,76.34,75.29,75.66,74.95,76.39,76.48,75.99,76.79,76.45,76.4,76.75,76.55,76.17,74.35,74.91,74.46,77.54,77.07,76.93,76.5,76.01,76.16,75.89,75.67,76.56,76.55,76.74,75.68,74.66,75.12,74.93,75.03,74.28,76.3,79.12,76.09,75.89,75.68,75.71,75.92,76.1,75.84,75.55,75.25,74.73,77.06,77.87,77.81,75.71,77.1,77.12,76.68,75.95,74.86,77.1,75.27,75.08,75.47,75.43,75.59,74.54,73.37,73.82,73.65,74.11,74.3,76.11,77.24,77.4,73.71,72.24,72.31,70.62,71.11,70.76,71.88,75.01,75.08,75.18,75.15,76.66,76.37,76.44,76.59,76.81,75.71,77.92,77.37,77.1,77.28,74.73,76.03,75.97,76.02,75.21,75.66,75.27,73.68,73.17,73.5,74.75,74.15,74.7,75.61,75.86,75.61,75.32,74.94,75.38,75.49,74.92,75.75,75.95,75.44,75.9,75.56,75.59,75.03,75.47,75.73,74.94,75.46,75.7,75.37,76.04,75.65,75.9,75.74,74.02,76.54,76.83,76.55,76.29,76.34,76.43,76.47,76.54,75.94,75.66,76,75.72,75.11,75.52,76.59,76.44,76.53,76.51,76.26,74.1,73.36,76.1,75.63,75.02,74.66,76.02,74.51,75.24,74.97,74.99,74.78,73.76,73.44,73.83,74.85,75.17,75.47,74.72,76.11,76.14,76.31,76.58,76.78,75.58,75.92,75.57,76.05,75.61,76.24,76.12,75.55,75.88,76.59,76.51,76.04,76.52,76.52,76.15,76.14,74.43,75.92,75.89,75.53,75.61,76.82,75.48,75.66,75.97,76.63,77.02,76.92,76.54,76.43,74.52,77.11,75.85,76.07,75.91,75.65,75.11,75.41,76.31,77.1]}]
I have the next code that calculate this with in the console part numbers to test.:
const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc val, 0) / arr.length;
return Math.sqrt(
arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc val, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
};
console.log(standardDeviation([10, 2, 38, 23, 38, 23, 21]));
console.log(standardDeviation([10, 2, 38, 23, 38, 23, 21], true));
The snipped shows that it works.
My values in the begin of my post are read with the next code.:
$.getJSON("lmetingen1l.json", function(data1) {
for (let j = 0; j < data1[0].data.length; j = 2) {
processedDataj.push(data1[0].data[j 1]);
}
console.log(processedDataj);
data1[0].data = processedDataj;
}
);
But how can i calculate my values with that standardDeviation part? Can't get it to work :(
Who can help, maybe its something simple what i miss.
CodePudding user response:
You should pass an array to the standardDeviation
function. You can do so directly with the array of your object.
var data1 = [{
"metingen": "metingen",
"data": [73.55, 74.95, 73.51, 74.78, 74.8, 74.71, 75.65, 75.21, 74.96, 74.3, 73.85, 73.88, 74.24, 74.26, 74.12, 74.24, 74.56, 74.7, 74.91, 75.3, 75.23, 74.56, 73.27, 73.34, 72.72, 73.05, 72.48, 73.58, 73.87, 73.77, 75.85, 74.07, 72.89, 72.92, 73.15, 73.19, 72.64, 75.14, 76.1, 76.22, 76.1, 76.11, 75.78, 72.88, 74.37, 75.17, 73.08, 72.9, 72.82, 72.12, 71.65, 72.88, 72.3, 72.61, 73.26, 74, 74.17, 74.13, 72.01, 74.93, 74.39, 73.26, 74.87, 76.04, 76.75, 76.24, 76.03, 76.49, 76.35, 76.2, 76.05, 76.37, 75.18, 75.65, 75.43, 75.9, 75.49, 75.23, 75.33, 74.87, 74.84, 75, 73.13, 73.45, 73.68, 73.41, 73.53, 74.11, 73.56, 74.24, 73.93, 75.83, 75.62, 74.62, 75.56, 75.5, 75.21, 74.21, 75.82, 76.35, 75.31, 75.89, 76.56, 76.91, 76.03, 75.65, 75.89, 74.65, 75.92, 75.38, 75.47, 76.31, 76.45, 76.63, 76.04, 76.32, 75.51, 75.57, 75.91, 76.38, 76.38, 76.27, 76.32, 75.53, 74.63, 75.72, 76.4, 76.19, 75.82, 75.91, 75.23, 74.94, 75.63, 76.04, 75.37, 75.16, 74.67, 76.38, 76.12, 75.91, 77.1, 76.73, 76.76, 76.3, 76.63, 74.41, 73.95, 73.88, 73.61, 68.85, 68.23, 68.96, 67.14, 66.56, 69.49, 69.72, 71.68, 70.57, 70.69, 70.51, 71.91, 70.81, 70.73, 75.73, 73.03, 72.99, 74.34, 73.77, 73.85, 76.89, 77.4, 76.72, 76.2, 75.83, 75.56, 74.71, 75.21, 75.04, 74.54, 74.61, 77.66, 74.78, 77.36, 77.1, 76.78, 76.3, 76.12, 76.16, 76.06, 76.18, 75.27, 74.24, 74.25, 72.51, 76.38, 76.27, 76.15, 76.11, 74.9, 73.28, 73.32, 73.3, 71.35, 74.01, 74.94, 74.76, 75.48, 74.3, 74.44, 73.53, 73.51, 75.38, 75.9, 75.98, 75.27, 75.56, 74.99, 75.57, 75.79, 76.51, 77.34, 77.27, 76.9, 77.39, 75.09, 75.4, 75.97, 76.23, 76.11, 75.99, 75.91, 75.48, 76.84, 76.17, 75.88, 76.01, 74.71, 75.13, 75.11, 75.37, 75.87, 76.99, 77.37, 76.99, 76.79, 76.18, 74.83, 74.47, 74.74, 75.02, 74.99, 75.27, 76.32, 76.06, 76.32, 75.93, 75.7, 76.2, 76.06, 76.17, 75.13, 76.34, 75.29, 75.66, 74.95, 76.39, 76.48, 75.99, 76.79, 76.45, 76.4, 76.75, 76.55, 76.17, 74.35, 74.91, 74.46, 77.54, 77.07, 76.93, 76.5, 76.01, 76.16, 75.89, 75.67, 76.56, 76.55, 76.74, 75.68, 74.66, 75.12, 74.93, 75.03, 74.28, 76.3, 79.12, 76.09, 75.89, 75.68, 75.71, 75.92, 76.1, 75.84, 75.55, 75.25, 74.73, 77.06, 77.87, 77.81, 75.71, 77.1, 77.12, 76.68, 75.95, 74.86, 77.1, 75.27, 75.08, 75.47, 75.43, 75.59, 74.54, 73.37, 73.82, 73.65, 74.11, 74.3, 76.11, 77.24, 77.4, 73.71, 72.24, 72.31, 70.62, 71.11, 70.76, 71.88, 75.01, 75.08, 75.18, 75.15, 76.66, 76.37, 76.44, 76.59, 76.81, 75.71, 77.92, 77.37, 77.1, 77.28, 74.73, 76.03, 75.97, 76.02, 75.21, 75.66, 75.27, 73.68, 73.17, 73.5, 74.75, 74.15, 74.7, 75.61, 75.86, 75.61, 75.32, 74.94, 75.38, 75.49, 74.92, 75.75, 75.95, 75.44, 75.9, 75.56, 75.59, 75.03, 75.47, 75.73, 74.94, 75.46, 75.7, 75.37, 76.04, 75.65, 75.9, 75.74, 74.02, 76.54, 76.83, 76.55, 76.29, 76.34, 76.43, 76.47, 76.54, 75.94, 75.66, 76, 75.72, 75.11, 75.52, 76.59, 76.44, 76.53, 76.51, 76.26, 74.1, 73.36, 76.1, 75.63, 75.02, 74.66, 76.02, 74.51, 75.24, 74.97, 74.99, 74.78, 73.76, 73.44, 73.83, 74.85, 75.17, 75.47, 74.72, 76.11, 76.14, 76.31, 76.58, 76.78, 75.58, 75.92, 75.57, 76.05, 75.61, 76.24, 76.12, 75.55, 75.88, 76.59, 76.51, 76.04, 76.52, 76.52, 76.15, 76.14, 74.43, 75.92, 75.89, 75.53, 75.61, 76.82, 75.48, 75.66, 75.97, 76.63, 77.02, 76.92, 76.54, 76.43, 74.52, 77.11, 75.85, 76.07, 75.91, 75.65, 75.11, 75.41, 76.31, 77.1]
}]
const standardDeviation = (arr, usePopulation = false) => {
const mean = arr.reduce((acc, val) => acc val, 0) / arr.length;
return Math.sqrt(
arr.reduce((acc, val) => acc.concat((val - mean) ** 2), []).reduce((acc, val) => acc val, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
};
// $.getJSON("lmetingen1l.json", function(data1) {
console.log(standardDeviation(data1[0].data));
// });