Business Analytics & Data Science

B06 · Intermediate

Regression Analysis

10 lessons

B06 · Module 3, lesson 2 · Lesson 6 of 10

Categorical predictors and interactions

Put categories such as the café or the day of the week into a regression with dummy variables, read each coefficient against a reference level, and let one predictor’s effect depend on another with an interaction. Then see a café dummy resolve B05’s Simpson’s paradox.

About 65 minutes

Before you start

You will be able to

  • Construct dummy variables for a categorical predictor with k levels, choosing a reference level, and explain why k − 1 dummies are needed (the dummy-variable trap).
  • Interpret dummy coefficients as differences from the reference level, holding the other predictors fixed.
  • Build an interaction term and interpret its coefficient as the difference between two groups’ slopes or effects.
  • Calculate a group’s own effect or slope from main-effect and interaction coefficients, and explain why centring a predictor changes the main effects but not the fit.
  • Use a group dummy to resolve a Simpson’s paradox, and say what the pooled slope was mixing.

Review first

Fictional case A fictional coffee business founded by Ana Lim. It starts as a catering and barista-training service, opens its own cafés and an online shop for beans, and plans a canned cold brew.

The idea

Fictional case

Ana wants one model of footfall for both cafés, so that the weather’s effect is estimated from all 612 café-days. But the cafés are very different places: Harbourside on the waterfront fills up at weekends, while Millbrook, among offices, empties. Café and day of the week are categories, not numbers. A regression can only multiply numbers by slopes, so the categories have to be turned into numbers first.

A dummy variable

A dummy variable is a 0/1 column marking membership of one category. For the café, Ana creates Millbrook=1\text{Millbrook} = 1 on Millbrook’s rows and 0 on Harbourside’s. Harbourside, the category without a dummy, is the reference category. Regressed on this one column, footfall gives

footfall^=139.7+86.7 Millbrook\widehat{\text{footfall}} = 139.7 + 86.7\,\text{Millbrook}

Put in Millbrook = 0 and the fitted value is 139.7, Harbourside’s mean daily footfall. Put in 1 and it is 139.7+86.7=226.3139.7 + 86.7 = 226.3, Millbrook’s mean. The dummy’s coefficient is the difference between the two cafés’ means, and its t test is the pooled-variance two-sample t test of B04. With other predictors in the model the coefficient keeps the same meaning, adjusted: the difference between the cafés on days alike on the other predictors. Adding the high and rain leaves it at 86.7 here, because both cafés share the same days and weather: the café dummy is uncorrelated with the weather columns, so there is nothing for the adjustment to remove.

When the weekend works differently at each café

Add a weekend dummy (1 on Saturdays and Sundays) and the model assumes the weekend changes footfall by the same amount at both cafés. Look at the four means first:

Mean daily footfall by café and type of day (1 March – 31 December 2027)
Weekdays (220 days)Weekends (86 days)Weekend − weekday
Harbourside114.3204.590.2
Millbrook271.8110.2−161.6
The four means as an interaction plot. The lines cross: the weekend raises Harbourside’s footfall and lowers Millbrook’s. Parallel lines would mean no interaction.

The weekend adds about 90 people at Harbourside and takes away about 162 at Millbrook. No single weekend coefficient can describe both. An interaction term lets it differ: multiply the two dummies, Millbrook×Weekend\text{Millbrook} \times \text{Weekend}, which is 1 only on Millbrook’s weekend days, and add the product as a third predictor:

footfall^=114.3+157.4 Millbrook+90.2 Weekend−251.8 (Millbrook×Weekend)\widehat{\text{footfall}} = 114.3 + 157.4\,\text{Millbrook} + 90.2\,\text{Weekend} - 251.8\,(\text{Millbrook} \times \text{Weekend})

With four coefficients for four groups, the model reproduces the four means exactly. The intercept is Harbourside’s weekday mean. The Millbrook coefficient, 157.4, is the cafés’ difference on weekdays (both other terms are 0). The Weekend coefficient, 90.2, is the weekend effect at Harbourside, the reference café. The interaction, −251.8, is how much Millbrook’s weekend effect differs from Harbourside’s, so Millbrook’s own weekend effect is 90.2−251.8=−161.690.2 - 251.8 = -161.6. Once an interaction is in a model, neither of its two parts has a meaning alone: each is the effect at the other’s reference level.

What leaving the interaction out does

