Linear regression calculator
See how well a straight line explains your data: x of 1 to 5 with y of 2, 4, 5, 4 and 5 gives slope 0.6, intercept 2.2 and an R-squared of 0.6, with every residual listed. Enter two lists or a set of points. Free in your browser.
- Free
- Runs in your browser
- No sign-up
- Your data stays on your device
Run — free
Worked example
- X
- 1, 2, 3, 4, 5
- Y
- 2, 4, 5, 4, 5
- N
- 5
- Mean x
- 3
- Mean y
- 4
- Slope
- 0.6
Simple linear regression finds the straight line that best explains how a response y changes with a predictor x under ordinary least squares. The fitted model is written y equals m times x plus b, where the slope m is the expected change in y for a one-unit rise in x and the intercept b is the fitted value when x is zero. Goodness of fit is summarized by R-squared, the fraction of variance in y explained by the line, which equals one minus residual sum of squares over total sum of squares. Students meet this tool in algebra and introductory statistics when they plot scatter charts and draw a line of best fit by hand; analysts use the same closed form for trend lines, calibration curves, and quick forecasts without spinning up a full statistics package.
How ordinary least-squares linear regression works
Begin with at least two paired observations of a predictor x and a response y. Ordinary least squares chooses the unique line y equals m x plus b that minimizes the sum of squared vertical residuals between each observed y and the value predicted on the line.
Algebraically that minimum is closed form: compute the sample means of x and of y, form the centered sums Sxx equals the sum of squared deviations of x from its mean and Sxy equals the sum of products of centered x and centered y, then set the slope m equal to Sxy divided by Sxx and the intercept b equal to mean y minus m times mean x.
Predicted values are m times each x plus b; residual sum of squares is the sum of squared differences between y and those predictions; total sum of squares is the sum of squared deviations of y from its own mean; R-squared is one minus residual over total. When every y value is identical and the horizontal line fits exactly, R-squared is reported as one. Non-finite numbers, empty series, a single point, and mismatched x and y lengths are likewise rejected before any fit is attempted.
What you can do with it
Homework and exam answer keys
Generate slope, intercept, R-squared, and residual tables for textbook paired data so teachers and auto-graders share one ground truth.
Lab calibration curves
Fit instrument reading versus concentration with ordinary least squares and export the equation for later prediction steps.
Simple trend lines in ops and product
Fit a straight trend to weekly volume or latency series and report m, b, and R-squared without a full analytics stack.
FAQ
What happens if all x values are the same?
Zero variance in x makes Sxx zero, so the least-squares slope is undefined. Add spread in x or use a different model.
How many points do I need?
At least two finite pairs. One point cannot determine a unique line under least squares; two or more are required.
What does R-squared mean here?
R-squared is the fraction of variance in y explained by the fitted line. It equals one for a perfect fit and approaches zero when the line explains nothing about y.
What does it cost?
The browser tool is free and runs locally.
What do the slope and the intercept tell me?
The slope is the expected change in y for each one-unit rise in x, which is 0.6 in the example. The intercept is the fitted value of y when x is zero, here 2.2. Together they give the line used for predictions.
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/linear-regression \
-H "Authorization: Bearer $KIT_KEY" \
-H "Content-Type: application/json" \
-d '{"x":"1, 2, 3, 4, 5","y":"2, 4, 5, 4, 5"}'const res = await fetch("https://api.kit.forhosting.com/stat/linear-regression", {
method: "POST",
headers: {
"Authorization": `Bearer ${process.env.KIT_KEY}`,
"Content-Type": "application/json"
},
body: JSON.stringify({
"x": "1, 2, 3, 4, 5",
"y": "2, 4, 5, 4, 5"
})
});
const { task_id } = await res.json();import os, requests
res = requests.post(
"https://api.kit.forhosting.com/stat/linear-regression",
headers={"Authorization": f"Bearer {os.environ['KIT_KEY']}"},
json={
"x": "1, 2, 3, 4, 5",
"y": "2, 4, 5, 4, 5"
},
)
task_id = res.json()["task_id"]<?php
$res = file_get_contents("https://api.kit.forhosting.com/stat/linear-regression", false, stream_context_create([
"http" => [
"method" => "POST",
"header" => "Authorization: Bearer " . getenv("KIT_KEY") . "\r\nContent-Type: application/json",
"content" => '{"x":"1, 2, 3, 4, 5","y":"2, 4, 5, 4, 5"}',
],
]));
$task = json_decode($res, true);body := bytes.NewBufferString(`{"x":"1, 2, 3, 4, 5","y":"2, 4, 5, 4, 5"}`)
req, _ := http.NewRequest("POST", "https://api.kit.forhosting.com/stat/linear-regression", body)
req.Header.Set("Authorization", "Bearer "+os.Getenv("KIT_KEY"))
req.Header.Set("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)Example request
{
"x": "1, 2, 3, 4, 5",
"y": "2, 4, 5, 4, 5"
}Example response
{
"task_id": "tsk_a1b2c3d4e5f6a1b2c3d4e5f6",
"type": "stat.linear_regression",
"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
This calculator accepts paired points as parallel x and y lists or as a JSON array of pairs, rejects degenerate series where every x value is the same because the slope would be undefined, and returns cleaned series, means, slope, intercept, R-squared, correlation, residual diagnostics, and a printable equation string.
How ordinary least-squares linear regression works
The method is deterministic floating-point arithmetic with no random seeds, no wall-clock timestamps, and no network calls, so identical inputs always produce identical JSON. If Sxx is zero because every x value is the same, the denominator vanishes and no finite slope exists, so the request is rejected with invalid input rather than returning Infinity or NaN.
What you send and what you get back
Send paired data in one of two shapes. Do not mix points with separate x and y in the same request. At least two pairs are required. Classic textbook checks include x equals one through five with y equals two, four, five, four, five, which yields slope zero point six, intercept two point two, and R-squared zero point six; and the perfect line through zero one, one three, two five, which yields slope two, intercept one, and R-squared one. Either provide both x and y as JSON arrays of finite numbers or as comma, space, or semicolon separated strings of equal length, or provide a single points field as a JSON array of two-element [x, y] pairs or objects with numeric x and y fields. Missing fields, empty lists, non-numeric tokens, NaN, Infinity, length mismatch, and constant x all fail with invalid_input and are not charged. On success the response includes n, cleaned x and y arrays, mean_x and mean_y, slope, intercept, r_squared, Pearson correlation, ss_res and ss_tot, sxx and sxy, predicted values, residuals, a printable equation string such as y equals m times x plus b, the method label ordinary_least_squares, and a short formula reminder. Pricing for successful API calls is the published base of $0.002 per item; validation failures do not consume prepaid balance.
Where linear regression calculators show up in real work
Laboratory calibration maps instrument reading x to concentration y and needs the OLS intercept and slope for later prediction with the same numbers every lab section sees. Business and operations teams draw simple trend lines on weekly volume, revenue, or latency series without opening a full notebook stack, then export the equation string into reports. If you later need multiple regression, polynomial fits, logistic models, correlation-only summaries, five-number summaries, or z-scores, treat those as separate search intents and landings rather than overloading this linear regression calculator page. Secondary-school and college statistics courses constantly assign scatterplots, line-of-best-fit drills, and interpret-the-slope word problems; a single trusted oracle that returns slope, intercept, and R-squared as clean JSON speeds grading and demo setup. Because this capability is a pure parse engine, API pricing is the published base of $0.002 per successful item and failed validation does not consume balance.
Technical questions
What method does this linear regression calculator use?
Ordinary least squares (OLS): m = Sxy / Sxx and b = mean_y − m · mean_x, with R² = 1 − SS_res / SS_tot. No iterative solvers and no machine-learning libraries.