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.

Lab 6 Solutions: Linear Regression with statsmodels

Open In Colab

Lab 6 Solutions: Linear Regression with statsmodels

These are the worked solutions, with outputs. Try the lab yourself first.

In this lab you build regression models of Qahwa House’s daily sales and use them to answer two questions from the manager.

The questions. Should we raise drink prices by 1 SAR? and Should every branch keep the “second cup half price” promotion?

How this lab works

  • Run the cells from top to bottom with Shift + Enter. Replace every ... with your code, then run the check cell under it.

  • Extended track: after Part D, try the ★ challenges.

  • Submit before the next lab session: File → Download → Download .ipynb.

You may use an AI assistant for code. If you do, add a short note at the end: what you asked, what you kept and what you changed.

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)

Part A: Simple regression

A1. Cups and temperature

Fit the simple regression of cups on temp_c with smf.ols(...).fit(). Store the fitted model in heat.

Intercept 308.19 temp_c 3.34 dtype: float64
✅ A1 a positive slope: hotter days sell more
✅ A1 the slope is between 3 and 4 cups per degree

A2. The slope by hand

The least-squares slope of a simple regression is b=cov(x,y)/sx2b = \text{cov}(x, y) / s_x^2. Compute it with pandas and store it in slope_by_hand. Then print R2R^2 (heat.rsquared).

3.3372 0.068
✅ A2 same slope as statsmodels
✅ A2 the temperature alone explains less than 10% of the variation

A3. Cups and price

Now fit cups on price_sar and store the model in naive.

Intercept -71.61 price_sar 31.59 dtype: float64
✅ A3 the slope is positive (!)

A4. Taken at face value, this slope says that raising prices sells more cups. In two sentences, give the most likely explanation. Hint: daily.groupby("branch")[["price_sar", "cups"]].mean().

Answer. The simple regression compares days across branches, and the busiest branch, Olaya, is also the most expensive (17.46 SAR and 513 cups on average) while University is the cheapest and quietest (12.97 SAR, 321 cups). The branch drives both the price and the sales, so the slope of +31.59 measures the difference between branches, not the effect of a price change.

Part B: Multiple regression and dummy variables

B1. The full model

Fit cups on price_sar, temp_c, weekend, promo and the branch (as C(branch)). Store it in full and print the coefficients.

Intercept                  645.01
C(branch)[T.Corniche]       72.20
C(branch)[T.Olaya]         159.15
C(branch)[T.University]   -147.41
price_sar                  -25.57
temp_c                       3.77
weekend                     69.18
promo                       54.37
dtype: float64
0.881
✅ B1 the price slope is now negative
✅ B1 three branch dummies (one branch is the baseline)
✅ B1 R-squared above 0.85

B2. Reading a dummy

Store the University branch’s coefficient in uni.

-147.41
✅ B2 University sells fewer cups than the baseline

B3. Which branch is the baseline? Write one sentence that explains uni to the manager, saying what is held fixed.

Answer. The baseline is Al Faisaliyah, the first branch in alphabetical order. On a day with the same price, temperature, weekday or weekend and promotion status, the University branch sells about 147 cups fewer than Al Faisaliyah.

B4. A prediction

Predict the cups sold at Olaya on a Saturday at 40 °C, with a price of 17 SAR and no promotion. Build a one-row DataFrame new_day with the five columns the model uses, then store full.get_prediction(new_day).summary_frame(alpha=0.05) in pred.

Loading...
✅ B4 between 550 and 650 cups
✅ B4 the interval for one day is wider than the interval for the mean

Part C: An interaction

C1. Does the promotion work the same everywhere?

Fit the model of B1 with promo * C(branch) in place of promo + C(branch). Store it in inter.

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
✅ C1 three interaction terms
✅ C1 a better fit than the full model

C2. The promotion’s effect in each branch

The promotion’s effect in the baseline branch is inter.params["promo"]; in another branch, add its promo:C(branch)[T.<branch>] term. Store the four effects in a Series promo_effect, indexed by branch name.

