Module 4 · Formula sheet

Back to the Module 4 guide.

Binary categories

Switch a group adjustment on or off. A dummy variable DD is 0 for the reference group and 1 for the other group:

Y^=β^0+β^1D.\widehat Y=\widehat\beta_0+\widehat\beta_1D.

Y^\widehat Y is the prediction; β^0\widehat\beta_0 is the reference prediction; β^1\widehat\beta_1 is the other group’s adjustment. Hats mark estimates fitted from data.

Substitute first:

D=0:Y^=β^0,D=1:Y^=β^0+β^1.\begin{aligned} D=0:&\quad\widehat Y=\widehat\beta_0,\\ D=1:&\quad\widehat Y=\widehat\beta_0+\widehat\beta_1. \end{aligned}

Invented example: Y^=40+15D\widehat Y=40+15D. Predictions are 40+15(0)=4040+15(0)=40 and 40+15(1)=5540+15(1)=55; the gap is 15.

With other predictors included, compare groups at the same input values. This constant gap assumes no interaction involving the group.

Multiple categories

One baseline; K−1K-1 switches. KK is the number of categories. For nonreference category jj, set Dj=1D_j=1 when the observation belongs to it and 0 otherwise:

Y^=β^0+∑j=1K−1β^jDj.\widehat Y=\widehat\beta_0+ \sum_{j=1}^{K-1}\widehat\beta_jD_j.

Y^\widehat Y is the prediction; β^0\widehat\beta_0 is the fitted baseline; β^j\widehat\beta_j is category jj‘s fitted adjustment. All switches are 0 for the reference.

Reference prediction=β^0,Category j prediction=β^0+β^j.\begin{aligned} \text{Reference prediction}&=\widehat\beta_0,\\ \text{Category }j\text{ prediction}&=\widehat\beta_0+\widehat\beta_j. \end{aligned}

For two nonreference categories a,ba,b, the prediction difference is β^a−β^b\widehat\beta_a-\widehat\beta_b.

Invented example: baseline 20, adjustments 5 and −3-3. Predictions: 2020, 20+5=2520+5=25, 20−3=1720-3=17. The last two differ by 5−(−3)=85-(-3)=8.

All KK switches would sum to the intercept column of ones, making the weights inseparable. Changing the reference changes coefficient meanings, but preserves predictions.

Quantitative interactions

Product = a new feature

For numerical inputs X1,X2X_1,X_2, calculate X1X2X_1X_2 and give that column its own coefficient:

Y^=β^0+β^1X1+β^2X2+β^3X1X2.\begin{aligned} \widehat Y&=\widehat\beta_0+ \widehat\beta_1X_1+\widehat\beta_2X_2\\ &\quad +\widehat\beta_3X_1X_2. \end{aligned}

Y^\widehat Y is the prediction. The fitted coefficients are intercept β^0\widehat\beta_0, main-input multipliers β^1,β^2\widehat\beta_1,\widehat\beta_2, and product multiplier β^3\widehat\beta_3.

Collect every term multiplying the input you change

Hold the other input fixed. One extra input unit changes the prediction by its slope:

Slope for X1=β^1+β^3X2,Slope for X2=β^2+β^3X1.\begin{aligned} \text{Slope for }X_1&=\widehat\beta_1+\widehat\beta_3X_2,\\ \text{Slope for }X_2&=\widehat\beta_2+\widehat\beta_3X_1. \end{aligned}

β^1\widehat\beta_1 is the X1X_1 slope at X2=0X_2=0; β^2\widehat\beta_2 is the X2X_2 slope at X1=0X_1=0. The interaction makes those slopes change.

Slide coefficients, invented input: TV coefficient 0.0191, product coefficient 0.0011, assumed radio input 20. TV’s slope is 0.0191+0.0011(20)=0.04110.0191+0.0011(20)=0.0411.

Hierarchy principle: keep X1X_1 and X2X_2 when including X1X2X_1X_2, even if their individual tests are not significant.

Coefficient / standard error / t

ReadoutMeaning
CoefficientThe fitted multiplier.
Standard error (SE)Estimated sample-to-sample variation in that multiplier.
t-statisticGap from the tested value, measured in standard errors.

To test the true interaction coefficient β3=0\beta_3=0:

