Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Regression: What Drives Daily Sales

Open In Colab

Regression: What Drives Daily Sales

So far we have described variables and the relationships between pairs of them. Regression goes one step further: it writes the outcome we care about as an equation in the variables that drive it, and each coefficient says how much the outcome changes when one driver changes while the others stay fixed. That last phrase is what makes regression useful for decisions, and this reading shows how easily it is lost.

The manager of Qahwa House has two questions:

  1. Should we raise drink prices by 1 SAR? What is a riyal on the price worth?

  2. Should every branch keep the “second cup half price” promotion?

After this reading you can:

  • fit a simple regression and read its slope, intercept, R2R^2 and standard error;

  • fit a multiple regression with the statsmodels formula interface and read each coefficient “holding the others fixed”;

  • explain how leaving out a variable can flip the sign of a coefficient;

  • add a categorical variable as dummy variables and read them against a baseline;

  • add an interaction and explain when a model needs one;

  • predict a new day with a prediction interval, check the residuals, and turn a model into a decision.

The data

One row per branch and day of 2025: 4 branches × 365 days = 1,460 rows. Every month, each branch tested a slightly different price for its drinks.

Loading...
ColumnMeaning
date, weekday, monththe day
weekend1 on Friday and Saturday
branch, citywhere
temp_cthe day’s highest temperature (°C)
price_sarthe average price of a cup that day (SAR); each branch tested a different price every month
promo1 on a second cup half price day
cupsdrinks sold that day
revenue_sardrink revenue that day (SAR)
Loading...

Olaya is the busiest and the most expensive branch; University the quietest and the cheapest. Keep that in mind.

1. Simple regression: cups on price

A simple regression fits a straight line y^=a+b x\hat{y} = a + b\,x through a scatterplot. The least-squares line is the one that makes the sum of the squared residuals e=y−y^e = y - \hat{y} (the vertical distances from the points to the line) as small as possible.

In statsmodels, a formula names the outcome on the left of ~ and the explanatory variables on the right:

Intercept   -71.61
price_sar    31.59
dtype: float64
<Figure size 650x380 with 1 Axes>

The slope is +31.59: each riyal on the price comes with about 32 more cups a day. Taken at face value, raising prices would sell more coffee. Before we doubt it, note what the slope is. For a simple regression it is the covariance divided by the variance of xx, and the line passes through the two means:

b=cov(x,y)sx2,a=yˉ−b xˉb = \frac{\text{cov}(x, y)}{s_x^2}, \qquad a = \bar{y} - b\,\bar{x}
slope b = 31.59, intercept a = -71.61

How good is the fit?

Two numbers summarise it:

  • R2R^2, the share of the variation in yy that the line explains (between 0 and 1). For a simple regression it is the square of the correlation.

  • The standard error of estimate ses_e, the typical size of a residual, in the units of yy. About 95% of the points lie within ±2se\pm 2 s_e of the line.

R-squared            0.416   (correlation squared: 0.416)
standard error s_e   69.0 cups

The price explains 42% of the day-to-day variation in cups, with a typical error of 69 cups. A fair fit, and a wrong answer, as the next section shows. The intercept (−71.6 cups at a price of 0 SAR) means nothing here: no branch ever charged less than 12 SAR. Never read a regression far outside the data it was fitted on.

2. Dummy variables: adding the branch

The branch is a categorical variable. A regression needs numbers, so it uses dummy variables: one 0/1 column per category, except one category, the baseline, which gets none. In a formula, C(branch) creates them; the baseline is the first category in alphabetical order, here Al Faisaliyah.

Loading...
Intercept                  651.87
C(branch)[T.Corniche]       67.93
C(branch)[T.Olaya]         141.07
C(branch)[T.University]   -123.26
price_sar                  -16.02
dtype: float64
R-squared 0.634

The price slope changed sign: −16.02 cups per riyal. Each branch dummy shifts the line up or down relative to Al Faisaliyah, at the same price: Olaya sells 141 cups more, University 123 fewer. The model draws four parallel lines, one per branch:

<Figure size 650x380 with 1 Axes>

The simple regression compared branches: Olaya charges more and sells more, because it is the busiest branch, not because of its prices. Once the branch is in the model, the price slope compares days within a branch. This is the confounding of Week 4, and regression’s answer to it: put the confounder in the model.

3. A second confounder: the heat

Each branch tested its prices month by month, but not quite at random: managers tried higher prices in the summer, when the heat sells more iced drinks.

Loading...

Summer days had higher prices (by 0.68 SAR on average), far higher temperatures, and more cups. Without the temperature in the model, part of the heat’s effect is credited to the price. Add it:

Loading...

With the branch and the temperature fixed, a price 1 SAR higher sells about 25.6 fewer cups a day. The estimate went from +31.6 to −16.0 to −25.6 as we added the two confounders. A coefficient in a multiple regression answers “what if this variable changes and the others in the model do not”. Leave a confounder out, and the coefficient quietly absorbs its effect.

Two warnings. Regression only adjusts for the variables you include; a confounder you did not measure still biases the result. And the estimate is credible here because the prices were tested: within a branch and a temperature, they changed for no other reason. With prices that managers set in response to demand, no amount of regression would be enough.

4. The full model: weekend and promotion

Two more 0/1 variables explain the day: the weekend (Friday and Saturday) and the promotion. Both are already dummies, so they enter as they are.

                         coefficient  95% low  95% high
Intercept                     645.01   612.01    678.01
C(branch)[T.Corniche]          72.20    67.28     77.13
C(branch)[T.Olaya]            159.15   153.10    165.20
C(branch)[T.University]      -147.41  -154.74   -140.07
price_sar                     -25.57   -27.75    -23.39
temp_c                          3.77     3.53      4.01
weekend                        69.18    65.64     72.72
promo                          54.37    49.78     58.97

