Skip to content

Commit

Permalink
implementation of the coefficient of determination (R²)
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolas-virionis committed Nov 29, 2021
1 parent 7a53e6e commit 6cea086
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,30 @@ class LinearModelOverTime {
let corr = new Correlation(this._xValues, this._data);
return corr.getCorrelation();
}

/**
* R² is the coefficient of the determination
* which, basically, verifies the accuracy of the
* linear model just calculated
* @returns {number} the coefficient of determination(R²)
*/
getR2() {
let totalSumOfSquares = [
...Correlation.getDifferenceFromMeanAndElements(this._data).map(
x => x ** 2
)
].reduce((sum, x) => sum + x, 0);
let residualSumOfSquares = this._data.reduce(
(sum, element, index) =>
sum +
(element -
(this.getSlope() * this._xValues[index] +
this.getLinearCoefficient())) **
2,
0
);
return 1 - residualSumOfSquares / totalSumOfSquares;
}
}

/**
Expand Down

0 comments on commit 6cea086

Please sign in to comment.