Statistics
How To Calculate The Coefficient Of Determination
The coefficient of determination answers a direct question: what share of the variation in your observed values is explained by your model's predictions? This page works through the ratio of residual variation to total variation, shows what values of one, zero and below zero mean, and explains the adjusted figure used when a single predictor is fitted to four observations.
Last updated
Quick Answer
R-squared = 1 - SSresidual / SStotal
- R-squared
- Coefficient of determination, the explained share of variance
- SSresidual
- Sum of squared gaps between observations and predictions
- SStotal
- Sum of squared gaps between observations and their mean
- n
- Number of observations
- k
- Number of predictors in the model
Square the gap between each observed value and its prediction and add them to get the residual sum of squares. Square the gap between each observation and the mean and add those to get the total sum of squares. Divide the first by the second and subtract from one. With observations 3, 5, 7, 9 and predictions 4, 5, 6, 8, the residual sum is 3, the total sum is 20 and R-squared is 0.85.
What Is The Coefficient Of Determination?
The coefficient of determination, written R-squared, is the proportion of the variation in observed values that a model accounts for. It answers a practical question: how much of the scatter in the data is explained by the fitted relationship, and how much is left over as error? In this teaching example the observed values are 3, 5, 7 and 9, while the model predicts 4, 5, 6 and 8. Comparing the two sets gives R-squared = 0.85, so the model explains 85 per cent of the variance and leaves 15 per cent unexplained. That single number is a compact score for how closely the predictions track the observations.
The calculation rests on two quantities called sums of squares. The residual sum of squares adds up the squared gaps between each observed value and its prediction, measuring the error the model failed to explain. The total sum of squares adds up the squared gaps between each observed value and the mean of all observations, measuring the total variation present in the data. Dividing the first by the second gives the unexplained fraction, and subtracting that fraction from one gives the explained fraction. Because both sums are measured in squared units, the ratio is dimensionless and easy to compare across problems.
The arithmetic for the example is short. The mean of 3, 5, 7 and 9 is 6. The residuals against the predictions are -1, 0, 1 and 1, whose squares sum to 3. The deviations from the mean are -3, -1, 1 and 3, whose squares sum to 20. The ratio 3 over 20 is 0.15, so R-squared is 1 minus 0.15, or 0.85, which matches the calculator output exactly.
The scale runs from one down through zero and can extend below it. A perfect set of predictions makes every residual zero, the residual sum of squares becomes zero, and R-squared equals one. A model that simply predicts the mean for every case has a residual sum of squares equal to the total sum of squares, giving exactly zero. Because the residual sum can exceed the total sum, a genuinely poor model can produce a negative value, which is a useful warning rather than a calculation error. There is no upper bound on how bad the value can be.
Consider replacing the predictions with the constant values 5, 5, 5 and 5. The residuals become -2, 0, 2 and 4, whose squares sum to 24, while the total sum of squares stays at 20. R-squared is then 1 minus 24 over 20, which is -0.2. This is worse than the mean-only baseline, because a constant prediction of 6 would have scored zero, and the negative sign tells you the fitted model performs worse than simply guessing the average. A negative result is a signal to rethink the model rather than a number to report with pride.
The plain R-squared always rises when extra predictors are added, even useless ones, so an adjusted version penalises complexity. It converts the unexplained fraction using the factor (n - 1) divided by (n - k - 1), where n is the number of observations and k is the number of predictors. Here n is four and k is one, so the factor is three over two. Applying it to the unexplained fraction of 0.15 gives 0.225, and one minus that is the adjusted value of 0.775. The adjusted figure is deliberately lower and is the one to quote when comparing models with different numbers of predictors.
A high R-squared is not evidence of causation. A model can fit two variables tightly simply because both drift with time, because of a shared underlying cause, or by chance in a small sample. The statistic measures association within the fitted sample, not the presence of a causal mechanism. Claims of causation need a sound research design, careful control of other influences and domain reasoning, not a large value of R-squared. A beautifully straight line can still be completely spurious.
Several limits are worth keeping in mind when interpreting the figure. The decomposition used here assumes a model with an intercept, which guarantees the residual sum is no larger than the total sum and keeps R-squared at or below one. It is most familiar in ordinary least squares regression, and other modelling frameworks use different pseudo measures that are not directly comparable with this one. R-squared also tends to rise as more predictors are added, so it should not be used on its own to choose between models of different sizes. A small sample such as four observations gives a very unstable estimate of how the model would perform on new data.
This calculator works with exactly four paired observations, entered as observed values and their matching predictions. It is a teaching model designed to make the arithmetic visible, not a general regression package for large data sets. The same formula applies to any number of pairs, provided you compute the mean and both sums of squares over all observations rather than a subset. Other conventions and related statistics exist, and the adjusted figure shown assumes one predictor, so treat these outputs as a clear illustration of the underlying idea rather than a complete statistical analysis.
Formula
R-squared = 1 - SSresidual / SStotal
Formula page →The core definition: one minus the fraction of total variation left unexplained by the model. It gives 1 for a perfect fit, 0 for predicting the mean, and a negative value for a model worse than the mean.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| R-squared | Coefficient of determination | dimensionless | The explained share of the total variance, expressed as a fraction rather than a percentage. |
| SSresidual | Residual sum of squares | squared units | Sum over all cases of the squared difference between the observed value and its prediction. |
| SStotal | Total sum of squares | squared units | Sum over all cases of the squared difference between the observed value and the mean of observations. |
SSresidual = sum of (yi - pi)^2
Formula page →Square each observed minus predicted gap and add them all. Squaring removes the sign, so large misses count much more than small ones and cannot cancel out.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| yi | Observed value for case i | value units | The actual measured value paired with prediction pi. |
| pi | Predicted value for case i | value units | The model's fitted value for the same case, sometimes written as y-hat. |
R-squared adjusted = 1 - (1 - R-squared) * (n - 1) / (n - k - 1)
Formula page →Penalises the addition of predictors. With four observations and one predictor the multiplier is three over two, turning an R-squared of 0.85 into an adjusted value of 0.775.
| Symbol | Meaning | Unit | Notes |
|---|---|---|---|
| n | Number of observations | count | Four in this calculator, one paired observation for each y and p field. |
| k | Number of predictors | count | Fixed at one here, so the denominator n - k - 1 equals two. |
| R-squared | Unadjusted coefficient of determination | dimensionless | The value from the main formula before the complexity penalty is applied. |
How To Calculate The Coefficient Of Determination
- 1
Find the mean of the observed values
Add the observed values y1 to y4 and divide by four. For 3, 5, 7 and 9 the total is 24 and the mean is 6. This mean is the baseline that a model with no useful predictors would predict for every case, and it anchors the total sum of squares.
- 2
Compute the residual sum of squares
For each pair, subtract the prediction p from the observed value y, square the result, and add all four squared gaps. With predictions 4, 5, 6 and 8 the gaps are -1, 0, 1 and 1, whose squares add to 3. This is the variation the model leaves unexplained.
- 3
Compute the total sum of squares
For each observed value, subtract the mean found in step one, square the difference, and add all four results. Around a mean of 6 the gaps are -3, -1, 1 and 3, whose squares add to 20. This is the total variation present in the observed data before any model is applied.
- 4
Divide and subtract from one
Divide the residual sum of squares by the total sum of squares to get the unexplained fraction, then subtract it from one. Here 3 over 20 is 0.15, so R-squared is 1 - 0.15 = 0.85. If the residual sum ever exceeds the total sum, this step correctly produces a negative value.
- 5
Apply the adjustment if required
For one predictor over four observations multiply the unexplained fraction by (n - 1) / (n - k - 1), which is three over two. That gives 0.225, so the adjusted R-squared is 0.775. Report the plain value when describing fit in this sample, and the adjusted value when comparing models with different predictor counts.
Examples
Example 1: Default case — observations 3, 5, 7, 9 against predictions 4, 5, 6, 8
- y1
- 3
- p1
- 4
- y2
- 5
- p2
- 5
- y3
- 7
- p3
- 6
- y4
- 9
- p4
- 8
| Step | Calculation | Result |
|---|---|---|
| Mean of observed values | (3 + 5 + 7 + 9) / 4 | 6 |
| Residual sum of squares | (-1)^2 + 0^2 + 1^2 + 1^2 | 3 |
| Total sum of squares | (-3)^2 + (-1)^2 + 1^2 + 3^2 | 20 |
| R-squared | 1 - 3 / 20 | 0.85 |
Result: R-squared = 0.85 (SSresidual = 3, SStotal = 20, adjusted R-squared = 0.775).
Example 2: Perfect predictions — predictions equal the observations
- y1
- 3
- p1
- 3
- y2
- 5
- p2
- 5
- y3
- 7
- p3
- 7
- y4
- 9
- p4
- 9
| Step | Calculation | Result |
|---|---|---|
| Mean of observed values | (3 + 5 + 7 + 9) / 4 | 6 |
| Residual sum of squares | 0^2 + 0^2 + 0^2 + 0^2 | 0 |
| Total sum of squares | (-3)^2 + (-1)^2 + 1^2 + 3^2 | 20 |
| R-squared | 1 - 0 / 20 | 1 |
Result: R-squared = 1 (perfect fit; SSresidual = 0, SStotal = 20).
Example 3: Worse than the mean — constant predictions of 5
- y1
- 3
- p1
- 5
- y2
- 5
- p2
- 5
- y3
- 7
- p3
- 5
- y4
- 9
- p4
- 5
| Step | Calculation | Result |
|---|---|---|
| Mean of observed values | (3 + 5 + 7 + 9) / 4 | 6 |
| Residual sum of squares | (-2)^2 + 0^2 + 2^2 + 4^2 | 24 |
| Total sum of squares | (-3)^2 + (-1)^2 + 1^2 + 3^2 | 20 |
| R-squared | 1 - 24 / 20 | -0.2 |
Result: R-squared = -0.2 (SSresidual = 24, SStotal = 20).
Calculator
R-squared
0.85
- Residual sum of squares
- 3
- Total sum of squares
- 20
- Adjusted R-squared
- 0.775
Values update as you type. This calculator covers the single scenario its formula assumes — see Common Mistakes for what it leaves out.
Prefer a full-width tool? Open the The Coefficient Of Determination calculator page.
Common Mistakes
Forgetting to square the residuals
Adding raw observed-minus-predicted gaps lets positive and negative errors cancel. The residuals in the default case are -1, 0, 1 and 1, which sum to 1 rather than 3, and that wrong total would give a misleading R-squared. Always square before summing so every gap contributes positively.
Using the wrong baseline for total variation
The total sum of squares compares observations with their own mean of 6, not with zero or with the model's predictions. Using zero as the baseline gives 3 + 25 + 49 + 81 = 158 instead of 20, which collapses the ratio and destroys the result. Compute the mean first and subtract it from each observed value.
Treating a negative value as impossible
A model can fit worse than the simple mean baseline, which makes the residual sum exceed the total sum and pushes R-squared below zero. The constant predictions of 5 give -0.2 here. A negative result is valid arithmetic and a strong hint that the model is poorly specified, so investigate rather than discard it.
Confusing R-squared with correlation
In simple regression the square of the correlation equals R-squared, but the correlation itself keeps a sign while R-squared does not. Reporting R-squared tells you strength of fit but not the direction of the relationship. Read the slope or the correlation separately when direction matters.
Assuming a high value proves causation
A tight fit can arise from a shared trend, a hidden common cause or chance in a small sample. The statistic only measures association in the fitted data. Causal claims require appropriate design and reasoning, not a number close to one.
Comparing plain R-squared across models of different sizes
Adding predictors can never increase the residual sum in an ordinary fitted model, so plain R-squared drifts upward as a model grows. That makes it biased toward complicated models. Use the adjusted value, which applies the three-over-two penalty for one predictor over four observations, when comparing specifications.
FAQ
What does an R-squared of 0.85 actually mean?
It means the model explains 85 per cent of the variance in the observed values, leaving 15 per cent as unexplained residual variation. In the worked case the residual sum of 3 is 15 per cent of the total sum of 20. It is a share of variance, not a percentage of predictions that are correct.
Can R-squared be negative?
Yes. If the predictions are worse than simply guessing the mean for every case, the residual sum of squares exceeds the total sum of squares and the value falls below zero. Predicting a constant 5 for the observations 3, 5, 7 and 9 gives -0.2, compared with zero for predicting the mean of 6.
What is the difference between R-squared and adjusted R-squared?
Plain R-squared rewards adding predictors, even unhelpful ones, because it only measures in-sample fit. Adjusted R-squared multiplies the unexplained fraction by (n - 1) / (n - k - 1) to penalise complexity. With four observations and one predictor that factor is three over two, lowering 0.85 to 0.775.
Does a value of one mean the model is correct?
It means the predictions match the observed values exactly in this sample, so every residual is zero. It does not prove the model is structurally right, because an over-flexible fit can pass through every point and still predict new data badly. Perfect in-sample fit and genuine explanatory power are different things.
Why do I need the mean before calculating anything?
The mean of 6 is the baseline that defines total variation. The total sum of squares is built from deviations around that mean, while the residual sum is built from deviations around the predictions. Without the mean you cannot measure how much variation the model has actually explained.
Can I use this calculator with more than four data points?
This page is fixed at four paired observations to keep the arithmetic transparent. The underlying formula works for any sample size: take the mean over all observations, sum all squared residuals and all squared deviations from the mean, then subtract the ratio from one. The adjusted version also needs the observation count and the number of predictors.
References
- [1], Coefficient of determination — https://en.wikipedia.org/wiki/Coefficient_of_determination
- [2], Residual sum of squares — https://en.wikipedia.org/wiki/Residual_sum_of_squares
- [3], Regression analysis — https://en.wikipedia.org/wiki/Regression_analysis
