Tutorials › Exam PA - Predictive Analytics › Worked Example With Group-Specific Slopes

Exam PA · Categorical Encoding and Interactions · Tutorial 261 of 966

Worked Example With Group-Specific Slopes

Use a fitted interaction model to derive each category’s prediction line and compare its predictions at meaningful values of a numeric predictor.

Intermediate 11 min read

What You'll Learn

  • Fit a factor-by-numeric interaction using R’s formula syntax.
  • Identify the reference group and interpret the model coefficients.
  • Derive a separate prediction equation for every group.
  • Compare fitted slopes and group differences at selected predictor values.
  • Check calculations against the fitted model and recognize when a comparison is extrapolation.

Why Group-Specific Slopes Matter

A numeric predictor can have a different relationship with the outcome in different categories. For example, in a hypothetical insurance portfolio, the association between a policyholder’s years of tenure and predicted annual service cost might differ between two plan types. A model with only a group indicator allows the groups to have different intercepts, but it forces them to have the same slope. A factor-by-numeric interaction allows both the intercepts and slopes to differ.

The essential interpretation is made group by group: write the fitted prediction equation for each category, identify its slope, and calculate predictions at the values of interest. A coefficient on the interaction term is a difference in slopes, not a prediction by itself.

Definition: A factor-by-numeric interaction is a regression term that allows the fitted relationship between a numeric predictor \(x\) and an outcome \(Y\) to vary across levels of a categorical predictor \(G\). With treatment coding, one factor level is the reference group. Its intercept and slope are represented by the model’s intercept and numeric-predictor coefficient; the other levels’ intercept and slope adjustments are represented by factor and interaction coefficients.

Model Form and R Syntax

Suppose \(G\) has two levels, with \(G=0\) denoting the reference group and \(G=1\) denoting another group. A linear regression with an interaction has fitted conditional mean

$$ E[Y\mid x,G] = \beta_0+\beta_1x+\beta_2G+\beta_3(xG). $$

The fitted value replaces each coefficient by its estimate. For the reference group, set \(G=0\); for the other group, set \(G=1\). This produces

$$ \begin{aligned} \widehat{Y}_{G=0} &= \widehat{\beta}_0+\widehat{\beta}_1x,\\ \widehat{Y}_{G=1} &= (\widehat{\beta}_0+\widehat{\beta}_2) +(\widehat{\beta}_1+\widehat{\beta}_3)x. \end{aligned} $$

Thus, \(\widehat{\beta}_2\) is the difference in fitted intercepts, and \(\widehat{\beta}_3\) is the difference in fitted slopes, comparing the non-reference group with the reference group. The intercept difference is the group difference at \(x=0\). If zero is not meaningful or is far outside the data range, that intercept comparison may not be substantively useful; the fitted predictions at relevant values of \(x\) can still be compared.

In R, the formula `outcome ~ group * x` includes the group main effect, the numeric main effect, and their interaction. The equivalent expanded formula is `outcome ~ group + x + group:x`. For example, use `lm(cost ~ plan * tenure, data = d)`. Set the intended reference level explicitly when needed: `d$plan <- relevel(factor(d$plan), ref = "Basic")`. With more than two factor levels, treatment coding compares each non-reference level with the reference; each comparison has its own intercept and slope adjustment.

The model is linear in \(x\) within each group, with a group-specific intercept and slope. The coefficient algebra below describes the fitted model regardless of whether the model is a good description of the data. For ordinary least squares inference, assumptions such as an appropriate conditional-mean specification, independent errors, and (for conventional standard errors and tests) suitable error variance and distribution conditions also matter. Do not infer that a group effect is causal merely because an interaction is fitted.

Deriving the Group Equations

The group equations follow directly from substituting the indicator for each group. This substitution is also a reliable way to avoid losing an interaction contribution.

1
Write the fitted model.
For two groups, use \(\widehat{Y}=\widehat{\beta}_0+\widehat{\beta}_1x+\widehat{\beta}_2G+\widehat{\beta}_3xG\).
2
Substitute the reference indicator.
For \(G=0\), both terms containing \(G\) are zero, leaving intercept \(\widehat{\beta}_0\) and slope \(\widehat{\beta}_1\).
3
Substitute the other-group indicator.
For \(G=1\), collect the constant terms and the terms multiplying \(x\). The intercept becomes \(\widehat{\beta}_0+\widehat{\beta}_2\), and the slope becomes \(\widehat{\beta}_1+\widehat{\beta}_3\).

