Linear Regression of y on x: Explained Simply
The calculator for this page is switched off while it is checked. The formula is below, and the worked example in the article shows how to use it by hand.
Linear regression of y on x finds the straight line that best predicts y from x, giving you an equation like y = a + bx. You enter paired data for x and y, and the calculator returns the slope (b), intercept (a), and usually a goodness-of-fit measure like R². This article explains the math, how to interpret the results, and common pitfalls.
Formula
The least-squares regression line is written as:
y = a + b·x
where:
- y is the dependent variable (what you predict).
- x is the independent variable (the predictor).
- b (slope) = [n·Σ(xy) − Σx·Σy] / [n·Σ(x²) − (Σx)²]
- a (intercept) = (Σy − b·Σx) / n
- n is the number of data pairs.
All sums are over the n data points. The slope tells you the average change in y for a one-unit increase in x. The intercept is the predicted y when x = 0.
Worked Example
Using the default data: x = 1, 2, 3, 4, 5 and y = 2, 4, 5, 4, 5.
- Compute sums: Σx = 15, Σy = 20, Σxy = (1·2)+(2·4)+(3·5)+(4·4)+(5·5) = 2+8+15+16+25 = 66, Σx² = 1+4+9+16+25 = 55, Σy² = 4+16+25+16+25 = 86.
- Slope b = (5·66 − 15·20) / (5·55 − 15²) = (330 − 300) / (275 − 225) = 30/50 = 0.6.
- Intercept a = (20 − 0.6·15) / 5 = (20 − 9) / 5 = 11/5 = 2.2.
- R² = [ (5·66 − 15·20) / sqrt((5·55 − 225)·(5·86 − 400)) ]² = [30 / sqrt(50·30)]² = [30 / sqrt(1500)]² = (30/38.7298)² ≈ 0.6.
The regression line is y = 2.2 + 0.6x. For x = 3, predicted y = 2.2 + 1.8 = 4.0, which matches the actual value.
When to Use Regression of y on x
Use this when you have paired numeric data and want to quantify how y changes with x, or to predict y for a new x. It assumes:
- A roughly linear relationship (check a scatterplot).
- Independence of observations.
- Homoscedasticity (constant variance of errors).
- Normally distributed errors for confidence intervals (less critical for point predictions).
It is not appropriate for data that shows a clear curve or when outliers heavily influence the line.
Interpreting the Results
The slope b is the key number. If b = 0.6, then for every 1-unit increase in x, y increases by 0.6 units on average. The intercept a is the predicted y when x = 0; sometimes that has no practical meaning (e.g., predicting weight from height at height 0).
R² (coefficient of determination) tells you the proportion of variance in y explained by x. An R² of 0.6 means 60% of the variation in y is explained by the linear relationship with x; the other 40% is due to other factors or random noise.
Always look at a scatterplot. A high R² with a curved pattern suggests a non-linear model is better.
Common Mistakes and Pitfalls
- Confusing x and y: Regression of y on x is not symmetric. If you swap x and y, you get a different line (unless R² = 1).
- Extrapolating beyond the data range: Predictions outside the observed x range are risky because the relationship may change.
- Ignoring outliers: A single extreme point can tilt the line dramatically. Check residuals.
- Assuming causation: Correlation does not imply causation. A strong regression does not prove x causes y.
- Rounding too early: Keep full precision in intermediate steps; round only the final answer.
Limitations and Alternatives
Simple linear regression only models one predictor. If you have multiple predictors, use multiple regression. If the relationship is curved, try polynomial regression or transform the data (e.g., log y). For data that violates normality, consider robust regression or non-parametric methods like Theil-Sen.
Also note that regression is sensitive to the scale of x. Changing units (e.g., from meters to centimeters) changes the slope but not the fit or R².
Frequently Asked Questions
What is the difference between regression and correlation?
Correlation (r) measures the strength and direction of a linear relationship, with no notion of prediction. Regression gives an equation to predict y from x and quantifies the average change. R² is the square of the correlation coefficient.
Can I use regression with non-numeric data?
No, both x and y must be numeric. If you have categorical predictors, use dummy variables or ANOVA. If y is categorical (e.g., yes/no), logistic regression is appropriate.
Why does my calculator give a different slope than Excel?
Most software uses the same least-squares method, but differences can arise from rounding, missing data handling, or if you accidentally swapped x and y. Check your data entry and the formula used.
How many data points do I need?
Technically, you need at least 2 points to draw a line, but for meaningful inference you typically want at least 10-20 pairs. More data gives more stable estimates of slope and intercept.
What does a negative slope mean?
A negative slope indicates that as x increases, y tends to decrease. For example, if x is age of a car and y is its price, a negative slope means older cars cost less on average.