Memra

Plug in, but only inside the x-range

◈ 9 cards

ŷ at adspend 3 is 23.64 — inside the observed 2–10, interpolation; at 12 it is 54.71 — outside, extrapolation, unreliable however high R² is. =FORECAST.LINEAR(12, C2:C7, B2:B7) and predict(fit, newdata = data.frame(adspend = c(3, 12))) — the newdata column must be named exactly as in the formula; a bare vector or a differently named column fails. The intercept is a prediction at x = 0, outside the data.

Using the line

The CFO's two questions: what would a store spending $3k sell? and what about $12k? Plug each into . In Sheets, FORECAST.LINEAR from Lesson 13.4; in R, predict() with the new x values in a data frame whose column has the formula's name:

> predict(fit, newdata = data.frame(adspend = c(3, 12)))
       1        2 
23.64286 54.71429 
> round(predict(fit, newdata = data.frame(adspend = c(3, 12))), 2)
    1     2 
23.64 54.71 

$23.64k at $3k of ad spend; $54.71k at $12k. predict(fit) with no newdata returns the six fitted values from Lesson 13.3.

The column name is not decoration. lm() recorded that its predictor is called adspend, and predict() looks for that name in newdata:

> predict(fit, newdata = data.frame(x = c(3, 12)))
Error in eval(predvars, data, env) : object 'adspend' not found
> predict(fit, newdata = c(3, 12))
Error in model.frame.default(Terms, newdata, na.action = na.action, xlev = object$xlevels) : 
  'data' must be a data.frame, environment, or list

A wrong name and a bare vector are the two ways it fails, and both messages say so.

Worked example — one of those predictions is worth less

The six stores spent between $2k and $10k. A prediction at $3k is interpolation: inside the range the line was fitted on, where the data have shown the relationship to be linear. A prediction at $12k is extrapolation: beyond any ad spend a store actually made, where the line is a guess that the pattern continues. Nothing in the fit tests that. Diminishing returns — the seventh ad reaching people who already bought — is the ordinary shape of marketing response, and a straight line cannot bend to show it. describes the fit between 2 and 10 and promises nothing beyond.

The memorable case: a line fitted to hours studied against exam score predicts, for a student who studies long enough, a score above the maximum. The arithmetic is fine; the range was left behind.

The intercept is the extreme case. is the prediction at ad spend zero — two units below the smallest x in the data. It anchors the line; it is not a forecast for a store that stops advertising, and the report should not read it as one.

The rule

Before quoting a prediction, check the x against range(stores$adspend)2 10. Inside: quote it, with the residual standard error as a rough ± (Lesson 13.5: about $0.6k). Outside: say so, and say that the number assumes the relationship holds where it was never observed. A CFO asking about $12k needs the second sentence more than the first.

Type both, then fill the worksheet and reproduce the two predictions in Python

CRISP-DM: prediction is deployment of the model; deciding whether it may be quoted is evaluation.

NORMAL ~/memra/learn/afm-112/prediction-interpolation-and-extrapolation utf-8 LF