R-squared 0.881, standard error 31.2 cups

Each coefficient holds all the others fixed:

CoefficientReading
price_sar −25.57one riyal more on the price: about 26 cups fewer a day
temp_c 3.77each degree hotter: about 4 cups more (iced drinks)
weekend 69.18a Friday or Saturday: about 69 cups more than a weekday
promo 54.37a promotion day: about 54 cups more
C(branch)[T.Olaya] 159.15Olaya sells 159 cups more than Al Faisaliyah on a comparable day
C(branch)[T.University] −147.41University sells 147 cups fewer than Al Faisaliyah

The model now explains 88% of the variation, and a typical error has dropped from 69 to 31 cups. The 95% interval for the price slope, −27.75 to −23.39, uses Week 5’s idea: the slope is an estimate from one year of days, and another year would give a slightly different one.

5. Interactions: does the promotion work everywhere?

The full model has one promotion coefficient: +54 cups, in every branch. That is an assumption, not a finding. An interaction lets the effect of one variable depend on another. In a formula, promo * C(branch) adds the promotion, the branch dummies and their products promo:C(branch)[T.…].

promo                            41.24
promo:C(branch)[T.Corniche]      -4.41
promo:C(branch)[T.Olaya]        -25.58
promo:C(branch)[T.University]    85.65
dtype: float64

R-squared 0.907, standard error 27.6 cups

Read it like the branch dummies: promo is the promotion’s effect in the baseline branch (Al Faisaliyah); each promo:C(branch)[T.…] term is how much larger or smaller it is in another branch. Adding them up:

<Figure size 650x320 with 1 Axes>
Olaya 15.7 Corniche 36.8 Al Faisaliyah 41.2 University 126.9 dtype: float64

The promotion adds about 127 cups a day at University, where students are price-sensitive, but only 16 at Olaya. The single coefficient of 54 was an average that fits no branch. With the interaction, R2R^2 rises to 0.907 and the typical error falls to 27.6 cups.

When does a model need an interaction? When you have a reason to think an effect differs between groups, and the data agree: here the University term is large and its 95% interval is far from 0. The same test on the weekend effect (Extended challenge E1 of the lab) finds no difference between branches, so that interaction is not needed.

6. From the model to the decision

What is one riyal on the price worth?

On a normal (non-promotion) day, a branch sells qq cups at pp SAR. Raise the price by 1 SAR and the model says it sells q+bq + b cups, with bb the price slope. The revenue changes from p qp\,q to (p+1)(q+b)(p + 1)(q + b):

price slope: -26.28 cups per SAR
Loading...

A riyal more costs about 26 cups a day in every branch, and revenue barely moves: +25.5 SAR a day at Olaya, −12.9 to −62.3 SAR a day elsewhere. The customers who leave take away about as much revenue as the higher price brings in. The naive simple regression would have promised 32 more cups. Revenue is not profit, though: fewer cups also means less milk and coffee bought, a question for the optimization weeks.

Where should the promotion run?

On a promotion day the average cup sells 18% below its price:

promo
0    0.00
1    0.18
dtype: float64
Loading...

Only at University do the extra cups pay for the discount: +637.9 SAR per promotion day. Everywhere else the promotion loses revenue, most of all at Olaya (−1,381.8 SAR a day), where it brings almost no new customers. With one promotion coefficient, the manager would have seen +54 cups everywhere and kept the promotion in all four branches.

7. Predicting a new day, and checking the residuals

A model also predicts. get_prediction gives the predicted mean and two intervals: one for the average of many such days (narrow), and one for a single day (wide, because one day also has its own residual):

Loading...

A Saturday at Olaya, 40 °C, at 17 SAR, without promotion: about 596 cups, and 95% of such days sell between 541 and 650. Plan stock for the interval, not the point.

Finally, look at the residuals. If the model has captured the pattern, they should look like noise: centred on zero, the same spread everywhere, no curve.

<Figure size 650x320 with 1 Axes>
mean residual 0.00, standard deviation 27.5

No pattern: a horizontal band around zero, as wide on busy days as on quiet ones. A curve would call for a transformed variable (Extended challenge E2 of the lab uses logarithms); a funnel would mean the errors grow with the level of sales.

Check your understanding

  1. In the regression cups ~ price_sar + temp_c, the price coefficient is −25. Say in one sentence what it means.

  2. Why did the price slope change sign when the branch entered the model?

  3. A variable region has five categories. How many dummy variables does the regression use, and why not five?

  4. The model cups ~ promo + C(branch) has a promotion coefficient of 54. What does adding promo:C(branch) allow, and why does it matter for the manager?

  5. A model predicts 596 cups with a 95% prediction interval of 541 to 650. Why is the interval for the mean of many such days much narrower?

Summary

  • A regression writes the outcome as an equation in its drivers; least squares minimises the squared residuals.

  • R2R^2 is the share of variation explained; the standard error ses_e is the typical error, in the outcome’s units.

  • A coefficient in a multiple regression holds the other variables in the model fixed; leave out a confounder and the coefficient absorbs its effect (here, +31.6 became −25.6).

  • A categorical variable enters as dummies, read against a baseline category.

  • An interaction lets an effect differ between groups: the promotion adds 127 cups at University and 16 at Olaya.

  • Turn coefficients into money before deciding: +1 SAR barely changes revenue; the promotion pays only at University.

Next week: how to tell whether a model will predict new data well (validation), and regression’s cousin for yes/no outcomes: classification.