R-Squared and Adjusted R-Squared
Install and import#
npm install fintech-algorithmsimport { rSquaredAndAdjustedRSquared } from "fintech-algorithms/foundations/dependence-regression-and-model-foundations/r-squared-and-adjusted-r-squared";Signature#
rSquaredAndAdjustedRSquared(input)Reports the share of the variation in input.y that the least squares fit on input.x accounts for, and the same figure adjusted for the one estimated slope.
Parameters#
| Name | Type | Notes |
|---|---|---|
input | D00Input | One record holding the predictor series x and the outcome series y, aligned position by position.x: non-empty list of finite numbers, at least two, not all identical · y: finite numbers, same length as x |
Returns#
D00Output
An object with rSquared (one minus residual sum of squares over total sum of squares) and adjustedRSquared (the same, penalised with an n-2 denominator).
Errors#
- When x or y is missing, empty, or contains a non-finite number — throws RangeError
- When x and y have different lengths, or fewer than two observations — the least-squares fit is computed for every topic in the family before the topic branch is taken — throws RangeError
- When x is constant, which leaves the regression slope undefined — throws RangeError
Complexity: time O(n),
space O(n).
Worked example#
verified This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.
Input#
{
"x": [1, 2, 3, 4, 5, 6],
"y": [2, 3, 5, 4, 6, 7],
"groups": ["A", "A", "A", "B", "B", "B"],
"predictX": 7,
"otherPredictor": [2, 4, 5, 8, 9, 13]
}Call#
rSquaredAndAdjustedRSquared(input)Returns#
object with 2 fields: rSquared, adjustedRSquared
{
"rSquared": 0.8889795918367347,
"adjustedRSquared": 0.8612244897959184
}Diagrams#
How it works#
This page states the contract — how to call it correctly. The article explains the concept: why it works, and where it breaks.
References#
- Linear Least Squares Regression — NIST/SEMATECH e-Handbook
- Correlation — NIST/SEMATECH e-Handbook
- Historical-example decision