Skip to main content
Module

x/simplestatistics/src/r_squared.js

simple statistics for node & browser javascript
Go to Latest
File
/** * The [R Squared](http://en.wikipedia.org/wiki/Coefficient_of_determination) * value of data compared with a function `f` * is the sum of the squared differences between the prediction * and the actual value. * * @param {Array<Array<number>>} x input data: this should be doubly-nested * @param {Function} func function called on `[i][0]` values within the dataset * @returns {number} r-squared value * @example * var samples = [[0, 0], [1, 1]]; * var regressionLine = linearRegressionLine(linearRegression(samples)); * rSquared(samples, regressionLine); // = 1 this line is a perfect fit */function rSquared(x, func) { if (x.length < 2) { return 1; }
// Compute the average y value for the actual // data set in order to compute the // _total sum of squares_ let sum = 0; for (let i = 0; i < x.length; i++) { sum += x[i][1]; } const average = sum / x.length;
// Compute the total sum of squares - the // squared difference between each point // and the average of all points. let sumOfSquares = 0; for (let j = 0; j < x.length; j++) { sumOfSquares += Math.pow(average - x[j][1], 2); }
// Finally estimate the error: the squared // difference between the estimate and the actual data // value at each point. let err = 0; for (let k = 0; k < x.length; k++) { err += Math.pow(x[k][1] - func(x[k][0]), 2); }
// As the error grows larger, its ratio to the // sum of squares increases and the r squared // value grows lower. return 1 - err / sumOfSquares;}
export default rSquared;