# R-Squared and Adjusted R-Squared

`D00-F09-A09` · Financial Mathematics, Statistics, and Data Foundations → Dependence, Regression, and Model Foundations · archetype `record-transform` · difficulty 1/5 · verification **verified**

Full page: https://docs.thefintechbuilder.com/foundations/dependence-regression-and-model-foundations/r-squared-and-adjusted-r-squared/
Agent skill: `npx skills add IslamBaraka90/Fintech-Algorithms-Library` — https://docs.thefintechbuilder.com/guides/agent-skill/

## Install and import

```bash
npm install fintech-algorithms
```

```ts
import { rSquaredAndAdjustedRSquared } from "fintech-algorithms/foundations/dependence-regression-and-model-foundations/r-squared-and-adjusted-r-squared";
```

## Signature

```ts
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 | Required | Notes |
| --- | --- | --- | --- |
| `input` | `D00Input` | yes | 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

This is the worked example published in the article, replayed by the test suite on every run. The output cannot drift.

### Input

`input`:

```json
{
  "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

```ts
rSquaredAndAdjustedRSquared(input)
```

### Returns

object with 2 fields: rSquared, adjustedRSquared

```json
{
  "rSquared": 0.8889795918367347,
  "adjustedRSquared": 0.8612244897959184
}
```

## Verification and provenance

Tier: **verified** (via input-expected).

The worked example below is the figure published in this algorithm's article, replayed and asserted by the test suite on every build. The arithmetic cannot drift without the build failing.

Both tiers guarantee the signature. Full explanation: https://docs.thefintechbuilder.com/guides/verification/

Generated from the docs.json payload shipped inside fintech-algorithms@0.13.0.
The signature and parameter list are checked against the compiled implementation at build time,
so a description that contradicts the code fails the build rather than reaching this file.

## Links

- Article (how it works, step by step): https://thefintechbuilder.com/foundations/dependence-regression-and-model-foundations/r-squared-and-adjusted-r-squared/
- Implementation source: https://github.com/IslamBaraka90/Fintech-Algorithms-Library/blob/main/src/foundations/dependence-regression-and-model-foundations/r-squared-and-adjusted-r-squared/impl.ts
- Package on npm: https://www.npmjs.com/package/fintech-algorithms
- Domain index for agents: https://docs.thefintechbuilder.com/foundations/llms.txt
