Tutorials › Exam PA - Predictive Analytics › Factor-by-Continuous Interactions

Exam PA · Coefficient Interpretation and Diagnostics · Tutorial 342 of 966

Factor-by-Continuous Interactions

Calculate how a continuous predictor’s effect varies by factor group, on both the link scale and the response scale.

Intermediate 11 min read

What You'll Learn

  • Write a factor-by-continuous interaction using reference-category coding.
  • Calculate the link-scale slope for each factor level.
  • Find predicted means at specified predictor values and factor levels.
  • Distinguish link-scale slopes from response-scale derivatives and finite changes.
  • Interpret interactions under log and logit links.
  • Identify common errors involving reference categories, interaction terms, and scales.

Why a Factor-by-Continuous Interaction Matters

A GLM without an interaction between a factor and a continuous predictor uses the same link-scale slope for every factor group. That assumption may be inappropriate. For example, the association between a policyholder’s years of experience and expected claim count might differ by vehicle type. A factor-by-continuous interaction allows the fitted slope to vary by group.

Interpreting the interaction requires keeping two scales distinct. The model’s linear predictor describes changes on the link scale; the response-scale mean is obtained by applying the inverse link. A group-specific link-scale slope is therefore not generally the same as a constant change in the expected outcome.

Definition: Let \(A\) be a binary factor, coded \(0\) for the reference group and \(1\) for a comparison group, and let \(x\) be a continuous predictor. A GLM with a factor-by-continuous interaction has conditional mean \(\mu=E[Y\mid A,x]\), link \(g\), and linear predictor $$ g(\mu)=\eta=\beta_0+\beta_1x+\beta_2A+\beta_3Ax. $$ The coefficient \(\beta_3\) is the change in the link-scale slope when moving from the reference group to the comparison group.

This expression assumes that, with other predictors held fixed, the linear predictor is linear in \(x\) and includes the displayed main effects and interaction. The GLM also specifies a response distribution and link; the model’s conditional mean must be compatible with that family and link. The formula does not describe a nonlinear relationship in \(x\) unless additional terms, such as transformations or polynomial terms, are included.

Finding Group-Specific Slopes and Predictions

Set the factor indicator to each group’s value. For the reference group, \(A=0\), so the predictor is \(\eta_0(x)=\beta_0+\beta_1x\). For the comparison group, \(A=1\), so it is \(\eta_1(x)=(\beta_0+\beta_2)+(\beta_1+\beta_3)x\). Thus, the slopes with respect to \(x\) on the link scale are \(\beta_1\) and \(\beta_1+\beta_3\), respectively.

$$ \frac{d\eta_0}{dx}=\beta_1,\qquad \frac{d\eta_1}{dx}=\beta_1+\beta_3,\qquad \eta_1(x)-\eta_0(x)=\beta_2+\beta_3x. $$

The main-effect coefficient \(\beta_2\) is the groups’ link-scale difference specifically at \(x=0\), not necessarily their difference at every \(x\). The interaction changes that difference as \(x\) changes. If \(x=0\) is outside the meaningful range, centering \(x\) at a useful value can make the main-effect interpretation more relevant; it does not remove the interaction or change fitted predictions when the model is re-expressed consistently.

Worked Example: Different Claim-Count Slopes by Vehicle Type

Model and assumptions. In a hypothetical portfolio, let \(Y\) be a policy’s claim count over one year. Assume \(Y\mid A,x\) has a Poisson distribution with parameter \(\mu\), so \(E[Y\mid A,x]=\mu\), and use a log link. Let \(A=0\) denote one vehicle type and \(A=1\) another. The fitted model is \(\log(\mu)=\log(2)+\log(2)x+\log(1.5)A+\log(1.25)Ax\). Calculate each group’s link-scale slope, then compare predicted means at \(x=2\) and the effect of increasing \(x\) by one unit from there.

Method. Substitute each factor value, identify its slope, and exponentiate the resulting linear predictor to obtain the Poisson mean.

For \(A=0\), \(\log(\mu_0)=\log(2)+\log(2)x\). Its link-scale slope is \(\log(2)\), corresponding to a one-unit multiplicative factor of \(e^{\log(2)}=2\) in the mean.

For \(A=1\), \(\log(\mu_1)=[\log(2)+\log(1.5)]+[\log(2)+\log(1.25)]x=\log(3)+\log(2.5)x\). Its link-scale slope is \(\log(2.5)\), and its one-unit mean multiplier is \(e^{\log(2.5)}=2.5\).

At \(x=2\), the reference-group mean is \(\mu_0=e^{\log(2)+2\log(2)}=2(2^2)=8\). The comparison-group mean is \(\mu_1=e^{\log(3)+2\log(2.5)}=3(2.5^2)=18.75\).