t=β^3−0SE⁡(β^3).t=\frac{\widehat\beta_3-0} {\operatorname{SE}(\widehat\beta_3)}.

Under the module’s approximate two-sided rule, reject zero when ∣t∣>2|t|>2; otherwise fail to reject, including at ∣t∣=2|t|=2.

The interaction slide reports coefficient 0.0011 and t=20.73t=20.73. Since ∣20.73∣>2|20.73|>2, reject zero under the model’s assumptions. Its displayed SE of 0.000 is rounded, not exactly zero; printed values need not reproduce the reported t. Statistical evidence alone proves neither causation nor better test predictions.

Exact t-tests

For an exact test under independent mean-zero normal errors of constant variance and linearly independent design columns, compare with a t-distribution having n−p−1n-p-1 degrees of freedom. Here nn counts observations and pp counts input columns excluding the intercept; this interaction model has p=3p=3.

Category interactions

The switch can add an intercept and a slope. Let XX be a numerical input, and D=0D=0 for the reference group or 11 for the other group:

Y^=β^0+β^1X+β^2D+β^3XD.\begin{aligned} \widehat Y&=\widehat\beta_0+ \widehat\beta_1X+\widehat\beta_2D\\ &\quad +\widehat\beta_3XD. \end{aligned}

Y^\widehat Y is the prediction. β^0,β^1\widehat\beta_0,\widehat\beta_1 are the fitted reference intercept and slope. β^2\widehat\beta_2 adds to the other group’s intercept; β^3\widehat\beta_3 adds to its slope.

Substitute D=0D=0 and D=1D=1 before reading the lines:

D=0:Y^=β^0+β^1X,D=1:Y^=(β^0+β^2)+(β^1+β^3)X.\begin{aligned} D=0:\quad\widehat Y &=\widehat\beta_0+\widehat\beta_1X,\\ D=1:\quad\widehat Y &=(\widehat\beta_0+\widehat\beta_2)\\ &\quad +(\widehat\beta_1+\widehat\beta_3)X. \end{aligned}

At the same XX, other-group prediction minus reference prediction is

Group gap=β^2+β^3X.\text{Group gap}=\widehat\beta_2+\widehat\beta_3X.

The lines are parallel when β^3=0\widehat\beta_3=0.

Invented example: Y^=20+2X+10D+3XD\widehat Y=20+2X+10D+3XD. The lines are 20+2X20+2X and 30+5X30+5X. At X=4X=4, predictions are 28 and 50; the gap rule gives 10+3(4)=2210+3(4)=22.

Polynomial features

Calculate powers first; fit their weights second. For numerical input XX, use columns X,X2,…,XkX,X^2,\ldots,X^k:

Y^=β^0+∑r=1kβ^rXr.\widehat Y=\widehat\beta_0+ \sum_{r=1}^{k}\widehat\beta_rX^r.

Y^\widehat Y is the prediction; β^0\widehat\beta_0 is the fitted intercept; β^r\widehat\beta_r multiplies power rr. Degree kk is the largest power. The coefficients still multiply known columns and add: this is linear regression.

Invented example: for Y^=1+3X+2X2\widehat Y=1+3X+2X^2 at X=2X=2, the row [1,X,X2][1,X,X^2] becomes [1,2,4][1,2,4]. Prediction: 1+3(2)+2(4)=151+3(2)+2(4)=15.

Count the columns actually present. pp counts input columns excluding the intercept. A full cubic uses [1,X,X2,X3][1,X,X^2,X^3]: p=3p=3. The specified model Y^=αX2+γ\widehat Y=\alpha X^2+\gamma uses [1,X2][1,X^2]: p=1p=1 and two fitted coefficients, α\alpha and γ\gamma.

Training and test RSS

Circle TRAIN or TEST first. Training chooses the weights; test data checks those weights without refitting.

RSS is the total squared prediction error:

RSS=∑i=1n(yi−y^i)2.\mathrm{RSS}=\sum_{i=1}^{n}(y_i-\widehat y_i)^2.

nn counts observations, yiy_i is actual output ii, and y^i\widehat y_i is its prediction. Subtract, square, then add. Compare models on the same observations.

The cubic can copy the line

For ordinary least-squares fits with intercepts on the same training data, zeroing the cubic’s squared and cubed coefficients makes it a line. Therefore

