Home › Statistics & Probability › Linear Regression Calculator
Linear Regression Calculator
Fits an ordinary least-squares line to paired (x, y) data and returns the slope, intercept, equation, Pearson r, R², the standard error of the estimate and an optional prediction for a new x.
When to use
You want the best-fit straight line through paired data, its equation, or a predicted y for a given x (trend lines, calibration curves, simple forecasting).
Do not use when: The relationship is curved or has several predictors (needs polynomial or multiple regression), or you only need the strength of association (use correlation).
Formula
slope = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)²; intercept = ȳ − slope · x̄; r = Σ(x − x̄)(y − ȳ) / √(Σ(x − x̄)² Σ(y − ȳ)²); ŷ(x_new) = intercept + slope · x_new
Ordinary least squares minimises the sum of squared vertical residuals; it assumes x is measured without error and residuals have constant variance. Predictions outside the observed x range are extrapolations.
Inputs
| Parameter | Type | Unit | Required | Description |
|---|---|---|---|---|
x_values | number_list | yes | Independent (explanatory) variable, one number per observation. | |
y_values | number_list | yes | Dependent (response) variable, in the same order as x_values. | |
x_new | number | no | Optional x value at which to evaluate the fitted line. |
Outputs
| Output | Type | Unit | Description |
|---|---|---|---|
slope | number | Change in y per unit change in x. | |
intercept | number | Fitted y at x = 0. | |
equation | string | Fitted line as y = bx + a with coefficients rounded to 4 decimals. | |
pearson_r | number | Correlation coefficient of x and y (omitted when y is constant). | |
r_squared | number | Fraction of the variance in y explained by the line (omitted when y is constant). | |
standard_error_of_estimate | number | √(Σ(y − ŷ)² / (n − 2)): typical vertical distance of the points from the line (needs n ≥ 3). | |
sample_size | integer | Number of (x, y) pairs used. | |
predicted_y | number | intercept + slope × x_new (only when x_new is given). |
Example
x = 1..5, y = 2, 4, 5, 4, 5, predict x = 6: {"x_values":[1,2,3,4,5],"y_values":[2,4,5,4,5],"x_new":6} → {"slope":0.6,"intercept":2.2,"equation":"y = 0.6x + 2.2","pearson_r":0.7746,"r_squared":0.6,"standard_error_of_estimate":0.8944,"sample_size":5,"predicted_y":5.8}
Exact line y = 2x + 1: {"x_values":[0,1,2,3],"y_values":[1,3,5,7]} → {"slope":2,"intercept":1,"equation":"y = 2x + 1","pearson_r":1,"r_squared":1,"standard_error_of_estimate":0}
GET https://tttkmbb.com/api/v1/calculate/linear-regression?x_values=1%2C2%2C3%2C4%2C5&y_values=2%2C4%2C5%2C4%2C5&x_new=6
Machine access
- API:
GET https://tttkmbb.com/api/v1/calculate/linear-regression(query parameters) orPOSTwith a JSON body{"inputs": {...}} - Schema: https://tttkmbb.com/api/v1/calculators/linear-regression · Markdown: https://tttkmbb.com/statistics/linear-regression.md · JSON definition: https://tttkmbb.com/statistics/linear-regression.json
- MCP: server
https://tttkmbb.com/mcp, toolrun_calculator with calculator_id="linear-regression" - OpenAPI operationId:
calculate_linear_regression - Freshness:
static. Authentication: none. Rate limit: fair use (see rate limits).
Sources
FAQ
How good is the fit?
R² near 1 means the line explains most of the variation in y; the standard error of the estimate gives the typical residual in y units. Always inspect the residuals for curvature or outliers as well.
Which variable goes on x?
x is the predictor (the variable you control or know first) and y the response; swapping them gives a different line because least squares minimises vertical distances only.
Related calculators
- Correlation Coefficient Calculator — Only the strength of the linear association.
- Slope Calculator — Slope of a line through two known points.