Increasing \(x\) from 2 to 3 multiplies the reference mean by 2, giving \(8(2)=16\), an increase of \(16-8=8\). For the comparison group, the multiplier is 2.5, giving \(18.75(2.5)=46.875\), an increase of \(46.875-18.75=28.125\).

Check. The means at \(x=3\) also follow directly from the model: \(\mu_0=2(2^3)=16\), and \(\mu_1=3(2.5^3)=46.875\). The ratio between consecutive means in each group matches its stated one-unit multiplier. The common mistake avoided here is treating the log-scale slope as an additive change in claim count.

From Link-Scale Slopes to Response-Scale Changes

For a differentiable inverse link \(g^{-1}\), the response mean is \(\mu=g^{-1}(\eta)\). Holding the factor group and other predictors fixed, the chain rule gives the response-scale derivative as the derivative of the inverse link multiplied by the group’s link-scale slope. This is a local rate of change at the specified predictor value, not a finite change over an interval.

$$ \frac{d\mu}{dx} = \frac{d\,g^{-1}(\eta)}{d\eta}\frac{d\eta}{dx} = (g^{-1})'(\eta)\,s_A, \qquad s_A= \begin{cases} \beta_1,&A=0,\\ \beta_1+\beta_3,&A=1. \end{cases} $$

