# R-squared

> Canonical: https://skillalmanac.com/glossary/r-squared | Last verified: 2026-11-25 | Index status: index-eligible

R-squared compares a regression model's squared prediction errors with the errors from always predicting the observed target mean. Higher is better, but the score can be negative.

Also called: R², coefficient of determination.

## In plain language

R-squared, or R², is a regression score. It equals 1 minus the model's sum of squared errors divided by the sum of squared deviations from the true target mean. A score of 1 is perfect; 0 matches the mean baseline; negative is worse.

## Technical definition

For nonconstant true targets, R² = 1 - residual sum of squares / total sum of squares, where residual sum of squares is sum((true - predicted)^2) and total sum of squares is sum((true - mean(true))^2). Constant targets make the denominator zero, so the formal expression is undefined.

## How it shows up when you use agents

R-squared, written R² and also called the coefficient of determination, evaluates numeric predictions from a regression model. An agent system might use such a model to estimate resolution time, cost or another continuous target.

In plain text, the formula is: R-squared equals 1 minus the sum of squared prediction errors divided by the sum of squared differences between each true value and the mean of the true values. The mean-prediction baseline therefore scores 0, while perfect predictions score 1 when the target is not constant.

Here is an explicitly hypothetical negative example. Let the true values be 1, 2 and 3, so their mean is 2. Predict 3, 2 and 1. The squared prediction errors are 4, 0 and 4, totaling 8. The squared deviations of the true values from their mean are 1, 0 and 1, totaling 2. Therefore R² = 1 - 8/2 = 1 - 4 = -3. The negative score correctly signals that these predictions are worse than always predicting 2.

As a reader action, compare R² on held-out data against a clearly stated baseline, then inspect errors in the target's original units. Report the evaluated population and target alongside the score.

## Common confusions

R² is not confined to the interval from 0 to 1. It can be negative when a model performs worse than the mean baseline. It is not classification accuracy, and a high value does not establish that features caused the target.

Do not assume R² is always the square of a correlation. That identity requires particular conditions and is not the general definition of the regression score.

A constant true target needs special care. Every true value then equals its mean, making the formula's denominator zero; formally, the score is undefined rather than an ordinary finite R². scikit-learn documents the underlying outcomes as NaN for perfect predictions and negative infinity for imperfect predictions. Its optional `force_finite=True` default replaces those results with 1.0 and 0.0, respectively, for practical use in higher-level procedures. Those replacements are implementation behavior, not a change to the formal formula.

## Worked examples in this catalogue

### [data-analysis-eda](https://skillalmanac.com/skills/skill-almanac/data-analysis-eda)

Status: illustrative example from the item's own documentation, not executed for this page.

What you see: Before computing a regression score, profile the supplied CSV and inspect the target column type, missingness and distribution.

Why it exercises the concept: R-squared needs meaningful numeric target values and a nonzero total sum of squares. This preparatory descriptive check helps inspect the target; the skill does not fit a regression model or calculate or validate R-squared.

## Related terms

- [Linear regression](https://skillalmanac.com/glossary/linear-regression.md)
- [Loss function](https://skillalmanac.com/glossary/loss-function.md)
- [Ground truth](https://skillalmanac.com/glossary/ground-truth.md)
- [scikit-learn](https://skillalmanac.com/glossary/scikit-learn.md)

## Sources

- [Machine Learning Glossary: R-squared](https://developers.google.com/machine-learning/glossary#r-squared), Google for Developers (accessed 2026-11-25)
- [r2_score](https://scikit-learn.org/stable/modules/generated/sklearn.metrics.r2_score.html), scikit-learn (accessed 2026-11-25)

Definitions are written by Skill Almanac from the sources above and checked against them on 2026-11-25. [Glossary index](https://skillalmanac.com/glossary.md).
