Linear Regression Calculator

Predict one variable from another. Upload paired data, get the slope, intercept, and a visual test of whether the relationship is real.

Enter your data

Results

Slope (b₁)
Intercept (b₀)
P (slope)
Enter data with at least 3 paired values to begin.

Predict y for a new x

Scatter plot of data with fitted regression line
Fig. 1 — Scatter plot with fitted regression line (blue). Each point is an observation; the line shows the predicted y for each x.
Residual plot: actual minus predicted y, against x
Fig. 2 — Residual plot. Each point shows the vertical distance (actual − predicted). A good regression spreads residuals evenly around the zero line with no pattern.
t-distribution curve showing significance test for slope
Fig. 3 — t-distribution for the slope. The curve shows the probability distribution of the slope under the null hypothesis (no relationship). Your observed slope is marked in red; the shaded tails show the rejection region at α = 0.05 (two-tailed).

§1 The formula

b₁ = Sxy / Sxx
b₀ = ȳ − b₁ x̄
b₁
Slope of the regression line
b₀
Intercept (y-value when x = 0)
Sxy
Sum of the products of deviations: Σ(xᵢ − x̄)(yᵢ − ȳ)
Sxx
Sum of squared x-deviations: Σ(xᵢ − x̄)²
x̄, ȳ
Mean of x and y

From the slope and intercept, the fitted line is ŷ = b₀ + b₁x. For each observation, the residual is eᵢ = yᵢ − ŷᵢ (actual minus predicted). The standard error of the slope is SE(b₁) = σ̂ / √Sxx, where σ̂ is the residual standard deviation. The t-statistic is t = b₁ / SE(b₁), which has n − 2 degrees of freedom.

§2 Worked example

Say we have six observations:

x 1 2 3 4 5 6
y 2 4 5 4 6 7

Working by hand:

  1. x̄ = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 3.5 Average of x values.
  2. ȳ = (2 + 4 + 5 + 4 + 6 + 7) / 6 ≈ 4.667 Average of y values.
  3. Sxx = (1−3.5)² + (2−3.5)² + ... + (6−3.5)² = 17.5 Sum of squared x-deviations.
  4. Sxy = (1−3.5)(2−4.667) + ... + (6−3.5)(7−4.667) ≈ 14.833 Sum of products of deviations.
  5. b₁ = 14.833 / 17.5 ≈ 0.847 Slope.
  6. b₀ = 4.667 − 0.847 × 3.5 ≈ 1.706 Intercept.
  7. r² ≈ 0.825 Strength of the linear relationship (82.5% of y-variation explained).
  8. SE(b₁) ≈ 0.254 Standard error of the slope, df = 4.
  9. t ≈ 0.847 / 0.254 ≈ 3.33 t-statistic for slope.
  10. p ≈ 0.032 Two-tailed p-value. Since p < 0.05, we reject H₀ and conclude the slope is significantly different from zero at α = 0.05.

§3 Assumptions & when this test is wrong

Assumptions of simple linear regression

  • Linearity. The relationship between x and y is approximately a straight line. If the true relationship is curved, the fitted line will be misleading.
  • Homoscedasticity. The vertical spread of points around the line is roughly the same across all x-values. Check this by looking at the residual plot (Fig. 2): if the spread widens or narrows, homoscedasticity is violated.
  • Independence. Each observation is independent; there is no autocorrelation (e.g., time-series data often violates this).
  • Approximate normality of residuals. The residuals should be approximately normally distributed. Mild departures do not usually matter for the significance test, but large departures (e.g., heavy tails) can affect p-values.

Scope and extrapolation

This calculator performs simple (one-predictor) linear regression. For multiple predictors, use multiple regression. The fitted line is reliable only in the observed range of x; predictions far outside that range (extrapolation) are unreliable and often misleading.

§4 FAQ

What is the difference between correlation and regression?

Correlation describes how strongly two variables are related (a symmetric measure); regression models how to predict one from the other (asymmetric). Correlation asks, "Are they related?" Regression asks, "If I know x, what is y?" The correlation calculator shows the correlation coefficient r and R²; this page focuses on prediction and inference about the slope.

What does R² mean?

R² (coefficient of determination) is the fraction of y-variance explained by x. It ranges from 0 to 1. For example, R² = 0.70 means the linear model explains 70% of the variation in y; the other 30% is due to other factors. R² = r², where r is Pearson correlation.

What does the p-value for the slope mean?

The p-value is the probability of observing a slope this extreme or more extreme if the true slope were zero (no relationship). A small p-value (e.g., p = 0.03) suggests the slope is unlikely to be zero, so we have evidence of a real relationship. The p-value does not tell you the size or importance of the effect, only whether it is discernible from noise.

How do I check if my regression is valid?

Look at the residual plot (Fig. 2). Points should scatter evenly around the zero line with no obvious pattern (funnel shape, curve, or trend). If residuals fan out or show a curved pattern, one of the assumptions is violated. Consider transforming x or y (e.g., log or square root), or fitting a polynomial.

What if I have fewer than 3 data points?

With fewer than 3 points, you cannot estimate the residual standard error (degrees of freedom df = n − 2). The minimum is n = 3 (df = 1). In practice, many more points are needed for stable inference.

§5 Sources


Related tools: Correlation Coefficient Calculator · Effect Size Calculator