The difference between the two fitted predictions at the same \(x\) is therefore

$$ \widehat{Y}_{G=1}-\widehat{Y}_{G=0} =\widehat{\beta}_2+\widehat{\beta}_3x. $$

This equation explains why the category difference can change as \(x\) changes. If \(\widehat{\beta}_3\ne 0\), the difference is not constant. If the fitted lines cross, the crossing value solves \(\widehat{\beta}_2+\widehat{\beta}_3x=0\), provided that equation has a solution in the range where predictions are relevant.

Worked Example: Two Plan Types and Tenure

Hypothetical scenario. An analyst fits a linear model for annual service cost in dollars using plan type and policy tenure in years. Basic is the reference plan. The fitted coefficients are \(\widehat{\beta}_0=50\), \(\widehat{\beta}_1=3\), \(\widehat{\beta}_{\text{Plus}}=8\), and \(\widehat{\beta}_{\text{Plus:tenure}}=-1.2\). In R, the model is fit with `lm(cost ~ plan * tenure, data = d)`. Derive both prediction equations and compare them at 5 years.

Method. Let \(G=1\) for Plus and \(G=0\) for Basic. The fitted model is \(\widehat{Y}=50+3x+8G-1.2xG\).

Basic equation. Set \(G=0\): \(\widehat{Y}_{\text{Basic}}=50+3x\). Its fitted intercept is 50 dollars and its fitted slope is 3 dollars per year of tenure.

Plus equation. Set \(G=1\): \(\widehat{Y}_{\text{Plus}}=50+3x+8-1.2x=(50+8)+(3-1.2)x=58+1.8x\). Its fitted intercept is 58 dollars and its fitted slope is 1.8 dollars per year.

At \(x=5\), Basic is \(50+3(5)=50+15=65\) dollars. Plus is \(58+1.8(5)=58+9=67\) dollars. The Plus-minus-Basic difference is \(67-65=2\) dollars. Checking with the difference formula gives \(8-1.2(5)=8-6=2\) dollars, so the calculations agree.

Common mistake avoided: Treating the interaction coefficient \(-1.2\) as the Plus slope. It is the change in slope from Basic to Plus; the Plus slope is \(3-1.2=1.8\).

More Than Two Factor Levels

With three or more levels, apply the same substitution separately to each level. For example, with Base as reference and levels Plus and Max, the model can be written as

$$ \widehat{Y}=\widehat{\beta}_0+\widehat{\beta}_1x +\widehat{\beta}_{P}I_P+\widehat{\beta}_{M}I_M +\widehat{\beta}_{Px}xI_P+\widehat{\beta}_{Mx}xI_M, $$

where \(I_P\) and \(I_M\) equal 1 for their named level and 0 otherwise. Base has intercept \(\widehat{\beta}_0\) and slope \(\widehat{\beta}_1\); Plus adds \(\widehat{\beta}_P\) to the intercept and \(\widehat{\beta}_{Px}\) to the slope; Max adds the corresponding Max coefficients. In R, the coefficient names depend on the factor labels and contrast coding, so inspect the fitted model and confirm the reference level before assigning meanings to coefficients.

Worked Example: Three Groups at a Selected Predictor Value

Hypothetical scenario. A model predicts a numerical renewal score from years of account history and a three-level service tier. Base is the reference. Suppose the fitted coefficients are: intercept 24, history slope 5, Plus main effect 6, Max main effect \(-4\), Plus-by-history coefficient \(-1\), and Max-by-history coefficient 2. Find all three equations, predict at 4 years, and identify the highest fitted prediction.

Base. As the reference group, Base has \(\widehat{Y}_{B}=24+5x\). At \(x=4\), its prediction is \(24+5(4)=24+20=44\).

Plus. Its intercept is \(24+6=30\), and its slope is \(5+(-1)=4\). Thus \(\widehat{Y}_{P}=30+4x\). At \(x=4\), its prediction is \(30+4(4)=30+16=46\).

Max. Its intercept is \(24+(-4)=20\), and its slope is \(5+2=7\). Thus \(\widehat{Y}_{M}=20+7x\). At \(x=4\), its prediction is \(20+7(4)=20+28=48\).

The predictions are Base 44, Plus 46, and Max 48, so Max has the highest fitted prediction at 4 years. As a check, the Max-minus-Base difference is \(-4+2(4)=-4+8=4\), and \(48-44=4\). The Plus-minus-Base difference is \(6-1(4)=2\), and \(46-44=2\).