The case’s footfall model, with the weather, festivals and both dummies, puts the weekend’s effect at +68.8 people at Harbourside and 68.8−232.2=−163.468.8 - 232.2 = -163.4 at Millbrook, holding the weather fixed, with R² = 0.904. Drop the interaction and the single weekend coefficient becomes −49.8: an average of a gain and a larger loss that describes neither café, and R² falls to 0.553. A manager reading −49.8 would cut Harbourside’s weekend roster, the opposite of what it needs.

In depth

Categories with more than two levels

A categorical predictor with k levels needs k−1k - 1 dummies, one for each level except the reference. Millbrook’s day of the week has seven levels; with Monday as the reference, Ana creates six dummies (Tue, …, Sun). Fitted with the high and rain over Millbrook’s 306 days, Tuesday to Friday’s coefficients are between −6.8 and +0.8 people, none significant at 5%, while Saturday’s is −168.0 and Sunday’s −162.6: each compares that day with a Monday of the same weather. The five weekdays behave alike, and so do the two weekend days, which is why one weekend dummy does almost as well. Its adjusted R² is 0.8456, against 0.8442 for the six weekday dummies, and it is one coefficient instead of six. Whether a group of dummies is needed is a question for a single test of the group, the nested F test of the multicollinearity-and-model-choice lesson, not for six separate t tests.

The choice of reference changes the coefficients but not the model. Make Millbrook the reference, with a Harbourside dummy, and the cell-means model becomes 271.8−157.4 Harbourside−161.6 Weekend+251.8 (Harbourside×Weekend)271.8 - 157.4\,\text{Harbourside} - 161.6\,\text{Weekend} + 251.8\,(\text{Harbourside} \times \text{Weekend}): the same four fitted means, the same R², the same residuals. Choose the reference that makes the comparisons you want to report direct, usually the largest or most familiar group.

The dummy-variable trap

Why not a dummy for every level? With an intercept in the model, the k dummies of one variable always add up to 1, the intercept’s own column, so one of them is an exact combination of the others and least squares has no unique answer. This is the dummy-variable trap, a case of perfect collinearity. Software reports it rather than fitting: the B06 engine, given all seven weekday dummies with an intercept, says the terms “are perfectly collinear with the others, so their effects can’t be separated”. The same happens with less obvious duplicates: Millbrook’s Saturday and Sunday dummies add up to its weekend column, so a model with all three can’t be fitted either. Excel’s LINEST, given collinear columns, sets one coefficient to 0 and carries on, which is easy to miss.

An interaction with a numeric predictor

An interaction can also let a slope differ between groups. Add High×Millbrook\text{High} \times \text{Millbrook} to the case’s footfall model: the product equals the high on Millbrook’s rows and 0 on Harbourside’s. The slope on the high, 1.65 people per °F, is now Harbourside’s; the interaction, −0.76 (t = −5.27), is the difference, so Millbrook’s slope is 1.65−0.76=0.891.65 - 0.76 = 0.89 people per °F. In the generator, footfall rises 1.1% per °F of Harbourside’s expected trade and 0.2% at Millbrook, so in people the waterfront café is far more sensitive to the weather.

Read the main effect of the café with care. In this model the Millbrook coefficient, 206.1, is the cafés’ difference on a weekday when the high is 0°F, which never happened in 2027. Centring the temperature, using High−60\text{High} - 60 in both terms, changes only that: the Millbrook coefficient becomes 160.6, the difference on a 60°F weekday, while the slopes, the fitted values and R² stay exactly the same. Centre a numeric predictor at a typical value whenever it enters an interaction, so the main effects describe something that exists.

The build-up of the multiple-regression lesson matters here too, and differs by café. Add the build-up and its interaction with the café, and the slopes become 1.43 people per °F at Harbourside and 0.45 at Millbrook; fitted to the generator’s planted mean footfall, the same model gives 1.46 and 0.44. Without the build-up, Millbrook’s slope of 0.89 is twice the planted one, because its spring was both cold and still building up.

Simpson’s paradox, with a café dummy

B05 found that across all 612 café-days footfall and the average ticket correlate at −0.324, while within each café they correlate positively (0.73 at Harbourside, 0.52 at Millbrook). In regression terms, the pooled slope of the average ticket on footfall is −$0.40 per 100 people. Add the Millbrook dummy and the slope on footfall becomes +$0.25 per 100 people, with Millbrook’s tickets $2.30 smaller at the same footfall. The dummy holds the café fixed: the slope now compares days within the same café, and the negative pooled slope came from the café difference alone (Millbrook has more people and smaller tickets).

