Linear Regression Calculator

JJ Ben-Joseph headshot JJ Ben-Joseph

What this linear regression calculator does

This linear regression calculator fits a least-squares straight line to paired X and Y observations. It reports the slope, intercept, and R-squared so you can describe the direction, size, and closeness of a linear fit.

Supply two equally long lists: one for X and one for Y. The calculator estimates the line Y = slope × X + intercept that minimizes the squared vertical differences between the observed Y values and the line's predicted values.

How to enter paired data for linear regression

For a valid linear regression, enter X and Y as matching lists: each position represents one observed pair.

  • Equal length: The X and Y lists need the same number of observations.
  • Separators: Separate values with commas or line breaks.
  • Numbers only: Remove headers, units, and other text; entries such as 3.5, -2, and 10 are suitable.
  • Pair order matters: The first X value is matched with the first Y value, the second with the second, and so forth.
  • Review unusual points: An extreme X or Y observation can have a large effect on a least-squares line.

When copying columns from a spreadsheet, paste one complete column into each box and preserve row order. A missing row in either list changes every pairing below it and makes the regression describe the wrong observations.

Example dataset for a simple linear regression

This linear regression example pairs study time in hours with test scores in points:

X (hours studied):

1
2
3
4
5
6

Y (test score):

52
57
63
68
74
79

Entering these pairs produces a positive slope: higher study-time values are associated with higher scores in this small dataset. Its R-squared is close to 1 because the points lie very near a straight increasing trend; that describes fit in these observations, not proof that study time alone causes the score change.

Formulas used by the linear regression calculator

This simple linear regression calculator uses ordinary least squares with one predictor, X, and one outcome, Y.

For n paired observations (x1, y1), (x2, y2), ..., (xn, yn), it first finds the mean of each variable:

μX=xin, μY=yin

In these regression formulas, means that the indicated quantity is added across all paired observations. The fitted line's slope b1 and intercept b0 are:

Formula: b_1 = (∑(x_i − μ_X)(y_i − μ_Y)) / (∑ (x_i−μ_X)^2)

b1=(xiμX)(yiμY)(xiμX)2

Formula: b_0 = μ_Y − b_1 μ_X

b0=μYb1μX

The regression equation returned by the calculator is:

Formula: y^= b_0 + b_1 x

y^=b0+b1x

For a selected X value, y^ is the Y value predicted by that fitted line.

Correlation and R-squared in the fitted line

For simple linear regression with an intercept, the calculator's R-squared is the square of the Pearson correlation coefficient r: R2=r2.

Formula: r = (∑ (x_i − μ_X) (y_i − μ_Y)) / (sqrt(∑ (x_i−μ_X)^2) sqrt(∑ (y_i−μ_Y)^2))

r=(xiμX)(yiμY)(xiμX)2(yiμY)2

R-squared, the coefficient of determination, is:

Formula: R^2 = r^2

R2=r2

In this one-predictor model, R-squared is the proportion of observed variation in Y accounted for by the fitted linear relationship with X.

Linear regression symbols in plain language

  • xi, yi: the i-th observed X and Y pair.
  • μX, μY: the average X value and average Y value.
  • b1: the estimated Y change for a one-unit increase in X.
  • b0: the predicted Y value at X = 0.
  • r: the direction and strength of linear association between X and Y.
  • R2: the share of Y variation described by the fitted line.

Interpreting linear regression results

Linear regression results are most useful when read in the units and data range used to fit the line.

Interpreting the regression slope

The regression slope estimates how much Y changes, on average, when X rises by one unit.

  • Positive slope: Larger X values tend to occur with larger Y values, such as more study hours and higher test scores.
  • Negative slope: Larger X values tend to occur with smaller Y values, such as greater router distance and lower Wi-Fi speed.
  • Slope near zero: The fitted straight line changes little as X changes, indicating little linear association in the data.

Keep the original units in view. If X is measured in hours and Y in dollars, a slope of 15 means the line associates one additional hour with approximately $15 more in Y.

