# Simple linear regression

> Simple linear regression: predicting one metric from another with a line of best fit, and checking the fit with R squared.

- Course: [Statistics for Product Managers and Marketers](https://productovi.com/courses/statistics-for-pm-and-marketers.md)
- Module: Relationships
- Lesson 18/24, ~42 min
- Source: https://productovi.com/courses/statistics-for-pm-and-marketers/linear-regression

## From correlation to simple linear regression

**Alex Tutor:** Yesterday we found CatChow's ad spend and orders correlate strongly, r = 0.95. Today the CFO wants an actual number from us on the product team: how many extra orders does one more dollar buy?

**Sofia:** Can't we just use r for that?

**Alex Tutor:** r only tells you how tightly the dots hug a line, not how steep that line is. For an actual number, we need simple linear regression: one predictor, one straight line.

**Dataset (CSV):** [CatChow weekly ad spend and orders from ads](https://productovi.com/datasets/statistics-for-pm-and-marketers/linear-regression/catchow-ad-spend-orders.csv)

The same ten weeks of ad spend and orders from the correlation lesson, now used to fit a line.

- `week`: Week number
- `ad_spend_usd`: Weekly ad spend, USD
- `orders_from_ads`: Orders attributed to ads that week

**Alex Tutor:** Same ten weeks as yesterday. This time we'll fit an actual line through the dots, not just measure how straight they look.

**Ad spend vs orders from ads, 10 weeks**

| Ad spend, USD | Orders from ads |
| --- | --- |
| 200 | 20 |
| 300 | 23 |
| 350 | 30 |
| 400 | 28 |
| 450 | 36 |
| 500 | 33 |
| 550 | 42 |
| 600 | 38 |
| 650 | 48 |
| 700 | 44 |

*The same scatter plot from the correlation lesson. Today's goal: the straight line that fits it best.*

**Sofia:** So we just draw a line through the middle of the dots?

**Alex Tutor:** That's the idea — but "through the middle" needs a precise rule, or every analyst would draw a different line.

> **Exercise:** What does linear regression give you that correlation alone does not?

## The line of best fit: least squares

**Alex Tutor:** Here's the rule for the line of best fit: pick the line that minimises the total squared vertical gap between each dot and the line. That's least squares.

**Sofia:** Squared again? Didn't we already do that for standard deviation?

**Alex Tutor:** Same trick: squaring stops positive and negative gaps from cancelling, and punishes big misses harder than small ones.

![Kibble pieces scattered around a straight orange rod, each linked to it by a vertical spring, while a cat nudges the rod into balance.](https://productovi.com/courses/statistics-for-pm-and-marketers/inline/linear-regression-illustration-2.webp)

*Picture each gap as a spring: the best line is where the total pull of all the springs is smallest.*

$$
b_1 = \frac{\sum (x_i-\bar{x})(y_i-\bar{y})}{\sum (x_i-\bar{x})^2}, \qquad b_0 = \bar{y} - b_1\bar{x}
$$

*The least-squares slope and intercept: the same numerator and denominator you computed for r, reused for the line's steepness.*

**Alex Tutor:** Each dot's actual value minus its predicted value is called a residual. Add up all the residuals, and they always sum to exactly zero.

**Sofia:** Wait, always? Even for a terrible line?

**Alex Tutor:** As long as the line includes an intercept and was fit by least squares — yes. It's the same reason deviations from a mean sum to zero.

> **Exercise:** The residuals of a least-squares regression line (fit with an intercept) always sum to exactly zero.

**Alex Tutor:** Good. Now let's actually fit the line on our ad-spend data.

**Sofia:** Same ten weeks from the dataset above?

**Alex Tutor:** Exactly. A spreadsheet's `SLOPE()` and `INTERCEPT()` functions do this arithmetic, or you can grind through the formula by hand.

**Sofia:** Let's see if spend really buys that many orders.

> **Exercise:** Using the dataset **CatChow weekly ad spend and orders from ads**, compute the slope b1 of the least-squares line predicting orders_from_ads from ad_spend_usd. Give your answer as orders per \$100 of extra weekly spend.

**Alex Tutor:** 5.39 extra orders per \$100 — not bad. But a slope alone doesn't answer "how many orders next week": it needs the intercept too.

**Sofia:** Right, the starting point when spend is zero?

**Alex Tutor:** Exactly — $b_0 = \bar y - b_1 \bar x \approx 34.2 - 0.0539 \times 470 \approx 8.85$ orders with zero ad spend. Some organic orders happen even without ads.

![A switched-off grey megaphone lies on a table while a few cats still walk on their own to a small cat-food shop.](https://productovi.com/courses/statistics-for-pm-and-marketers/inline/linear-regression-illustration-3.webp)

*The intercept: the orders that still arrive in a week with no ad spend at all.*

**Alex Tutor:** Careful how that 8.85 goes into a report, though. Which of these two sentences would you sign?

> **Exercise:** CatChow fitted a line of weekly orders from ads against weekly ad spend, using ten weeks with \$200–\$700 of spend. The line gives an intercept of b0 ≈ 8.85 orders. Which sentence for the CFO reads it **correctly**?

**Sofia:** So now we can predict any week?

> **Exercise:** Using $b_0 \approx 8.85$ and $b_1 \approx 0.0539$, predict orders_from_ads for a week with \$550 of ad spend.

**Alex Tutor:** 38.5 predicted, 42 actual for that week — a residual of about 3.5. Not bad for a straight line through messy real data.

**Alex Tutor:** Your turn. Week 4 is the one that dipped: \$400 of spend, 28 orders. What residual does the line leave there?

> **Exercise:** Week 4 had \$400 of ad spend and 28 orders from ads. Using $b_0 \approx 8.85$ and $b_1 \approx 0.0539$, what is the residual for week 4 (actual minus predicted)?

## R squared: how good is the whole line?

**Sofia:** Is there a way to say how good the whole line is, not just one week?

**Alex Tutor:** Yes — R squared (R²), also called the coefficient of determination. It's the share of the variation in orders that the line explains, from 0 to 1.

$$
R^2 = 1 - \frac{\sum (y_i - \hat{y}_i)^2}{\sum (y_i - \bar{y})^2}
$$

*R²: one minus the share of variance left over as residuals. R² = 1 is a perfect fit; R² = 0 means the line explains nothing.*

**Sofia:** For our data, is that just r squared?

**Alex Tutor:** For a single predictor, yes exactly: $R^2 = r^2 \approx 0.95^2 \approx 0.90$. About 90% of the week-to-week variation in orders lines up with ad spend.

**Alex Tutor:** Check it the long way too, from the residuals, so you see where that 0.90 comes from.

> **Exercise:** Across CatChow's ten weeks of ad spend and orders from ads, the squared deviations of orders from their mean add up to 749.6. The squared residuals from the fitted line add up to 77.5. Compute R².

**Alex Tutor:** Now tie the four terms together.

> **Exercise:** Match each regression term to what it means.

**Alex Tutor:** One more thing before you start forecasting next quarter with this line.

**Sofia:** Uh oh.

**Alex Tutor:** We only tested spend from \$200 to \$700. The line might not hold outside that interval — predicting for \$5,000 is a guess, not a measurement.

![A straight road runs across solid ground and continues past a cliff edge into fog, where a small cat raises a paw to stop.](https://productovi.com/courses/statistics-for-pm-and-marketers/inline/linear-regression-illustration-4.webp)

*The line is only tested where we have data. Beyond that stretch it is a guess, not a measurement.*

**Alex Tutor:** And R² = 0.90 is still not 1 — a residual of a few orders each week is normal. Don't expect the line to nail every single week.

> **Exercise:** CatChow fitted a line of weekly orders from ads against weekly ad spend over ten weeks with \$200–\$700 of spend (R² = 0.90). Which of these are real risks when using it to plan next quarter's ad budget? Select all that apply.

## Before you commit next quarter's plan

**Alex Tutor:** Good catches, all three.

**Sofia:** Go team.

**Alex Tutor:** One more diagnostic skill, then we wrap up with the memo.

**Alex Tutor:** Look at this residual pattern.

> **Exercise:** You plot the residuals against ad spend and they aren't scattered randomly: small negative residuals at low spend, positive around the middle, negative again at high spend — a curve. What might this pattern mean, and what would you consider doing next? Answer in 2-4 sentences.

**Alex Tutor:** Right idea — a systematic pattern like that means look for curvature, not just noise. Now, the real memo.

**Sofia:** For the CFO, no pressure.

**Alex Tutor:** You have the line, the R², and the extrapolation warning. Put it together.

**Alex Tutor:** Here's the scenario.

> **Exercise:** Write your recommendation about using this line to plan next quarter's ad budget.

**Alex Tutor:** Great work. You can now fit a simple linear regression and read what it does and doesn't promise. Next: what happens when more than one thing drives orders at once — multiple regression.

- Previous lesson: [Correlation coefficient: how two metrics move together](https://productovi.com/courses/statistics-for-pm-and-marketers/correlation.md)
- Next lesson: [Multiple linear regression](https://productovi.com/courses/statistics-for-pm-and-marketers/multiple-regression.md)

---
Productovi Learning. Free, no sign-up needed to read.
