# Linear regression: the best-fit line, slope, intercept and R² explained

> Study hours against marks, ad spend against sales, temperature against ice-cream orders. Linear regression draws the one straight line that fits the points best and lets you predict the next one. This guide builds it by hand from eight data points and shows where it breaks.

Published 2026-10-09 by Toolcloak. 3 minute read. Original: https://toolcloak.pages.dev/articles/linear-regression-the-best-fit-line-slope-intercept-and-r-squared-explained
Related free tool: [Linear Regression Calculator (Line of Best Fit)](https://toolcloak.pages.dev/tools/linear-regression-calculator)

Linear regression answers one question: if two things move together, what straight line describes that movement best? The line has the form **y = m × x + b**. The slope **m** says how much y changes for each step in x, and the intercept **b** says where the line starts when x is zero.

> **In short**
> - The **least squares** line is the one that makes the total of the squared gaps (residuals) between points and line as small as possible.
> - In our example, each extra study hour adds about **4.26 marks**, and the line is **y = 4.26x + 46.57**.
> - **R²** tells you how much of the ups and downs the line explains. Here it is 0.97, or 97%.
> - Predicting **inside** your data range is reasonably safe. Predicting far outside it is not.

## The example: hours studied and test score

Eight students recorded how many hours they studied and what they scored out of 100. Plotting them shows a clear upward drift, but no single straight line touches every point. Regression finds the best compromise.

| Hours (x) | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 |
| --- | --- | --- | --- | --- | --- | --- | --- | --- |
| Score (y) | 52 | 55 | 61 | 60 | 68 | 72 | 75 | 83 |

*Average hours = 4.5, average score = 65.75. The best-fit line always passes through this point of averages.*

**Real scores and the fitted line**

| Hours studied | Actual score | Fitted line |
| --- | --- | --- |
| 1 | 52 | 50.8 |
| 2 | 55 | 55.1 |
| 3 | 61 | 59.4 |
| 4 | 60 | 63.6 |
| 5 | 68 | 67.9 |
| 6 | 72 | 72.1 |
| 7 | 75 | 76.4 |
| 8 | 83 | 80.7 |

*The two lines stay close together. The biggest miss is at 4 hours, where the student scored 3.6 marks below the line.*

Try it: [Linear Regression Calculator (Line of Best Fit)](https://toolcloak.pages.dev/tools/linear-regression-calculator). Paste your x and y values to get slope, intercept, R² and predictions.

## Building the line in four steps

**From raw data to y = 4.26x + 46.57**

1. **Find the averages.** x̄ = 4.5 hours, ȳ = 65.75 marks.
2. **Measure the pull.** Add up (x − x̄)(y − ȳ) for every point: 179. Add up (x − x̄)²: 42.
3. **Slope.** m = 179 ÷ 42 = 4.2619. Each extra hour adds about 4.26 marks.
4. **Intercept.** b = ȳ − m × x̄ = 65.75 − 4.2619 × 4.5 = 46.57.

*These are the standard least squares formulas. The calculator does the same arithmetic for any number of points.*

## Reading slope and intercept

**What the numbers mean here**

|  | Value | Note |
| --- | --- | --- |
| Slope | 4.26 | Marks gained per extra hour |
| Intercept | 46.57 | Predicted score at zero hours |
| R² | 0.97 | Share explained by hours |
| Correlation r | 0.98 | Strong and positive |

*The intercept is a mathematical starting point, not always a real-life fact. Nobody studies for zero hours and scores exactly 46.57; it only anchors the line.*

## Residuals: how far each point is from the line

A **residual** is the actual value minus the predicted one. Positive means the student did better than the line expected; negative means worse. Least squares makes the residuals add up to zero and keeps their squares as small as possible.

| Hours | Actual | Predicted | Residual |
| --- | --- | --- | --- |
| 1 | 52 | 50.8 | +1.2 |
| 2 | 55 | 55.1 | −0.1 |
| 3 | 61 | 59.4 | +1.6 |
| 4 | 60 | 63.6 | −3.6 |
| 5 | 68 | 67.9 | +0.1 |
| 6 | 72 | 72.1 | −0.1 |
| 7 | 75 | 76.4 | −1.4 |
| 8 | 83 | 80.7 | +2.3 |

*Squared residuals add up to about 24.6, against about 787.5 for the total variation around the average score.*

## R²: how much does the line explain?

R² compares two amounts: the leftover error around the line (24.6) and the total spread of scores around their average (787.5). R² = 1 − 24.6 ÷ 787.5 = **0.969**. About 97% of the differences between students' scores line up with study hours in this tiny sample.

**Where the variation in scores comes from**

|  | Share |
| --- | --- |
| Explained by the line | 96.9% |
| Left over (residuals) | 3.1% |

*A high R² does not prove that studying causes higher scores. Motivation, sleep or prior knowledge could drive both.*

**How to read R²**

| Zone | From | To |
| --- | --- | --- |
| Weak | 0 | 0.25 |
| Moderate | 0.25 | 0.5 |
| Good | 0.5 | 0.75 |
| Strong | 0.75 | 1 |

*These bands are a rough guide. What counts as good depends on the field: 0.5 can be excellent for human behaviour and poor for a physics experiment.*

## Making predictions

Put a new x into the equation. For 9 hours the line says 4.2619 × 9 + 46.57 = **84.9**. That is just past our data (the maximum was 8), so it is a mild stretch. For 20 hours the line gives 131.8 marks, which is impossible on a test out of 100.

**Interpolating against extrapolating**

| Inside the data (1 to 8 hours) | Far outside the data (20 hours) |
| --- | --- |
| Reasonably safe | Unreliable |
| 6 hours gives about 72 marks | The line predicts 131.8 out of 100 |
| The line was built from points around it | Real relationships bend and flatten |
| Still carries an error of a few marks | Nothing in the data supports it |

*A straight line is an approximation over the range you measured. Always ask whether the relationship could plausibly continue.*

## One strange point can tilt the whole line

Regression gives every point a vote, and points far from the rest have the loudest voice. Change only the last student's score from 83 to 40 (say, they fell ill) and the slope collapses from 4.26 to about **0.68** marks per hour. Always plot your data before trusting the line.

**Slope of the best-fit line**

|  | Marks per hour |
| --- | --- |
| All 8 points as measured | 4.26 |
| Last score changed to 40 | 0.68 |

*One altered point out of eight cut the slope by 84%. Check unusual points: are they errors, or real cases the line should explain?*

> **Correlation is not causation:** A line that fits well tells you the two things move together in this data. It does not tell you why. Ice-cream sales and sunburn both rise with temperature; neither causes the other.

## Checklist before you trust a regression

1. **Plot first.** If the points curve, a straight line is the wrong shape.
2. **Look for outliers** and decide whether they are errors or real.
3. **Keep predictions inside the data range** wherever possible.
4. **Use enough points.** Eight points are fine for learning, but too few for serious conclusions.
5. **Do not claim cause** from fit alone.

## Frequently asked questions

### What is linear regression?

It is a method that finds the straight line y = mx + b that best fits a set of points, so you can describe the relationship between two variables and predict new values.

### What is the least squares method?

It chooses the line that minimises the sum of the squared vertical gaps between the points and the line. Squaring stops positive and negative gaps from cancelling and punishes large misses.

### What does R-squared mean?

R² is the share of the variation in y that the line explains. 0 means the line explains nothing and 1 means it explains everything. In the example, 0.97 means about 97 percent.

### What is a residual?

A residual is the actual value minus the value the line predicts. Small, random-looking residuals suggest a good fit; patterns in them suggest a straight line is the wrong model.

### Can I predict values outside my data?

You can, but be careful. Relationships often bend or flatten outside the measured range, so predictions far beyond your data can be badly wrong.

### Is regression the same as correlation?

They are related. Correlation r measures how tightly points follow a line, and for one variable R² equals r squared. Regression goes further and gives the actual equation of the line.