Olaya 15.7 Corniche 36.8 Al Faisaliyah 41.2 University 126.9 dtype: float64
✅ C2 four branches
✅ C2 the promotion works best at University
✅ C2 and worst at Olaya

C3. In two sentences: what would the manager have concluded from the model of B1, and why is it misleading?

Answer. The model of B1 has one promotion coefficient, so it tells the manager that a promotion day adds about 54 cups in every branch and suggests keeping it everywhere. With the interaction, the promotion adds about 127 cups at University but only about 16 at Olaya: the 54 is an average that describes no branch.

Part D: The decision

D1. What is one riyal on the price worth?

For each branch, take the mean cups and price_sar of non-promotion days (a DataFrame normal, one row per branch). With the price slope bb of inter, the revenue changes from p qp\,q to (p+1)(q+b)(p+1)(q+b). Store the change per day in a Series price_change, indexed by branch.

branch Olaya 25.5 Corniche -12.9 Al Faisaliyah -41.7 University -62.3 dtype: float64
✅ D1 four branches
✅ D1 small changes: under 100 SAR a day in every branch
✅ D1 Olaya gains, University loses

D2. Where does the promotion pay?

On a promotion day the average cup sells 18% below its price. With promo_effect extra cups, the revenue of a promotion day is (q+extra)×p×0.82(q + \text{extra}) \times p \times 0.82. Store the change against a normal day, (q+extra) p 0.82−p q(q + \text{extra})\,p\,0.82 - p\,q, in a Series promo_change.

branch Olaya -1381.8 Corniche -836.4 Al Faisaliyah -581.2 University 637.9 dtype: float64
✅ D2 the promotion pays at University
✅ D2 and loses revenue in the three other branches

D3. Your recommendation

Write three sentences for the manager: one on the price (with numbers), one on the promotion (with numbers), and one on what the regression cannot tell you.

Recommendation. A price 1 SAR higher costs about 26 cups a day in every branch, so revenue barely changes (+25.5 SAR a day at Olaya, −12.9 to −62.3 SAR elsewhere): I would not raise prices for revenue’s sake. Keep the second cup half price promotion only at University, where it brings about 127 extra cups and +637.9 SAR a day, and stop it in the other three branches, where it loses 581 to 1,382 SAR per promotion day. The model only covers prices between 12 and 18.5 SAR and the factors it includes; it says nothing about costs or about how customers would react to a lasting price rise, so the change should be tested for a month in one branch first.

★ Extended challenges

E1. Is the weekend effect the same everywhere? Add a weekend:C(branch) interaction to inter and look at the p-values of the new terms (model.pvalues). Should the model keep them?

E2. Elasticity. Fit np.log(cups) ~ np.log(price_sar) + temp_c + weekend + promo * C(branch). The coefficient of np.log(price_sar) is the price elasticity: the % change in cups for a 1% change in price. What does its value say about revenue?

E3. Revenue directly. Fit revenue_sar on the same variables as inter. Compute the promotion’s effect on revenue in each branch and compare it with D2.

weekend:C(branch)[T.Corniche]      0.530
weekend:C(branch)[T.Olaya]         0.480
weekend:C(branch)[T.University]    0.283
dtype: float64

E1. All three p-values are well above 0.05 (0.283 to 0.530): the data show no difference in the weekend lift between branches, so the extra terms only add noise. Keep the simpler model.

-0.987

E2. The elasticity is about −0.99: a price 1% higher sells about 1% fewer cups. Revenue, price times cups, then hardly changes, which is what D1 found branch by branch.

Olaya -1374.4 Corniche -836.5 Al Faisaliyah -609.7 University 659.9 dtype: float64

E3. The revenue model gives the same picture: the promotion adds about 660 SAR a day at University and loses about 610 to 1,374 SAR elsewhere, close to D2’s figures, which were computed from the cups model and the 18% discount.