This follows because \(\mu\) depends on \(x\) through \(\eta\): a small change in \(x\) first changes \(\eta\) at rate \(s_A\), and the inverse link converts that link-scale change into a change in \(\mu\). Unless \((g^{-1})'(\eta)\) is constant, the response-scale derivative also depends on the current fitted value.

For a log link, \(\mu=e^\eta\), so \((g^{-1})'(\eta)=e^\eta=\mu\) and \(d\mu/dx=\mu s_A\). Over a finite change \(\Delta x\), the mean ratio within a group is \(e^{s_A\Delta x}\), and the additive change is \(\mu(x)\{e^{s_A\Delta x}-1\}\). For a logit link, \(p=\mu=1/(1+e^{-\eta})\), and \((g^{-1})'(\eta)=p(1-p)\). The probability derivative is therefore \(p(1-p)s_A\); a finite probability change must instead be found by calculating both endpoint probabilities.

Worked Example: Probability Changes Under a Logit Interaction

Model and assumptions. In a hypothetical study, \(Y\) indicates whether a submitted application is approved. Assume \(Y\mid A,x\) is Bernoulli with probability \(p\), and use the logit link: \(\operatorname{logit}(p)=-2+0.5x+1A-0.2Ax\). Calculate both groups’ link-scale slopes and their predicted probabilities at \(x=2\). Then calculate each probability’s local response-scale derivative there and the comparison group’s finite probability change from \(x=2\) to \(x=3\).

Method. Substitute the group indicator into the linear predictor. Apply the inverse-logit function for predictions and \(p(1-p)s_A\) for the local derivative.

For \(A=0\), the predictor is \(\eta_0=-2+0.5x\), with slope \(s_0=0.5\). At \(x=2\), \(\eta_0=-2+0.5(2)=-1\), so \(p_0=1/(1+e^1)=0.2689\).

For \(A=1\), the predictor is \(\eta_1=-2+1+(0.5-0.2)x=-1+0.3x\), with slope \(s_1=0.3\). At \(x=2\), \(\eta_1=-1+0.3(2)=-0.4\), so \(p_1=1/(1+e^{0.4})=0.4013\).

At the reference-group prediction, the local derivative is \(0.5(0.2689)(1-0.2689)=0.0983\). At the comparison-group prediction, it is \(0.3(0.4013)(1-0.4013)=0.0721\). These are local probability changes per one unit of \(x\) at \(x=2\), not exact one-unit changes.

For the comparison group at \(x=3\), \(\eta_1=-1+0.3(3)=-0.1\), so \(p_1(3)=1/(1+e^{0.1})=0.4750\). Its finite increase from \(x=2\) is \(0.4750-0.4013=0.0737\), to four decimal places. This is different from the local derivative \(0.0721\).

Check. The comparison group’s link-scale predictor increases by \(0.3\) from \(-0.4\) to \(-0.1\), as its slope requires. The probability remains between 0 and 1. The common mistake avoided is reporting \(0.3\) as a probability increase; it is a log-odds slope.

More Than Two Factor Levels

With a factor having \(K\) levels, treatment coding chooses one reference level and represents each other level with an indicator. The model includes an indicator and its interaction with \(x\) for each non-reference level. If level \(j\) has main-effect coefficient \(\gamma_j\) and interaction coefficient \(\delta_j\), its predictor is \((\beta_0+\gamma_j)+(\beta_1+\delta_j)x\). Its link-scale slope is \(\beta_1+\delta_j\); the reference-level slope remains \(\beta_1\).

In R, a formula such as outcome ~ group * x includes the main effects of group and x, as well as their interaction. With a factor, the numerical coefficients depend on the chosen reference level and coding. Predictions for a given group and \(x\) do not depend on which reference level is used, provided the model is represented consistently.

Worked Example: Slopes Across Three Service Zones

Model and assumptions. A hypothetical study models annual service expense \(Y\) in dollars by service zone and \(x\), a workload measure. Assume \(Y\mid\text{zone},x\) is normally distributed with mean \(\mu\) and variance \(400\) dollars squared, and use an identity link, so \(\mu=E[Y\mid\text{zone},x]\). Zone A is the reference. The model is \(\mu=100+4x+20B-3Bx-10C+2Cx\), where \(B=1\) for Zone B and \(C=1\) for Zone C; both are zero for Zone A. Find each zone’s slope and predicted mean at \(x=5\).

Method. Substitute the appropriate indicator values, then read the coefficient of \(x\) in each group-specific mean. Under the identity link, that coefficient is both the link-scale slope and the response-scale slope.

For Zone A, \(B=C=0\), so \(\mu_A=100+4x\). Its slope is \(4\) dollars per workload unit, and at \(x=5\), \(\mu_A=100+4(5)=120\) dollars.

For Zone B, \(B=1,C=0\), so \(\mu_B=(100+20)+(4-3)x=120+x\). Its slope is \(1\) dollar per workload unit, and at \(x=5\), \(\mu_B=120+5=125\) dollars.

For Zone C, \(B=0,C=1\), so \(\mu_C=(100-10)+(4+2)x=90+6x\). Its slope is \(6\) dollars per workload unit, and at \(x=5\), \(\mu_C=90+6(5)=120\) dollars.

Check. Substitution into the original expression gives Zone B \(100+20+20-15=125\), and Zone C \(100+20-10+10=120\). The equal predicted means for A and C at \(x=5\) do not imply equal slopes: their slopes are 4 and 6. The common mistake avoided is inferring equal group effects from predictions that happen to match at one predictor value.

Interpretation Traps

  • Omitting the interaction coefficient. The comparison-group slope is \(\beta_1+\beta_3\), not \(\beta_3\) alone. The interaction coefficient is the difference between group slopes.
  • Using \(\beta_2\) as the group difference at every \(x\). The link-scale difference is \(\beta_2+\beta_3x\); \(\beta_2\) applies at \(x=0\).
  • Confusing scales. Under a log link, a slope is a change in log mean per unit, and its exponential is a mean multiplier. Under a logit link, a slope is a change in log odds, not a probability change.
  • Treating a derivative as a finite change. The derivative is local. For a finite interval, calculate predictions at both endpoints and subtract them (or take their ratio when the target is a ratio).
  • Ignoring units. A slope is per one unit of \(x\). If \(x\) is measured in hundreds of units, that is not the same increment as one individual unit.
Key takeaway: First calculate the slope for each group on the link scale. Then use the specified inverse link to obtain predicted means and response-scale changes. The interaction changes the link-scale slope; the link determines how that change appears in the response.

Check Your Understanding

For each problem, calculate on the stated scales and check that your answer respects the model’s link.

  1. A Poisson GLM with log link has \(\log(\mu)=\log(3)+\log(1.2)x+\log(2)A+\log(1.5)Ax\). Find both groups’ link-scale slopes and the mean for \(A=1\) at \(x=2\). (Answer: slopes \(\log(1.2)\) and \(\log(1.8)\); comparison-group mean \(19.44\).)
  2. A Bernoulli GLM with logit link has \(\operatorname{logit}(p)=-1+0.4x-0.5A+0.2Ax\). Find the comparison-group slope, probability at \(x=1\), and local probability derivative there. (Answer: slope \(0.6\); \(p=0.2891\); derivative \(0.1233\).)
  3. An identity-link model has mean \(\mu=50+2x+8A-0.5Ax\). Find both group slopes and the group means at \(x=4\). (Answer: slopes \(2\) and \(1.5\); means \(58\) and \(64\).)
  4. A log-link model has \(\log(\mu)=\log(4)+0.1x+\log(1.5)A-0.05Ax\). For \(A=1\), find the slope, mean at \(x=2\), and exact mean change from \(x=2\) to \(x=4\). (Answer: slope \(0.05\); mean \(6e^{0.1}\approx6.6310\); change \(6e^{0.1}(e^{0.1}-1)\approx0.6974\).)
  5. A logit model has \(\operatorname{logit}(p)=-2+0.8x+0.6A-0.3Ax\). At \(x=1\), calculate the link-scale group difference, both probabilities, and their probability difference (comparison minus reference). (Answer: link-scale difference \(0.3\); probabilities \(0.2315\) and \(0.2891\); probability difference \(0.0576\).)
Clinical Biostats