Linear Regression Equation Calculator
Linear regression is the most widely used statistical technique in science, engineering, economics, finance, medicine, and machine learning. It finds the best-fit straight line — described by the equation y = mx + b — through a set of data points, letting you understand the relationship between two variables and predict future values. This free calculator uses the least squares method to compute the slope (m), y-intercept (b), Pearson correlation coefficient (r), and R² from your data, with a complete step-by-step solution.
Slope: m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²)
Intercept: b = (Σy − m·Σx) / n = ȳ − m·x̄
Correl.: r = (n·Σxy − Σx·Σy) / √[(n·Σx² − (Σx)²)(n·Σy² − (Σy)²)]
R²: r² (proportion of variance explained)
What Is a Linear Regression Equation?
A linear regression equation is a mathematical formula that describes the best-fit straight line through a scatter plot of data. It expresses the relationship between an independent variable x (the predictor or explanatory variable) and a dependent variable y (the response or outcome variable). The equation takes the form:
where: m = slope (rate of change), b = y-intercept, ŷ = predicted y value
The "best-fit" line is the one that minimises the sum of squared residuals — the squared vertical distances between each observed data point and the line. This optimisation criterion is called the least squares method and is the foundation of simple linear regression.
- Linear regression models a linear (straight-line) relationship between two variables
- It is the most commonly used statistical model in academic research, data science, and business analytics
- Taught in AP Statistics, A-Level Statistics, IB Maths AA/AI, and every college statistics course worldwide
- Used daily in finance (stock trend analysis), medicine (dose-response), engineering (load testing), and climate science
- The output is both a predictive equation and a measure of relationship strength (r and R²)
The Linear Regression Formula Explained
The formulas for slope and y-intercept come directly from minimising the sum of squared residuals using calculus. You only need five summations from your data:
Σx = sum of all x values
Σy = sum of all y values
Σxy = sum of each (x × y) product
Σx² = sum of each x squared
Σy² = sum of each y squared (needed for r only)
Slope:
m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²)
Y-intercept:
b = (Σy − m·Σx) / n
Pearson correlation coefficient:
r = (n·Σxy − Σx·Σy) / √[(n·Σx² − (Σx)²)(n·Σy² − (Σy)²)]
Coefficient of determination:
R² = r²
An equivalent formula for slope uses the means of x and y: m = Σ[(xᵢ − x̄)(yᵢ − ȳ)] / Σ[(xᵢ − x̄)²]. Both formulas give identical results. The calculator uses the numerically more stable Σ-form shown above.
How to Calculate Linear Regression Step by Step
- List your data pairs (x₁, y₁), (x₂, y₂), …, (xₙ, yₙ).
- Count n — the total number of data pairs.
- Calculate the five sums: Σx, Σy, Σxy, Σx², Σy².
- Compute the slope m: m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²)
- Compute the y-intercept b: b = (Σy − m·Σx) / n
- Write the equation: ŷ = mx + b
- Compute r (optional but recommended): r = (n·Σxy − Σx·Σy) / √[(n·Σx² − (Σx)²)(n·Σy² − (Σy)²)]
- Compute R² = r² to interpret how well the line fits the data.
- Verify by substituting a known x value and checking that the predicted ŷ is reasonable.
Understanding the Slope (m)
The slope m tells you how much y changes for every 1-unit increase in x. It is the "rate of change" of the regression line.
- Positive slope (m > 0): As x increases, y tends to increase. Example: as study hours increase, exam scores increase.
- Negative slope (m < 0): As x increases, y tends to decrease. Example: as temperature increases, heating costs decrease.
- Zero slope (m = 0): No linear relationship — y is not predictably affected by x. The regression line is horizontal.
- Units of m: The slope is expressed in units of y per unit of x. If x is hours and y is dollars, m is in $/hour.
→ For every $1 increase in advertising spend, sales increase by $2.50 on average.
If m = −0.8 in a model of price (x) vs demand (y):
→ For every $1 increase in price, demand decreases by 0.8 units on average.
Understanding the Y-Intercept (b)
The y-intercept b is the predicted value of y when x = 0. It is where the regression line crosses the y-axis.
Mathematically, the regression line always passes through the point of means (x̄, ȳ) — the average of all x values and the average of all y values. You can use this to double-check your calculation: substitute x̄ into your equation and verify you get ȳ.
Correlation Coefficient r — Measuring Relationship Strength
The Pearson correlation coefficient r measures the strength and direction of the linear relationship between x and y. It always falls between −1 and +1:
| |r| Range | Interpretation | Example Use |
|---|---|---|
| 1.0 (exact) | Perfect linear relationship | Mathematical identity (e.g., y = 2x exactly) |
| 0.90 – 0.99 | Very strong | Height vs arm span in adults |
| 0.70 – 0.89 | Strong | Study hours vs exam score |
| 0.50 – 0.69 | Moderate | Income vs spending |
| 0.30 – 0.49 | Weak | Age vs blood pressure |
| 0.10 – 0.29 | Very weak | Shoe size vs IQ |
| 0.00 – 0.09 | No linear relationship | Eye colour vs salary |
R² — Coefficient of Determination
R² (R-squared), the coefficient of determination, equals r² and tells you the proportion of variance in y that is explained by the linear regression with x.
Interpretation: R² × 100% of the variation in y is explained by x via the regression line.
Remaining (1 − R²) × 100% is unexplained (residual variance).
r = 0.95 → R² = 0.9025 → 90.25% of y variance explainedr = 0.80 → R² = 0.64 → 64% of y variance explainedr = 0.50 → R² = 0.25 → 25% of y variance explainedr = 0.30 → R² = 0.09 → only 9% explained — model is weakA model with R² = 0.90 leaves only 10% of the variation unexplained — generally excellent for real-world data.
Full Worked Example — Step by Step
Find the linear regression equation for the following dataset (study hours x vs exam score y):
| x (hours) | y (score) | xy | x² | y² |
|---|---|---|---|---|
| 1 | 52 | 52 | 1 | 2,704 |
| 2 | 58 | 116 | 4 | 3,364 |
| 3 | 64 | 192 | 9 | 4,096 |
| 4 | 69 | 276 | 16 | 4,761 |
| 5 | 73 | 365 | 25 | 5,329 |
| 6 | 80 | 480 | 36 | 6,400 |
| Σ = 21 | Σ = 396 | Σ = 1,481 | Σ = 91 | Σ = 26,654 |
Step 1 — Calculate slope m:
Numerator: n·Σxy − Σx·Σy = 6(1,481) − (21)(396) = 8,886 − 8,316 = 570
Denominator: n·Σx² − (Σx)² = 6(91) − (21)² = 546 − 441 = 105
m = 570 / 105 = 5.4286
Step 2 — Calculate y-intercept b:
b = (Σy − m·Σx) / n = (396 − 5.4286 × 21) / 6 = (396 − 113.999) / 6 = 282.001 / 6 = 47.000
Step 3 — Write the equation:
ŷ = 5.43x + 47.00Step 4 — Correlation coefficient r:
r = 570 / √(105 × (6 × 26,654 − 396²))
= 570 / √(105 × (159,924 − 156,816))
= 570 / √(105 × 3,108) = 570 / √326,340 = 570 / 571.26 = 0.9978
Step 5 — R² = r² = 0.9978² = 0.9956 → 99.56% of variance explained
Interpretation: For each additional study hour, exam score increases by 5.43 points on average. The model is an excellent fit (r = 0.998).
Using the Equation for Prediction
Once you have the regression equation ŷ = mx + b, substitute any x value to predict the corresponding y:
ŷ = 5.43(4.5) + 47.00 = 24.43 + 47.00 =
71.43 ≈ 71Predict score for 7 hours of study:
ŷ = 5.43(7) + 47.00 = 38.01 + 47.00 =
85.01 ≈ 85Predict score for 0 hours (y-intercept):
ŷ = 5.43(0) + 47.00 =
47.00 — baseline score with no study
Interpretation Reference Table
| Metric | Value/Range | Meaning |
|---|---|---|
| Slope m | Positive | y increases as x increases |
| Slope m | Negative | y decreases as x increases |
| Slope m | Zero | No linear trend — flat line |
| Y-intercept b | Any value | Predicted y when x = 0 |
| r | +0.9 to +1 | Very strong positive relationship |
| r | +0.7 to +0.9 | Strong positive relationship |
| r | +0.5 to +0.7 | Moderate positive relationship |
| r | 0 to +0.5 | Weak to no positive relationship |
| r | −0.5 to 0 | Weak to no negative relationship |
| r | −0.9 to −0.7 | Strong negative relationship |
| r | −1 to −0.9 | Very strong negative relationship |
| R² | 0.90 – 1.00 | Excellent model fit |
| R² | 0.70 – 0.89 | Good model fit |
| R² | 0.50 – 0.69 | Moderate model fit |
| R² | 0.00 – 0.49 | Poor model fit — consider other models |
Assumptions of Linear Regression
Simple linear regression is valid and reliable only when the following assumptions are reasonably met:
- Linearity: The true relationship between x and y is linear (a scatter plot should show a roughly straight pattern, not a curve).
- Independence: Each data point is collected independently — one observation does not influence another.
- Homoscedasticity: The spread (variance) of residuals is roughly constant across all x values — the scatter around the regression line should be similar throughout.
- Normality of residuals: For formal hypothesis testing and confidence intervals, residuals should be approximately normally distributed.
- No extreme outliers: One or two extreme outliers can dramatically shift the slope and intercept. Always inspect a scatter plot before trusting a regression equation.
Real-World Applications of Linear Regression
Linear regression is used in virtually every field where quantitative data is collected:
- Finance & Economics: Estimating the relationship between GDP growth and unemployment (Okun's Law), modelling stock returns vs market index returns (beta coefficient), predicting revenue from advertising spend.
- Medicine & Public Health: Dosage vs drug concentration in blood, blood pressure vs sodium intake, age vs bone density. Researchers in the US NIH, UK NHS, and WHO routinely use regression for clinical insights.
- Science & Engineering: Calibrating instruments (measured value vs actual value), analysing temperature vs material expansion, load vs deflection in structural engineering.
- Education: Predicting student GPA from SAT/ACT scores, analysing teacher-student ratio vs performance across schools (used by education ministries in Canada, Australia, UK, and USA).
- Real Estate: House price vs square footage, distance from city centre vs land value, interest rate vs mortgage applications.
- Machine Learning: Linear regression is the simplest supervised learning algorithm and the starting point for understanding neural networks, gradient descent, and regularisation.
- Environmental Science: CO₂ concentration vs global temperature, rainfall vs crop yield, air pollution level vs hospital admissions.
Common Mistakes to Avoid
- Using regression when the relationship is not linear: Always plot your data first. If the scatter plot shows a curve, a straight-line model will be misleading even if r looks decent.
- Extrapolating far outside the data range: The regression equation is only reliable within (or near) the x range of your original data. Extrapolation can produce absurd predictions.
- Confusing r with R²: r = 0.7 looks impressive, but R² = 0.49 — meaning only 49% of y's variation is explained. Always report both to give a complete picture.
- Ignoring outliers: A single extreme point can completely change the slope and intercept. Always check for and investigate outliers before including them.
- Assuming causation from correlation: A high r does not prove that x causes y. Always think critically about the mechanism.
- Reversing x and y: The regression of y on x is NOT the same as the regression of x on y (unless r = ±1). Make sure you assign the correct variable as the predictor (x) and response (y).
- Arithmetic errors in Σ calculations: The most common source of wrong answers in manual calculation. Build a table with columns for x, y, xy, x², y² and sum each column carefully.
Glossary
- Linear Regression
- A statistical method that models the linear relationship between a dependent variable y and an independent variable x using the equation ŷ = mx + b, fitted by minimising the sum of squared residuals.
- Least Squares Method
- The standard algorithm for finding the best-fit line by minimising the sum of squared vertical distances (residuals) between observed data points and the regression line.
- Slope (m)
- The rate of change of y with respect to x in the regression equation. Tells you how much y changes per one-unit increase in x.
- Y-intercept (b)
- The predicted value of y when x = 0. The point where the regression line crosses the y-axis.
- Residual
- The difference between an observed y value and the y value predicted by the regression equation: eᵢ = yᵢ − ŷᵢ. Positive residuals are points above the line; negative residuals are below.
- Pearson Correlation Coefficient (r)
- A number between −1 and +1 measuring the strength and direction of the linear relationship between x and y. The closer |r| is to 1, the stronger the relationship.
- Coefficient of Determination (R²)
- The square of r, representing the proportion of variance in y explained by the linear model. R² = 0.80 means 80% of y's variation is explained by x.
- Scatter Plot
- A graph of paired (x, y) data points. Always examine the scatter plot before running regression to verify linearity and check for outliers.
- Extrapolation
- Using the regression equation to predict y for x values outside the range of the original data. Generally unreliable — the linear pattern may not continue.
- Interpolation
- Using the regression equation to predict y for x values within the range of the original data. Generally reliable when the model has a good fit.
- Homoscedasticity
- The assumption that the variance of residuals is constant across all x values. Violation (heteroscedasticity) means the spread of errors is unequal — wider at some x values than others.
Frequently Asked Questions
Q: What is a linear regression equation?
A: It is the equation of the best-fit line through data points, written as y = mx + b, where m is the slope and b is the y-intercept. It is found using the least squares method that minimises the sum of squared residuals.
Q: How do you find the linear regression equation from data points?
A: Calculate n, Σx, Σy, Σxy, and Σx² from your data. Then: m = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²), and b = (Σy − m·Σx) / n. Write the equation as ŷ = mx + b.
Q: What is the least squares method?
A: It is the optimisation technique that finds the slope and intercept which minimise the sum of squared vertical distances (residuals) between each observed y value and the y value predicted by the line. It guarantees the best-fit line in the sense of minimum total squared error.
Q: What does the correlation coefficient r tell you?
A: It measures the strength (0 to 1) and direction (+ or −) of the linear relationship between x and y. r = +1 is a perfect positive relationship; r = −1 is a perfect negative relationship; r = 0 means no linear relationship.
Q: What is R² in linear regression?
A: R² = r² is the coefficient of determination. It tells you what percentage of the variation in y is explained by the linear model. R² = 0.85 means 85% of y's variance is accounted for by x.
Q: What is the slope in linear regression?
A: The slope m is the rate of change — for every 1-unit increase in x, y changes by m units on average. A positive slope means y rises with x; a negative slope means y falls with x.
Q: What is the y-intercept in linear regression?
A: The y-intercept b is the predicted y value when x = 0. It is where the regression line crosses the y-axis. It only has a useful real-world interpretation when x = 0 is within or near the observed data range.
Q: How many data points do you need for linear regression?
A: A minimum of 2 data points is mathematically required, but at least 5–10 are needed for a meaningful result. For professional or academic analysis, 30 or more data points are recommended.
Q: What is the difference between correlation and regression?
A: Correlation (r) quantifies the strength and direction of the relationship but gives no predictive equation. Regression produces the equation ŷ = mx + b that lets you predict y from x. Regression goes further than correlation.
Q: Can linear regression be used for prediction?
A: Yes — substitute any x value into ŷ = mx + b to get the predicted y. Predictions within the data range (interpolation) are reliable. Predictions outside it (extrapolation) should be treated with caution.