Coefficient of determination calculator
Observed values of 3, 5, 7 and 9 against predictions that each miss by half a unit give an R squared of 0.95, with a residual sum of squares of 1 out of a total of 20. Paste both lists in matching order. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- Observed
- 3, 5, 7, 9
- Predicted
- 2.5, 5.5, 6.5, 9.5
- N
- 4
- R squared
- 0.95
- Sum squared error
- 1
- Total sum squares
- 20
The coefficient of determination calculator compares observed outcomes with predictions and returns R squared, the share of variation explained by the model under the standard sum-of-squares definition. Supply two equally sized numeric arrays whose positions represent matched observations and predictions. The result includes the residual sum of squares, total sum of squares, observed mean, and pair count, making the calculation easy to inspect as well as reuse. It accepts finite numbers, rejects mismatched arrays, and reports when the observed series has no variance, because R squared is not defined in that case.
Prepare matched observed and predicted values
Enter the measured outcomes in observed and the corresponding model outputs in predicted. Position is the pairing rule: the first prediction is compared with the first observation, the second with the second, and so on. Both arrays therefore need the same number of elements, and the calculator returns an error if their lengths differ.
Use raw values expressed on the same scale and in the same units. Do not sort one array independently, remove an item from only one side, or mix probabilities with class labels, because any of those changes destroys the intended pairing. Every element must be a finite number; blank values, nulls, infinity, and text are rejected instead of being silently converted.
At least two pairs are required. If data has missing outcomes, decide how those rows should be handled before calling the calculator, then remove the complete pair consistently. That preparation keeps the resulting score tied to a clearly defined evaluation sample and makes comparisons between models meaningful.
Understand how R squared is calculated
The calculator first finds the mean of the observed values. It then computes the total sum of squares by adding each squared distance from that mean; this quantity represents the variation available to explain. Next it computes the residual sum of squares by adding each squared difference between an observation and its paired prediction.
R squared is one minus the residual sum divided by the total sum. A score of 1 indicates exact predictions for every supplied pair. A score of 0 means the predictions perform no better, by this squared-error comparison, than always using the observed sample mean.
The value can be negative when prediction errors are larger than the mean baseline errors; negative results are valid and should not be clamped to zero. When every observed value is identical, the total sum of squares is zero, so the ratio has no defined value and the calculator returns an error. The response exposes both sums and the observed mean so you can reproduce the calculation or diagnose an unexpected result.
Interpret the result in the right context
R squared summarizes fit on the particular pairs you provide, but it does not establish causation, guarantee useful future predictions, or reveal whether errors follow an acceptable pattern. A high training-set score can coexist with overfitting, so evaluate predictions from a validation or test set when measuring generalization. Compare scores only when the outcome, sample, and evaluation procedure are compatible.
Domain context also matters: a modest score may be useful for a noisy real-world outcome, while a very high score may still be inadequate for precise engineering control. Inspect residual plots and error measures such as mean absolute error or root mean squared error alongside R squared, since those tools show error scale and systematic bias that a single proportion cannot.
This implementation uses the common unweighted definition based on the observed mean; it does not add an intercept, fit a regression, adjust for the number of predictors, or calculate adjusted R squared.
What you can do with it
Compare model revisions
Calculate the same fit statistic on a fixed evaluation sample before and after changing features or parameters.
Audit a reported score
Use the returned error sum, variance sum, and observed mean to reproduce and inspect an R squared result.
FAQ
What does R squared measure?
It compares squared prediction error with variation around the observed mean. Under this definition, 1 is exact fit and 0 matches the mean baseline.
Can R squared be negative?
Yes. A negative value means the supplied predictions have more squared error than predicting the observed sample mean for every item.
Why must the arrays have equal lengths?
Each observed value must be paired by position with exactly one prediction. Different lengths leave one or more values without a counterpart.
Why are constant observed values rejected?
When all observations are equal, their total sum of squares is zero. The R squared ratio is therefore undefined.
Does this calculate adjusted R squared?
No. It calculates ordinary R squared from outcomes and predictions and does not use a predictor count or apply an adjustment.
Related tools
For developers — API access
Everything on this page is available programmatically. This section is for teams who want to wire it into their own systems; everyone else can just use the tool above.
API endpoint
Prefer to automate it? One authenticated POST creates the task; the result comes back by webhook or a signed link. The same capability also runs here on the web, by email and from Telegram — and soon from our app too.
Call it from your stack
curl -X POST https://api.kit.forhosting.com/stat/coefficient-determination \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"observed":[3,5,7,9],"predicted":[2.5,5.5,6.5,9.5]}'const res = await fetch("https://api.kit.forhosting.com/stat/coefficient-determination", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"observed": [
3,
5,
7,
9
],
"predicted": [
2.5,
5.5,
6.5,
9.5
]
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/stat/coefficient-determination",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"observed": [
3,
5,
7,
9
],
"predicted": [
2.5,
5.5,
6.5,
9.5
]
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/stat/coefficient-determination", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"observed":[3,5,7,9],"predicted":[2.5,5.5,6.5,9.5]}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"observed":[3,5,7,9],"predicted":[2.5,5.5,6.5,9.5]}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/stat/coefficient-determination", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"observed": [
3,
5,
7,
9
],
"predicted": [
2.5,
5.5,
6.5,
9.5
]
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "stat.coefficient_determination",
"status": "queued",
"_links": {
"result": "/tasks/tsk_…/result"
}
}The API is asynchronous: the call returns a task_id immediately and the result arrives by webhook. Polling is capped at 1 req/s per task.
Pricing
Published price — no tokens, no invented credits. A failed task is never charged.
Errors
| HTTP | Code | Meaning |
|---|---|---|
401 | unauthorized | Missing or invalid API key. |
402 | insufficient_balance | Your balance doesn't cover the task price. |
404 | unknown_type | That task type doesn't exist. |
429 | rate_limited | Too many requests. Use the webhook instead of polling. |
Technical notes
Interpret the result in the right context
The browser calculation is available directly, while an automated API request is priced at $0.002.
Integration use cases
Evaluate a regression test set
Measure how much observed outcome variation is explained by predictions generated for held-out records.
Technical questions
What does the API request cost?
Each API request costs $0.002. The calculation can also run in the browser.