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: Linear Regression with statsmodels

Open In Colab

Lab 6: Linear Regression with statsmodels

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.

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.

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).

A3. Cups and price

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

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().

Your answer here (double-click to edit).

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.

B2. Reading a dummy

Store the University branch’s coefficient in uni.

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

Your answer here (double-click to edit).

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.

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.

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.

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

Your answer here (double-click to edit).

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.

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.

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.

Write your recommendation here (double-click to edit).

★ 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.

AI-use log (if you used an AI assistant)

What you asked, what you kept, what you changed.