Memra

ŷ = b₀ + b₁x, residuals, least squares

◈ 11 cards

ŷ = 13.29 + 3.45 · adspend. =SLOPE(C2:C7, B2:B7) — y first — is 3.4524; =INTERCEPT is 13.2857; coef(lm(sales ~ adspend, data = stores)) prints both. The slope sentence: for each extra $1k of ad spend, sales rise by about $3.45k on average. Residual = actual − predicted; store 3 is 31 − 30.55 = +0.45, above the line. Least squares minimises the sum of squared residuals; the residuals sum to 0 and the line passes through (6, 34).

The line through the cloud

A strong linear scatter earns a line: . is the slope, the intercept, and ("y-hat") is the predicted sales at a given ad spend — the hat says "from the line, not observed". In Sheets, the two coefficients are two functions, and both take the y range first:

=SLOPE(C2:C7, B2:B7)        → 3.4524
=INTERCEPT(C2:C7, B2:B7)    → 13.2857

Swap the ranges and you get the slope of ad spend on sales — a different line for a different question, with no error to tell you. In R the line is fitted once and both coefficients read off it:

> fit <- lm(sales ~ adspend, data = stores)
> coef(fit)
(Intercept)     adspend 
  13.285714    3.452381 

So .

Worked example — the slope sentence

The slope is the number the CFO wants, and it has one correct sentence: for each additional $1k of ad spend, sales are higher by about $3.45k, on average. Every part matters. One unit of x (the slope is per unit); y changes by b₁ (not "x changes"); on average (individual stores sit above and below the line). Wrong versions: "sales are 3.45 times ad spend" (that is a ratio, not a slope); "sales equal 3.45" (a level, not a change); "ad spend rises by 3.45" (the wrong variable).

The intercept is the predicted sales at zero ad spend, $13.29k. No store spent zero — the data run from $2k to $10k — so the intercept is an anchor for the line, not a forecast for a store that stops advertising. Lesson 13.6 makes that a rule.

Residuals — how far each store is from the line

A residual is actual minus predicted, . Store 3 spent $5k and sold $31k; the line predicts :

> coef(fit)[1] + coef(fit)[2] * 5
(Intercept) 
   30.54762 
> round(residuals(fit), 4)
      1       2       3       4       5       6 
-0.1905 -0.0952  0.4524  0.5476 -0.9048  0.1905 

Store 3's residual is : it sold more than the line predicts — the point is above the line, and the model under-predicted it. Store 5 (−0.90) sits below; the model over-predicted. The signs are the whole meaning: positive is above, negative is below, and a store manager whose residual is consistently positive is doing something the ad budget does not explain.

Why this line and not another

Of every line that could be drawn through six points, the least-squares line is the one that makes the sum of the squared residuals as small as possible. Squared, because the plain residuals of the best line always add to zero — the positives and negatives cancel by construction:

> sum(residuals(fit))
[1] -1.526557e-16
> coef(fit)[1] + coef(fit)[2] * 6
(Intercept) 
         34 

is zero to floating-point precision. And at the mean ad spend, , the line predicts exactly the mean sales, : every least-squares line passes through . Two checks you can run on any fit.

Type the three calls, then fill the worksheet and reproduce the slope in Python

The code block computes and .

CRISP-DM: fitting the line is modelling → build model.

storexyŷresidual122020.19−0.19242727.10−0.10353130.55+0.45473837.45+0.55584040.90−0.906104847.81+0.19ŷ = 13.2857 + 3.4524 x. Least squares picks the line that minimises the sum of the squared residuals;the plain residuals always sum to 0.
Actual minus predicted, store by store. Positive (green) is above the line — under-predicted; negative (red) is below — over-predicted. The six residuals sum to 0.
NORMAL ~/memra/learn/afm-112/the-fitted-line-and-the-slope-sentence utf-8 LF