# 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.

- Calculator id: `linear-regression` · Category: Statistics & Probability (`statistics`) · Tool name: `calculate_linear_regression`
- Canonical page: https://tttkmbb.com/statistics/linear-regression · This document: https://tttkmbb.com/statistics/linear-regression.md · JSON definition: https://tttkmbb.com/statistics/linear-regression.json

## Purpose

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.

**Use when:** 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).

## Input

| Parameter | Type | Unit | Required | Description |
| --- | --- | --- | --- | --- |
| `x_values` | number_list |  | required | Independent (explanatory) variable, one number per observation. |
| `y_values` | number_list |  | required | Dependent (response) variable, in the same order as x_values. |
| `x_new` | number |  | optional | Optional x value at which to evaluate the fitted line. |

## Output

| Field | 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). |

## 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.

## Data Sources

- NIST/SEMATECH e-Handbook of Statistical Methods, 4.1.4.1 Linear Least Squares Regression — https://www.itl.nist.gov/div898/handbook/pmd/section1/pmd141.htm (government, retrieved 2026-09-23)
- Wikipedia – Simple linear regression — https://en.wikipedia.org/wiki/Simple_linear_regression (reference, retrieved 2026-09-23)

Data freshness: `static`. Deterministic formula with fixed constants; results never go stale. Inputs supplied by the caller determine the output.

## API

- `GET https://tttkmbb.com/api/v1/calculate/linear-regression?x_values=…&y_values=…`
- `POST https://tttkmbb.com/api/v1/calculate/linear-regression` with JSON body `{"inputs": {…}}`
- Response: unified envelope (`success`, `request`, `result.values`, `result.units`, `sources`, `freshness`, `timestamp`, `next_actions`, `links`); see https://tttkmbb.com/docs/response-format.md
- Schema: https://tttkmbb.com/api/v1/calculators/linear-regression · OpenAPI operationId `calculate_linear_regression` in https://tttkmbb.com/openapi.json
- Authentication: none. Rate limit: fair use, see https://tttkmbb.com/docs/rate-limits.md.

## MCP

- Server: `https://tttkmbb.com/mcp` (Streamable HTTP, JSON-RPC 2.0, no auth)
- Tool:  `run_calculator` with `{"calculator_id": "linear-regression", "inputs": {…}}`

## Example

- x = 1..5, y = 2, 4, 5, 4, 5, predict x = 6: inputs `{"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: inputs `{"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
```

## Limitations

The relationship is curved or has several predictors (needs polynomial or multiple regression), or you only need the strength of association (use correlation). 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. All values are computed from the formula above; no measurement or live data is involved.

## 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

- [Correlation Coefficient Calculator](https://tttkmbb.com/statistics/correlation.md) — Only the strength of the linear association.
- [Slope Calculator](https://tttkmbb.com/math/slope.md) — Slope of a line through two known points.