Add the interaction Footfall×Millbrook\text{Footfall} \times \text{Millbrook} and each café gets its own slope: $0.36 per 100 people at Harbourside and 0.36−0.18=0.180.36 - 0.18 = 0.18 at Millbrook. The generator planted $0.40 and $0.22 before sales were scaled to the year’s $236,000 total, which is about $0.33 and $0.18 in the log’s dollars, so the fitted slopes are close to the truth. The model with one shared slope, $0.25, is between the two, an average weighted towards the café whose footfall varies more.

Testing an interaction

An interaction’s t test asks whether the two groups’ slopes (or effects) differ, holding the rest of the model fixed. The café × temperature interaction’s t of −5.27 says the cafés’ temperature slopes clearly differ. When a categorical predictor has more than two levels, its interaction with a numeric predictor is several product columns, and the question “do the slopes differ at all?” needs one test of all of them together: the nested F test in the next lesson.

Formulas

Model with a dummy and an interaction

y^=b0+b1x+b2D+b3(x×D)\hat y = b_0 + b_1x + b_2D + b_3(x \times D)
y^\hat y
Fitted valueunits of y
DD
Dummy: 1 for the group, 0 for the reference group
xx
A numeric predictor (or another dummy)its units
b0,b1b_0, b_1
Intercept and slope on x for the reference groupunits of y; per unit of x
b2b_2
Difference between the groups when x = 0units of y
b3b_3
Difference between the groups’ slopes on xunits of y per unit of x

Assumes. The model is additive apart from the interaction included.

The group’s own slope is b₁ + b₃; its intercept b₀ + b₂. Centre x so that b₂ describes a value of x that occurs.

Number of dummies for a categorical predictor

dummies=k−1\text{dummies} = k - 1
kk
Number of levels (categories) of the predictor
dummies\text{dummies}
Number of 0/1 columns entered with an intercept

k dummies with an intercept is the dummy-variable trap: they sum to the intercept’s column.

Two dummies and their interaction: the four group means

yˉ00=b0,yˉ10=b0+b1,yˉ01=b0+b2,yˉ11=b0+b1+b2+b3\bar y_{00} = b_0,\quad \bar y_{10} = b_0 + b_1,\quad \bar y_{01} = b_0 + b_2,\quad \bar y_{11} = b_0 + b_1 + b_2 + b_3
yˉab\bar y_{ab}
Mean of y in the group with D₁ = a and D₂ = bunits of y
b0b_0
Intercept: the mean of the reference group (both dummies 0)units of y
b1,b2b_1, b_2
Coefficients of the two dummies D₁ and D₂units of y
b3b_3
Coefficient of the product D₁ × D₂units of y

Assumes. No other predictors in the model; then the fit reproduces the four means exactly.

The effect of D₂ when D₁ = 1 is b₂ + b₃.

Worked examples

A first example: Room rates by type and day

Fictional figures

Ashmere Hotel (fictional) has three room types (standard, superior, suite) and regresses each night’s rate ($) on dummies for superior and suite (standard is the reference) and a weekend dummy: rate = 88 + 24 superior + 71 suite + 19 weekend. Interpret the coefficients, find the fitted rate for a suite on a Saturday, and find how much more a suite costs than a superior room.

  1. Name the reference: a standard room on a weekday, whose fitted rate is the intercept, $88.
  2. Read each dummy as a difference from the reference, holding the other dummy fixed: a superior room costs $24 more than a standard room on the same kind of night, a suite $71 more, and a weekend night $19 more than a weekday for the same room type.
  3. Suite on a Saturday: 88+71+19=$17888 + 71 + 19 = \$178 (superior = 0, suite = 1, weekend = 1).
  4. Suite against superior is not a coefficient: it is the difference of their two coefficients, 71−24=$4771 - 24 = \$47, the same on weekdays and weekends because the model has no interaction.

Answer. A suite on a Saturday is fitted at $178; suites cost $47 more than superior rooms on the same night.

What it means. Three room types need two dummies. If the hotel wanted superior rooms as the baseline, it would refit with standard and suite dummies and get −24 and +47, with exactly the same fitted rates.

A harder example: Two cafés, two weekends

Fictional figures

