Machine LearningModule 4 · Guide and practice
Module 4 · Categories, interactions, and curves
One picture: a prediction is a starting value plus a few contributions. This module changes what those contributions can be: a group adjustment, a changing slope, or a curve.
Slides → small steps → practice. Browse the original slides above each topic, or click to enlarge. Keep the formula sheet nearby.
1. The same machine, different inputs
Section titled “1. The same machine, different inputs”Coefficient = multiplier
Section titled “Coefficient = multiplier”In Module 3, the model was
is the outcome; is an input. The intercept is the starting value. The coefficient multiplies the input. Noise is the variation the rule does not explain.
Hat = estimate from data
Section titled “Hat = estimate from data”The true coefficients are unknown. We fit estimates, marked with hats, and predict without adding unknown noise:
With input columns, add their contributions:
selects a column; excludes the intercept. A column can contain a switch, a product, or a square. Least squares still chooses the weights that minimize total squared prediction error.
2. Two categories: use a switch
Section titled “2. Two categories: use a switch”
0 means off; 1 means on
Section titled “0 means off; 1 means on”A dummy variable, or indicator, switches a group adjustment on or off. Let for a student and for a nonstudent:
The rule is starting value + adjustment × switch:
Substitute the switch before interpreting
Section titled “Substitute the switch before interpreting”| Group | Substitute | Prediction |
|---|---|---|
| Nonstudent | ||
| Student |
The group coded 0 is the reference or baseline. predicts that group; is the student-minus-nonstudent difference.
Invented example: dollars. Nonstudents get ; students get .
With only this switch and an intercept, least squares predicts each group’s sample average. With other inputs included, compare groups at the same values of those inputs.
Try it — invented: in , means premium membership. Predict for both groups. Rule: binary categories.
Show the answer
Regular: , so . Premium: , so . A negative adjustment lowers the prediction by 8.
3. Several categories: one baseline, the rest switches
Section titled “3. Several categories: one baseline, the rest switches”
One group supplies the starting value
Section titled “One group supplies the starting value”For Basic, Plus, and Pro plans, choose Basic as the reference. Define for Plus and for Pro; otherwise each is 0.
and are fitted adjustments for Plus and Pro.
| Plan | Plus switch | Pro switch | Prediction |
|---|---|---|---|
| Basic | 0 | 0 | |
| Plus | 1 | 0 | |
| Pro | 0 | 1 |
Remember K − 1
Section titled “Remember K − 1”categories need switches when there is an intercept. All switches are off for the reference. Coding the groups instead would impose equally spaced predictions.
Invented coefficients: baseline 20, Plus adjustment 5, Pro adjustment . Predictions are , , and . Plus exceeds Pro by .
Changing the reference changes the coefficient meanings, but preserves the fitted predictions.
Why leave one switch out?
Exactly one of all group switches is on in each row. They therefore add to 1, duplicating the intercept column of ones. Leaving one out lets the model separate the weights.
4. Interactions: one input changes another’s effect
Section titled “4. Interactions: one input changes another’s effect”
Multiply two inputs to make a new column
Section titled “Multiply two inputs to make a new column”Let be TV spending, radio spending, and predicted sales. An additive model gives each input a fixed slope:
An interaction lets one input change the other’s slope. Literally calculate a new feature, , and give it a coefficient:
Read the fitted multipliers
Section titled “Read the fitted multipliers”These numbers come from slide 3 of the interaction deck. Each coefficient is a fitted multiplier; the intercept is the baseline.
| Term | Coefficient | Std. Error | t |
|---|---|---|---|
| Intercept | 6.7502 | 0.248 | 27.23 |
| TV | 0.0191 | 0.002 | 12.70 |
| Radio | 0.0289 | 0.009 | 3.24 |
| TV × Radio | 0.0011 | 0.000 | 20.73 |
Calculate each contribution, then add
Section titled “Calculate each contribution, then add”Invented inputs: take , in the model’s input units. Use the slide’s fitted coefficients above. The new product is .
- Intercept: start at .
- TV contribution: .
- Radio contribution: .
- Product contribution: .
Prediction = sum of the four contributions:
Group the TV terms to find its slope
Section titled “Group the TV terms to find its slope”Hold fixed. Everything multiplying becomes its slope:
Using the slide’s , and our assumed :
One extra TV unit now adds 0.0411 predicted sales units. The coefficient 0.0191 is TV’s slope only when radio spending is 0.
Similarly, radio’s slope is . A positive interaction makes either slope increase with the other input; a negative interaction decreases it.
Hierarchy principle: keep and when including , even if their individual tests are not significant. Keep the starting effects when modeling how they change.
Standard error = how much the estimate might bounce
Section titled “Standard error = how much the estimate might bounce”Collect another sample, refit, and the coefficient will change. Std. Error, or SE, estimates the size of that sample-to-sample variation.
t = effect divided by uncertainty
Section titled “t = effect divided by uncertainty”To test the true interaction coefficient , ask: how many standard errors is the estimate from zero?
Use the module’s approximate two-sided rule: reject zero when ; otherwise fail to reject, including at .
Here : strong evidence of a nonzero interaction under the model’s assumptions. Statistical evidence alone proves neither causation nor improved predictions on new data.
Read rounded tables carefully. SE printed as 0.000 means a tiny positive value rounded down. It does not mean zero uncertainty. The printed values cannot reproduce the exact t-statistics.
How small is that interaction SE?
Rearrange to get . Using the rounded coefficient 0.0011 and reported gives approximately . This is an approximate reconstruction, not the original unrounded SE.
5. Category interactions: let groups have different slopes
Section titled “5. Category interactions: let groups have different slopes”
First switch on a different starting value
Section titled “First switch on a different starting value”Let be income, for students and for nonstudents. For predicted balance :
shifts the student starting value. Both groups still have slope : parallel lines.
Then switch on an extra slope
Section titled “Then switch on an extra slope”Add : it equals 0 for nonstudents and for students.
Substitute first; read second:
changes the intercept; changes the slope. At income , the student-minus-nonstudent gap is .
Try it — invented: , with balance in dollars and income in thousands of dollars. Find both lines, then predict at . Rule: category interactions.
Show the answer
- : . At , dollars.
- : . At , dollars.
The gap rule gives , matching . For parallel lines, the interaction coefficient would have to be 0.
For groups, multiply each of the switches by to allow a separate slope per group.
6. Curves are still possible in linear regression
Section titled “6. Curves are still possible in linear regression”
A curve starts with calculated columns
Section titled “A curve starts with calculated columns”To model curvature, such as fuel efficiency changing with horsepower, add as a column:
This is quadratic. A cubic adds ; degree means powers through .
Linear in the weights
Section titled “Linear in the weights”The inputs are squared or cubed. The fitted weights still multiply known columns and add, so this remains linear regression.
Invented example: at , the columns become . With weights :
Remember: calculate columns first; fit weights second. Extra terms can follow a real curve or random training bumps. Check new data to tell whether they help.
7. Shared practice Q4: straight line versus cubic
Section titled “7. Shared practice Q4: straight line versus cubic”View the original practice sheet (includes solutions)
Given: 100 observations, one original input , and numerical outcome . Fit both models by ordinary least squares on the same training data:
and are separate fitted coefficients. RSS is the total squared miss: , using actual output and prediction for observation .
Circle TRAIN or TEST first: training data chooses the weights; test data checks them without refitting.
- Linear truth: which has smaller training RSS?
- Linear truth: which do you expect to have smaller test RSS?
- Nonlinear truth: which has smaller training RSS?
- Nonlinear truth, amount unknown: which has smaller test RSS?
Rules: polynomial features and training versus test RSS.
The move to remember: switch off the extra terms
Section titled “The move to remember: switch off the extra terms”Set , then and . The cubic now copies the line exactly. Its best training fit therefore cannot be worse:
Answer all four cases
1. Linear truth, training: cubic RSS is smaller or equal. It can copy the line and improve on it if extra terms help. Equality is possible.
2. Linear truth, test: expect the line to do better. It has the right shape already; extra terms may learn training noise. This expectation is not a guarantee for every dataset.
3. Nonlinear truth, training: cubic RSS is again smaller or equal. The ability to copy the line does not depend on what generated the data.
4. Nonlinear truth, test: not enough information. Extra terms might capture useful curvature or mostly noise. Compare on the same held-out observations, without refitting.
The cubic has input columns, , and four coefficients including its intercept.
8. Shared practice Q6: fit a quadratic by renaming a column
Section titled “8. Shared practice Q6: fit a quadratic by renaming a column”View the original practice sheet (includes solutions)
Given: fit to , , and . Find minimizing RSS. Here weights , and is the fitted intercept. Count input column, ; there is no term.
Rules: transformed least squares and RSS.
Same squared input → same prediction
Section titled “Same squared input → same prediction”Set , where selects an observation. The model becomes a line, :
| Supplied | Supplied | Calculated |
|---|---|---|
Squaring makes and identical inputs. Squared error favors their average output as their shared prediction.
Fast solution: two input values, two group averages
At : the only output is 0. Since , choose .
At : the average output is . Since , choose .
Thus . This line hits both group averages, so it minimizes the total squared error.
Check RSS. At original inputs , predictions are . Subtract predictions from the supplied outputs :
The two outputs at differ, so one shared prediction cannot hit both.
The general rule when that shortcut is unavailable
Section titled “The general rule when that shortcut is unavailable”For observations, calculate averages and . Then use the usual straight-line rules:
The numerator measures how the columns move together; the denominator measures input spread:
Verify Q6 with the general calculation
Means: , new inputs , and supplied outputs give
Center: subtract from each column:
| Row | ||
|---|---|---|
| 1 | ||
| 2 | ||
| 3 |
Multiply and add for :
Square and add for :
Divide to find the slope: .
Find the intercept: . Both answers match the shortcut.
9. The five questions to ask
Section titled “9. The five questions to ask”- Group label? Switch its adjustment on or off.
- groups? One reference, switches.
- Product term? Group terms to read the changing slope.
- Power of an input? Calculate the column, then fit its weight.
- More flexible model? Training can improve; test performance must be checked.
Open the formula sheet to reuse the rules with different numbers.