RSScubic, train≤RSSlinear, train.\mathrm{RSS}_\text{cubic, train} \leq\mathrm{RSS}_\text{linear, train}.

This holds whatever generated the data; equality is possible.

True relationshipTest comparison
LinearExpect the line to do better; individual datasets can differ.
Nonlinear, amount unknownNot enough information to choose.

Training improvement gives no test guarantee: extra terms may learn real curvature or training noise.

Transformed least squares

Rename the column, reuse the line formula

To fit y^i=αxi2+γ\widehat y_i=\alpha x_i^2+\gamma, calculate zi=xi2z_i=x_i^2. Here xi,yix_i,y_i are supplied input and output for observation ii; y^i\widehat y_i is its prediction; α\alpha is the weight and γ\gamma the intercept to fit.

The model becomes y^i=αzi+γ\widehat y_i=\alpha z_i+\gamma. For nn observations, calculate means:

zˉ=1n∑i=1nzi,yˉ=1n∑i=1nyi.\bar z=\frac1n\sum_{i=1}^{n}z_i, \qquad \bar y=\frac1n\sum_{i=1}^{n}y_i.

Then calculate how the columns move together, SzyS_{zy}, and the input spread, SzzS_{zz}:

Szy=∑i=1n(zi−zˉ)(yi−yˉ),S_{zy}=\sum_{i=1}^{n}(z_i-\bar z)(y_i-\bar y), Szz=∑i=1n(zi−zˉ)2.S_{zz}=\sum_{i=1}^{n}(z_i-\bar z)^2.

For Szz>0S_{zz}>0, the least-squares coefficients are

α=SzySzz,γ=yˉ−αzˉ.\boxed{\alpha=\frac{S_{zy}}{S_{zz}}}, \qquad \boxed{\gamma=\bar y-\alpha\bar z}.

If Szz=0S_{zz}=0, every new input is identical and the slope cannot be uniquely identified.

Q6 shortcut: two distinct inputs

The supplied pairs (0,0),(1,1/2),(−1,3/2)(0,0),(1,1/2),(-1,3/2) become (0,0),(1,1/2),(1,3/2)(0,0),(1,1/2),(1,3/2) in (z,y)(z,y) coordinates.

At z=0z=0, the output mean is 0, so γ=0\gamma=0. At z=1z=1, the output mean is (1/2+3/2)/2=1(1/2+3/2)/2=1, so α+γ=1\alpha+\gamma=1 and α=1\alpha=1. The fitted line hits both group averages, minimizing squared error.

Q6 through the general formula

With n=3n=3, the means are zˉ=(0+1+1)/3=2/3\bar z=(0+1+1)/3=2/3 and yˉ=(0+1/2+3/2)/3=2/3\bar y=(0+1/2+3/2)/3=2/3.

Subtracting 2/32/3 gives centered inputs −2/3,1/3,1/3-2/3,1/3,1/3 and centered outputs −2/3,−1/6,5/6-2/3,-1/6,5/6.

Szy=(−23)(−23)+(13)(−16)+(13)(56)=49−118+518=23.\begin{aligned} S_{zy}&=(-\tfrac23)(-\tfrac23)\\ &\quad +(\tfrac13)(-\tfrac16)\\ &\quad +(\tfrac13)(\tfrac56)\\ &=\tfrac49-\tfrac1{18}+\tfrac5{18}=\tfrac23. \end{aligned} Szz=(−23)2+(13)2+(13)2=49+19+19=23.\begin{aligned} S_{zz}&=(-\tfrac23)^2+(\tfrac13)^2+(\tfrac13)^2\\ &=\tfrac49+\tfrac19+\tfrac19=\tfrac23. \end{aligned}

Thus α=(2/3)/(2/3)=1\alpha=(2/3)/(2/3)=1 and γ=2/3−1(2/3)=0\gamma=2/3-1(2/3)=0.

Check: Y^=X2\widehat Y=X^2 predicts 0,1,10,1,1 at original inputs 0,1,−10,1,-1. Subtract those predictions from supplied outputs 0,1/2,3/20,1/2,3/2:

RSS=02+(−12)2+(12)2=12.\mathrm{RSS}=0^2+(-\tfrac12)^2+(\tfrac12)^2=\tfrac12.

The general formula works for any specified new column zi=g(xi)z_i=g(x_i), where gg is the rule for calculating it.