The case’s footfall model for both cafés has coefficients of 68.8 on Weekend, 157.4 on Millbrook, −232.2 on Millbrook × Weekend, 1.27 on the high, −71.8 on rain and 336.8 on Festival (intercept 39.5). Find each café’s weekend effect and the cafés’ difference on a weekend, holding the weather fixed; then explain what the model without the interaction says.

  1. Harbourside is the reference café, so its weekend effect is the Weekend coefficient: +68.8 people, holding the weather and festivals fixed.
  2. Millbrook’s weekend effect adds the interaction: 68.8+(−232.2)=−163.468.8 + (-232.2) = -163.4 people.
  3. The cafés’ difference: on a weekday it is the Millbrook coefficient, 157.4; on a weekend it adds the interaction, 157.4−232.2=−74.8157.4 - 232.2 = -74.8. At weekends Harbourside is busier than Millbrook by about 75 people.
  4. Without the interaction the single Weekend coefficient is −49.8 and R² falls from 0.904 to 0.553: the model forces one weekend effect on two cafés whose weekends go in opposite directions.

Answer. Weekend effect +68.8 people at Harbourside and −163.4 at Millbrook; on a weekend Millbrook has about 75 fewer people than Harbourside, against 157 more on a weekday.

What it means. The interaction is the finding: the cafés serve different customers (weekend visitors at the waterfront, office workers in town). A pooled model without it would mislead both managers, and its weather slope would also be estimated from badly fitted rows.

Applied: a business decision: Do busy days shrink the ticket?

Fictional figures

A regional manager proposes pushing add-ons on busy days, because “busier days have smaller tickets”: the pooled slope of the average ticket on footfall over the 612 café-days is −$0.40 per 100 people. Ana adds a Millbrook dummy, then the footfall × Millbrook interaction. Should the proposal stand on the pooled slope?

  1. Fit the pooled slope: −$0.40 per 100 people. It compares all days, including Harbourside’s days with Millbrook’s.
  2. Add the café dummy: the slope on footfall becomes +$0.25 per 100 people, and Millbrook’s tickets are $2.30 smaller at the same footfall. Holding the café fixed, busier days go with slightly larger tickets.
  3. Add the interaction: +$0.36 per 100 people at Harbourside and +$0.18 at Millbrook. Both cafés agree in sign.
  4. Explain the pooled slope: Millbrook has more people and much smaller tickets than Harbourside, so mixing the cafés produces a downward slope that neither café shows (B05’s Simpson’s paradox).

Answer. No. Within each café, busier days go with slightly larger tickets ($0.36 per 100 people at Harbourside, $0.18 at Millbrook); the negative pooled slope comes from comparing the cafés with each other.

What it means. The add-on idea may still be worth trying, but not because busy days shrink tickets; they don’t. If the manager wants to know whether add-ons raise tickets, a trial on some days and not others would answer it. Ana’s note should give the within-café slopes and say why the pooled one is misleading.

Explore

Both cafés’ 612 days. The model starts without interactions: note the Weekend coefficient. Tick Millbrook × Weekend and watch Weekend become Harbourside’s own effect, with R² jumping. Then tick High × Millbrook and work out Millbrook’s temperature slope from the two coefficients.

Loading the interactive…

The average ticket against footfall, the Simpson’s paradox of B05. Turn on the least-squares line for all days, then colour by café and look at each café’s line: the pooled slope points the other way.

Loading the interactive…

Common mistakes

  • “The Weekend coefficient is the effect of the weekend.”

    With an interaction in the model it is the weekend effect in the reference café only. Millbrook’s is the Weekend coefficient plus the interaction. Always add the interaction before quoting a group’s effect.

  • “I included a dummy for every category to be complete.”

    With an intercept, k dummies sum to the intercept’s column, so the model can’t be fitted (the dummy-variable trap). Leave one level out as the reference, or drop the intercept, but not both.

  • “The Millbrook coefficient of 206 says Millbrook has 206 more people.”

    In a model with a High × Millbrook interaction, it is the difference at a high of 0°F, a temperature the cafés never saw. Centre the temperature (High − 60) and it becomes 160.6, the difference on a 60°F weekday. The fit is identical either way.

  • “Tuesday’s dummy isn’t significant, so I’ll drop it and keep the others.”

    Dropping one weekday dummy merges Tuesday into the reference (Monday), changing what every other dummy compares with. Decide on the whole categorical variable at once: keep all its dummies, recode it (weekend versus weekday), or test the group with a nested F test.

  • “Day of the week is coded 1 to 7, so I’ll enter it as one number.”

    A single column 1–7 forces equal steps from Monday to Sunday, a straight line through the week. Millbrook’s weekdays are flat and its weekend drops by about 165 people; no line through 1–7 can describe that. Code categories with dummies.