Interpreting the regression intercept

The intercept is the line's predicted Y when X equals zero. It can be substantively useful when zero lies within a meaningful range of X values. When zero is far outside the observed range, the intercept still positions the fitted line mathematically but should not be treated as a credible real-world prediction.

Understanding regression R-squared

For this simple linear regression, R-squared runs from 0 to 1:

  • R-squared close to 1: The observed points are tightly clustered around the fitted line.
  • R-squared around 0.5: The line captures part of the Y variation, while substantial scatter remains.
  • R-squared near 0: A straight line using X accounts for very little of the variation in Y.

A strong fitted line does not demonstrate that X causes Y. Conversely, a modest R-squared can still be useful in settings where substantial natural variation is expected.

Worked linear regression example step by step

Using the study-time and score pairs above, the calculator follows the same least-squares process:

X (hours): 1, 2, 3, 4, 5, 6

Y (score): 52, 57, 63, 68, 74, 79

  1. Calculate each variable's mean.
    • Mean of X: (1 + 2 + 3 + 4 + 5 + 6) / 6 = 3.5
    • Mean of Y: (52 + 57 + 63 + 68 + 74 + 79) / 6 = 65.5
  2. For every pair, find its deviations from those means and add the products (xiμX)(yiμY).
  3. Add the squared X deviations, (xiμX)2.
  4. Divide the first sum by the second to obtain the slope:
    • slope b1=(xiμX)(yiμY)(xiμX)2
  5. Use the slope and means to calculate the intercept:
    • intercept b0=μYb1μX

For these six pairs, the fitted equation is approximately:

Predicted score = 46.4 + 5.4571 × hours studied

Within this dataset, the line associates one additional hour of study with about 5.46 additional score points. The intercept is the line's prediction at zero study hours; because zero was not observed here, it is an extrapolated mathematical anchor rather than a measured baseline.

Comparing key linear regression outputs

Output What it represents How to interpret it
Slope (b1) Estimated Y change for a one-unit increase in X The sign gives direction; the size is expressed in Y units per X unit
Intercept (b0) Line's predicted Y at X = 0 Most meaningful when zero is a realistic X value near the observed data
R-squared (R2) Proportion of Y variation explained by the fitted straight line Near 1 indicates a close linear fit; near 0 indicates little linear explanatory power

Assumptions and limitations of simple linear regression

This calculator fits a simple least-squares regression line, so its output should be considered alongside the pattern and context of the paired data.

  • Linearity: A straight line should be a reasonable summary of how X and Y relate. Curves, thresholds, and plateaus may not be represented well.
  • Independent errors: Residuals should not systematically depend on neighboring observations. Time series and clustered observations can violate this condition.
  • Constant variance: The residual spread should be broadly similar across X values. A widening or narrowing pattern can make standard regression inferences unreliable.
  • Normal residuals for inference: Approximate residual normality is commonly needed for formal tests and confidence intervals, though it is less central to simply fitting a line.
  • Outlier sensitivity: A small number of extreme observations can pull the slope and intercept substantially. Check whether unusual values are mistakes, exceptional cases, or valid data.
  • Association is not causation: A regression slope alone cannot rule out confounding variables, reverse causation, or coincidence.
  • One predictor only: This page fits one X variable to one Y variable. Questions involving several predictors need a multiple-regression or other appropriate model.

Use the slope, intercept, and R-squared as an exploratory summary of your pairs. Important decisions generally require subject-matter knowledge, data-quality checks, and diagnostics beyond a single fitted line.

Provide equal-length datasets to see the best fit line.

Residual Run Mini-Game

Click to guide your trend line—catch drifting points before noise pulls them away.

Score0
Best0
Time90s
Stability
Controls: ←/→ slope, ↑/↓ intercept, space to pause, tap/drag to nudge line.
Stay close to the true regression—tight residuals earn higher streaks.
Aim for minimal error. Highlighted captures mirror low residuals in real fits.

Tip: After you calculate, the game tunes targets to your latest slope and intercept.