HomeStatistics & 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

ParameterTypeUnitRequiredDescription
x_valuesnumber_listyesIndependent (explanatory) variable, one number per observation.
y_valuesnumber_listyesDependent (response) variable, in the same order as x_values.
x_newnumbernoOptional x value at which to evaluate the fitted line.

Outputs

OutputTypeUnitDescription
slopenumberChange in y per unit change in x.
interceptnumberFitted y at x = 0.
equationstringFitted line as y = bx + a with coefficients rounded to 4 decimals.
pearson_rnumberCorrelation coefficient of x and y (omitted when y is constant).
r_squarednumberFraction of the variance in y explained by the line (omitted when y is constant).
standard_error_of_estimatenumber√(Σ(y − ŷ)² / (n − 2)): typical vertical distance of the points from the line (needs n ≥ 3).
sample_sizeintegerNumber of (x, y) pairs used.
predicted_ynumberintercept + 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

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