Guided practice

Work through these with hints when you need them. Every hint and every worked solution is there to be used.

Loading four questions…

On your own

No hints this time. Answer, check, and read the feedback; the solution appears once you have answered or if you ask for it. Your first answers count toward mastery.

Loading six questions…

In practice

Dummy variables and interactions are how regression handles the categories that run through business data: region, store format, product line, channel, customer segment, month. A retailer comparing stores models sales on store size with a dummy for each format, then asks whether the payoff from extra floor space differs by format (a size × format interaction). A software company estimates whether slow replies from its support desk lost more customers on its monthly plan than on its annual one (a response-time × plan interaction). The question “does this work the same everywhere?” is an interaction question, and averaging over groups that differ, as the pooled weekend coefficient did, is one of the commonest ways regression misleads.

Fictional case

Ana’s note to her two managers: “Holding the weather and festivals fixed, a weekend day brings Harbourside about 69 more people than a weekday and costs Millbrook about 163, from a model with a café × weekend interaction. Without that interaction the model says the weekend costs both cafés about 50 people, which is wrong for both. Warm days matter more at Harbourside: about 1.4 more people per °F against 0.45 at Millbrook, after allowing for the cafés’ build-up in their first four months. And busy days don’t shrink the ticket: within each café, busier days have slightly larger tickets; the opposite pattern appears only if the cafés are pooled.”

The note reports each café’s own figure rather than the coefficients, which a manager would have to add up. It states the reference implicitly by naming both cafés, says what was held fixed, and explains in one sentence why the simpler model misleads. It doesn’t quote the Millbrook main effect at 0°F, which describes no real day.

Two habits carry over to any analysis. First, plot the group means or group slopes before fitting: crossing or diverging lines in an interaction plot are the sign that a single coefficient won’t do. Second, when interactions are in, report effects for each group with their own uncertainty rather than the raw coefficients; the ToolPak’s output gives the pieces, and the next lesson’s nested F test decides whether a whole set of interactions earns its place.

Knowledge check

Six questions, one attempt each, no hints. Score 80% or more to complete the lesson; you can take it again at any time.

Loading six questions…

Summary

  • A dummy variable is 1 for one category and 0 otherwise; its coefficient is the difference from the reference category, holding the other predictors fixed.
  • A categorical predictor with k levels needs k − 1 dummies; k dummies with an intercept is the dummy-variable trap (perfect collinearity).
  • Changing the reference level changes the coefficients, not the fitted values or R².
  • An interaction term is the product of two predictors. It lets one predictor’s effect differ by group: a group’s own effect or slope is the main effect plus the interaction.
  • At Harbourside the weekend adds about 69 people; at Millbrook it costs 68.8 − 232.2 = −163.4. Without the interaction the single Weekend coefficient, −49.8, describes neither café.
  • With an interaction in the model, each main effect is the effect at the other predictor’s reference or zero; centre numeric predictors so it describes a real value.
  • A café dummy resolves B05’s Simpson’s paradox: the ticket’s slope on footfall is −$0.40 per 100 people pooled, +$0.25 holding the café fixed, and +$0.36 and +$0.18 for each café.

Key formulas

Terms introduced

Interaction
In a regression, a term that lets the effect of one predictor depend on the value of another, usually the product of the two columns. Its coefficient is the change in one predictor’s slope per unit of the other (for a dummy, the difference between the two groups’ slopes).
Main effect
The coefficient of a predictor that also appears in an interaction: its effect when the other predictor in the interaction is zero or at its reference level.
Interaction plot
A chart of the mean of y for each combination of two factors, one line per level of one factor. Parallel lines suggest no interaction; diverging or crossing lines suggest one.

Sources

  1. Holmes, A., Illowsky, B. & Dean, S. Introductory Business Statistics 2e, section 13.4, “The Regression Equation” (dummy variables and interaction terms). OpenStax. openstax.org/books/introductory-business-statistics-2e/pages…
  2. Penn State Eberly College of Science. STAT 462: Applied Regression Analysis, Lesson 8, “Categorical Predictors”. online.stat.psu.edu/stat462/node/86
  3. Microsoft Support. “LINEST function”. support.microsoft.com/en-US/excel/functions/linest-function