Common mistake avoided: Comparing only the group main-effect coefficients. Those coefficients are differences at \(x=0\), not at 4 years; the interaction terms also affect the comparison at 4 years.

Worked Example: Finding and Checking a Crossover

Hypothetical scenario. A fitted model compares two hypothetical product segments. Segment Silver is the reference, with intercept 70 and slope 2.5. The Gold main-effect coefficient is \(-10\), and the Gold-by-\(x\) interaction coefficient is 1.5. Derive the equations, compare predictions at \(x=6\), and determine whether the fitted lines cross at a positive \(x\).

Equations. Silver has \(\widehat{Y}_{S}=70+2.5x\). Gold has intercept \(70-10=60\) and slope \(2.5+1.5=4\), so \(\widehat{Y}_{G}=60+4x\).

At \(x=6\), Silver predicts \(70+2.5(6)=70+15=85\). Gold predicts \(60+4(6)=60+24=84\). Gold minus Silver is \(84-85=-1\).

The difference equation gives the same result: \(-10+1.5x\). Setting it to zero gives \(-10+1.5x=0\), so \(1.5x=10\) and \(x=10/1.5=6.6667\) to four decimal places. The difference is negative at 6 and becomes positive above approximately 6.6667, so the fitted Gold line is lower at 6 and higher beyond the crossover. Whether this crossing is useful depends on whether that predictor value is supported by the observed data.

Common mistake avoided: Adding the interaction coefficient to the group intercept, or using the group main effect as the prediction difference at every \(x\). The interaction adjusts the slope, and the difference here is \(-10+1.5x\).

Practical Checks Before Reporting a Comparison

A reliable interpretation keeps four quantities distinct: the reference intercept, the reference slope, the group intercept adjustment, and the group slope adjustment. Write the group equation before substituting a selected value. Then calculate the prediction for each group and, if relevant, calculate the difference directly from the difference equation as an independent check.

QuantityMeaning in the two-group model
\(\widehat{\beta}_0\)Reference-group fitted value at \(x=0\)
\(\widehat{\beta}_1\)Reference-group change in fitted value for a one-unit increase in \(x\)
\(\widehat{\beta}_2\)Other-minus-reference fitted difference at \(x=0\)
\(\widehat{\beta}_3\)Other-minus-reference difference in fitted slopes

The unit of the slope depends on the units of both variables: it is outcome units per one unit of \(x\). If tenure is recorded in years, a slope is per year; if it is recorded in months, the numerical slope will use a different scale. Confirm the coding and units used in the fitted data before explaining a result.

Finally, fitted lines describe model-based conditional means, not guaranteed individual outcomes. A prediction at a value far outside the data range may be extrapolation, and a numerical difference is not automatically statistically significant or causally interpretable. Those conclusions require additional analysis and assumptions beyond reading the coefficient arithmetic.

Key takeaway: For each factor level, add that level’s main-effect coefficient to the reference intercept and its interaction coefficient to the reference slope. Use the resulting group equations to calculate and compare predictions at the specified \(x\).

Check Your Understanding

For each problem, write the group equations before calculating the requested comparison.

  1. A fitted model uses North as the reference group, with intercept 18 and slope 2.4. South has main-effect coefficient 5 and interaction coefficient \(-0.4\). Find both predictions at \(x=10\) and the South-minus-North difference. (Answer: North \(42\); South \(43\); difference \(1\).)
  2. A model has reference-group equation \(\widehat{Y}_A=40+3x\). Group B has main-effect coefficient \(-3\) and interaction coefficient \(0.8\). Find B’s equation and compare the predictions at \(x=5\). (Answer: B \(\widehat{Y}=37+3.8x\); A \(55\); B \(56\); B-minus-A difference \(1\).)
  3. For Base, the fitted equation is \(12+2x\). Group C has main-effect coefficient 4 and interaction coefficient \(-0.5\); Group D has main-effect coefficient \(-2\) and interaction coefficient 1. Find all predictions at \(x=4\) and state which group or groups have the largest prediction. (Answer: Base \(20\); C \(22\); D \(22\); C and D tie for the largest prediction.)
  4. Reference group R has equation \(\widehat{Y}_R=80+1.5x\). Group S has main-effect coefficient \(-12\) and interaction coefficient 2. Find the crossing value and compare the predictions at \(x=4\) and \(x=8\). (Answer: S is \(68+3.5x\); crossing at \(x=6\); at \(x=4\), R \(86\), S \(82\); at \(x=8\), R \(92\), S \(96\).)
Clinical Biostats