Why the Formula Operator Matters
In R, a small change in a formula can change which effects a fitted model is allowed to represent. The asterisk in `A * B` is shorthand for a model with both predictors’ main effects and their interaction. The colon in `A:B` specifies the interaction term alone; it does not automatically add either main effect. This distinction matters when interpreting coefficients, constructing a model matrix, and deciding which patterns of fitted values are possible.
The discussion below uses ordinary treatment contrasts and an intercept unless stated otherwise. For a factor, treatment coding represents each nonreference level with an indicator column; the reference level is represented by zeros in those columns. The specific column labels can depend on factor names and level labels, but the number and role of columns follow from the coding.
What the Terms Represent
A main effect represents a predictor’s contribution without requiring a joint effect with another predictor. An interaction represents an additional contribution associated with combinations of predictor values, beyond the main effects already included in the model. With factors, the interaction can allow the difference between levels of one factor to vary across levels of the other.
The colon term does not have one universal interpretation independent of the other terms in the model. In a model that includes the main effects, the interaction describes departures from the additive main-effect structure. In a model with the colon term but without the main effects, the coefficients describe a different, more restricted parameterization. A formula should therefore be read as a complete specification of terms, not as a label that implies terms that are absent.
This expansion is a formula rule, not a claim that the model is always appropriate. Whether to include the main effects and interaction depends on the modeling question and the information in the data. Sparse or unobserved combinations can also make some model-matrix columns redundant or impossible to estimate reliably.
Worked Example: Comparing Two Formulas for Two Factors
A hypothetical service study records response time for each of four combinations of region (East or West) and service type (Standard or Rapid). There is one observation per combination, and the observed response-time averages are: East-Standard 10, East-Rapid 12, West-Standard 14, and West-Rapid 20 minutes. Use East and Standard as reference levels. Write the model matrices for `response ~ Region:Service` and `response ~ Region * Service`, then compare their fitted values.
Method. Let \(W=1\) for West and \(0\) for East; let \(R=1\) for Rapid and \(0\) for Standard. Under treatment coding, the interaction column is the product \(WR\). In the row order given, the colon-only formula includes an intercept and the interaction column:
The first three rows have the same two entries for the columns actually in `Region:Service`: an intercept of 1 and an interaction value of 0. Thus this model requires those three combinations to have the same fitted value. Their observed responses are 10, 12, and 14, so the least-squares estimate of the intercept is their mean, \((10+12+14)/3=12\). The remaining observation has interaction value 1, so its fitted value is 20 and the interaction coefficient is \(20-12=8\). The colon-only model therefore fits 12, 12, 12, and 20.
For the star formula, the model matrix has all four columns displayed above. It can fit all four observed values exactly. The intercept is 10, the West effect is \(14-10=4\), and the Rapid effect is \(12-10=2\). The interaction coefficient is the remaining difference:
Checking the four fitted values gives \(10\), \(10+2=12\), \(10+4=14\), and \(10+4+2+4=20\). The key difference is not that the colon term disappears from the star formula: it appears in both. The star formula also includes the two main-effect columns. Common mistake: treating `Region:Service` as shorthand for the full interaction model. That would incorrectly assign separate fitted values to the first three rows here; the colon-only model does not contain those main effects.
Model-Matrix Column Counts
Under treatment coding, if factor A has \(a\) levels and factor B has \(b\) levels, their main effects have \(a-1\) and \(b-1\) columns. The interaction term has \((a-1)(b-1)\) columns when the ordinary full-rank treatment-coded design is available. With an intercept, the star formula’s total is therefore \(1+(a-1)+(b-1)+(a-1)(b-1)=ab\). The colon-only formula has the intercept plus the interaction columns, for a total of \(1+(a-1)(b-1)\).
These counts assume the model matrix has full rank. Missing combinations, redundant predictors, or other design features can cause columns to be aliased or unidentifiable. A column count is not a guarantee that every coefficient can be estimated from a particular data set.
Worked Example: Building a Two-Factor Model Matrix
A hypothetical analysis uses a two-level factor, Channel (Desk or App), and a three-level factor, Package (Basic, Plus, or Complete). Desk and Basic are reference levels. There is one observed response average in each of the six combinations: 8, 10, and 11 for Desk across the three packages, and 12, 15, and 16 for App. Construct the matrix for `y ~ Channel * Package` and find coefficients that reproduce these six averages.
Method. Define \(C=1\) for App and 0 for Desk, \(P_2=1\) for Plus and 0 otherwise, and \(P_3=1\) for Complete and 0 otherwise. The star formula expands to `Channel + Package + Channel:Package`. Its columns, in the stated row order, are:
The intercept is the Desk-Basic response, 8. The Package-Plus coefficient is \(10-8=2\), and the Package-Complete coefficient is \(11-8=3\). The Channel-App coefficient is \(12-8=4\). The App-Plus interaction is \(15-8-4-2=1\), and the App-Complete interaction is \(16-8-4-3=1\).
Check the fitted values in row order: \(8\), \(8+2=10\), \(8+3=11\), \(8+4=12\), \(8+4+2+1=15\), and \(8+4+3+1=16\). Thus the star model reproduces all six cell averages. It has \(1+1+2+2=6\) columns, consistent with \(ab=2\cdot3=6\). By contrast, `Channel:Package` has the intercept and two interaction columns, for three columns total; it does not include the Channel or Package main effects. Common mistake: counting only one interaction column because there is only one nonreference Channel level. Each of the two nonreference Package levels can interact with that Channel indicator, giving two interaction columns.
When One Predictor Is Numeric
The same formula rule applies when one predictor is numeric. For a two-level factor \(G\) and a numeric predictor \(x\), `G * x` expands to `G + x + G:x`. Under treatment coding, the factor main effect has one column, the numeric main effect has one column, and the interaction term has one column: the product of the nonreference-level indicator and \(x\). Thus `G:x` contributes one column; with an intercept, the model matrix for `~ G:x` has two columns in total.
Worked Example: A Factor-by-Numeric Interaction
A hypothetical study records an outcome at \(x=0,1,2\) for two groups, Standard and Flex. The observed outcomes are 5, 7, and 9 for Standard, and 8, 11, and 14 for Flex. Use Standard as the reference group. Construct the model matrix for `y ~ G * x` and calculate its coefficients.
Method. Let \(F=1\) for Flex and 0 for Standard. Since `G * x` expands to `G + x + G:x`, the columns are \(1,F,x,Fx\):
The intercept is the fitted Standard outcome at \(x=0\), which is 5. The Standard slope is the change from \(x=0\) to \(x=1\), or \(7-5=2\); hence the coefficient of \(x\) is 2. The Flex main-effect coefficient is the difference at \(x=0\), \(8-5=3\). The Flex slope is \(11-8=3\), so its increment over the Standard slope is \(3-2=1\); the \(Fx\) coefficient is 1.
The fitted values are \(5\), \(5+2=7\), \(5+2(2)=9\), \(5+3=8\), \(5+3+2+1=11\), and \(5+3+2(2)+2(1)=14\), matching the data. In `~ G:x`, the interaction column is \(Fx\), but the main-effect columns \(F\) and \(x\) are absent. That model would impose a common intercept and would not include a general \(x\) slope for the reference group. Common mistake: counting the intercept as part of the interaction term. The interaction contributes one column; the intercept is a separate column.
Why the Interaction Columns Have This Form
The factor-by-numeric case follows directly from the meaning of treatment coding. Let \(F\) be the indicator for the nonreference group. A model with separate group intercepts and slopes can be written as
When \(F=0\), the fitted value is \(\beta_0+\beta_xx\). When \(F=1\), it is \((\beta_0+\beta_F)+(\beta_x+\beta_{Fx})x\). Subtracting the reference-group expression from the other-group expression gives \(\beta_F+\beta_{Fx}x\): the group difference can change with \(x\). This is why the product \(Fx\) is the interaction column and why the factor and numeric main effects are distinct terms.
For two factors, the same logic applies to products of their nonreference-level indicators. A product is 1 only for the corresponding combination of nonreference levels. Those product columns encode the extra cell-specific departures from the main effects in the full model. If a formula omits the main effects, the model cannot generally reproduce the same set of fitted values as the full formula.
Practical Reading Checks
Rewrite every `A * B` as `A + B + A:B` before deciding what the model includes.
For treatment coding, establish which indicator columns represent nonreference levels.
Keep the intercept separate from the main-effect and interaction columns, and check whether the design is full rank.
Ask which differences or slopes the formula forces to be shared rather than estimating separately.
The formulas `A:B` and `A * B` can both be useful, but they answer different modeling specifications. Before fitting either, confirm that the written formula matches the intended structure and that the observed combinations provide enough information for its terms. This careful reading of formula syntax is aligned with the SOA Exam PA (Predictive Analytics) syllabus.
Check Your Understanding
For each problem, identify the formula terms and use the stated treatment coding to count or interpret the model-matrix columns.
- A factor \(P\) has three levels and factor \(Q\) has two levels. With an intercept, expand `P * Q` and count its columns. Then count the columns for `P:Q` alone. (Answer: `P + Q + P:Q`; star has \(1+2+1+2=6\) columns, while colon-only has \(1+2=3\).)
- A two-level factor \(H\) interacts with numeric \(z\). Expand `H * z`; how many columns does `H:z` contribute, and how many columns are in `~ H:z` with the default intercept? (Answer: `H + z + H:z`; the interaction contributes 1 column, and `~ H:z` has 2 columns including the intercept.)
- For two two-level factors \(A\) and \(B\), use indicators \(A_1\) and \(B_1\) for their nonreference levels. State the columns in `~ A:B` and the fitted-value restriction it imposes on the three combinations other than the joint nonreference combination. (Answer: columns are \(1\) and \(A_1B_1\); the three other combinations all have fitted value equal to the intercept.)
- A factor \(M\) has four levels and \(x\) is numeric. Under treatment coding, expand `M * x` and count the columns, including the intercept. (Answer: `M + x + M:x`; there are \(1+3+1+3=8\) columns.)
- For a two-level factor \(G\) and numeric \(x\), a fitted model is \(\widehat y=6+2x+3G+0.5Gx\), with \(G=0\) the reference group. Find each group’s intercept and slope, and state whether the interaction term is present. (Answer: reference intercept 6 and slope 2; nonreference intercept 9 and slope 2.5; the interaction is present with coefficient 0.5.)