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
Predict y for a new x
§1 The formula
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:
x̄ = (1 + 2 + 3 + 4 + 5 + 6) / 6 = 3.5Average of x values.ȳ = (2 + 4 + 5 + 4 + 6 + 7) / 6 ≈ 4.667Average of y values.Sxx = (1−3.5)² + (2−3.5)² + ... + (6−3.5)² = 17.5Sum of squared x-deviations.Sxy = (1−3.5)(2−4.667) + ... + (6−3.5)(7−4.667) ≈ 14.833Sum of products of deviations.b₁ = 14.833 / 17.5 ≈ 0.847Slope.b₀ = 4.667 − 0.847 × 3.5 ≈ 1.706Intercept.r² ≈ 0.825Strength of the linear relationship (82.5% of y-variation explained).SE(b₁) ≈ 0.254Standard error of the slope, df = 4.t ≈ 0.847 / 0.254 ≈ 3.33t-statistic for slope.p ≈ 0.032Two-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
- OpenStax. Introductory Statistics 2e. Chapter 12: Linear Regression and Correlation
- NIST/SEMATECH. e-Handbook of Statistical Methods. 1.3.5.10: Simple Linear Regression Prediction and Estimation
- National Center for Biotechnology Information. Regression Modeling for Categorical Outcomes (covers diagnostics)
Related tools: Correlation Coefficient Calculator · Effect Size Calculator