Tutorials › Exam PA - Predictive Analytics › Writing GLM Formulas For Predictor Structures

Exam PA · GLM Families and Model Construction · Tutorial 311 of 966

Writing GLM Formulas For Predictor Structures

Translate a scientific predictor structure into R formula syntax, then check its model-matrix columns and interpret each fitted term.

Intermediate 11 min read

What You'll Learn

  • Translate additive effects into R formula terms and understand the default intercept.
  • Distinguish numeric predictors from categorical factors and identify the reference level.
  • Expand the `*` operator into main effects and an interaction term.
  • Construct and verify model-matrix rows from treatment-coded predictors.
  • Calculate link-scale fitted values and interpret interaction coefficients in context.
  • Inspect fitted coefficients and term contributions in R.

From a Scientific Description to an R Formula

A GLM formula is more than a compact way to list variables. It specifies the structure of the linear predictor: which predictors have additive effects, how categorical predictors are represented, and whether the effect of one predictor is allowed to depend on another. A formula that omits a scientifically important interaction imposes a different model, even if it contains all the right variable names.

For a GLM with response \(Y_i\), the linear predictor for observation \(i\) is \(\eta_i\), and the mean is related to it by the link function \(g(\mu_i)=\eta_i\). The formula defines the predictor columns that can contribute to \(\eta_i\); it does not by itself choose a response family or link. Those are specified separately in a call such as `glm(..., family=...)`.

Definition: An R model formula has the general form `response ~ terms`. The response is on the left of `~`; the terms on the right determine the columns used to form the linear predictor. Unless removed, the formula includes an intercept. For a design matrix \(X\) and coefficient vector \(\boldsymbol{\beta}\), the model writes \(\boldsymbol{\eta}=X\boldsymbol{\beta}\), with \(\eta_i\) the linear predictor for observation \(i\).

This tutorial focuses on predictor structure. The response coding and the choice of family and link must already be appropriate for the outcome. When formulas are translated into a model matrix, numeric variables generally contribute their recorded values, while factor variables are represented through contrast columns. This distinction matters: a numeric code such as 1, 2, 3 is treated as a quantitative scale unless it is explicitly stored as a factor.

Additive Terms and the Model Matrix

In an R formula, a plus sign adds terms. For example, `claim_cost ~ age_c + vehicle_age` specifies an intercept, an age effect, and a vehicle-age effect. With numeric predictors, each observation's row contains the observed predictor values. The corresponding coefficients multiply those values to produce its linear predictor.

The connection between formula and matrix follows directly from the additive model. If the formula has two numeric predictors \(x_1\) and \(x_2\), then \(\eta_i=\beta_0+\beta_1x_{i1}+\beta_2x_{i2}\). Writing the row as \((1,x_{i1},x_{i2})\) and the coefficient vector as \((\beta_0,\beta_1,\beta_2)^\mathsf{T}\) gives their dot product, which is the \(i\)th element of \(X\boldsymbol{\beta}\). The leading 1 corresponds to the intercept.

$$ \eta_i = \beta_0 + \beta_1 x_{i1} + \beta_2 x_{i2} = \begin{pmatrix}1 & x_{i1} & x_{i2}\end{pmatrix} \begin{pmatrix}\beta_0\\\beta_1\\\beta_2\end{pmatrix} $$

This also explains why inspecting the matrix is useful: the formula is a compact instruction, while the matrix shows the actual columns supplied to the fitting procedure. For a fitted GLM, `model.matrix(fit)` displays those columns, and `coef(fit)` gives their estimated coefficients. Multiplying a row by the estimated coefficients gives the fitted linear predictor for that observation. Applying the inverse link gives its fitted mean.

Worked Example: Additive Numeric Predictors

A hypothetical insurer models a positive repair-cost measure using a Gaussian GLM with identity link. The response is in dollars. The predictors are centered driver age, `age_c`, and vehicle age, `vehicle_age`, measured in years. For three policies the predictor values are \((-1,2)\), \((0,0)\), and \((1,1)\), respectively. Suppose a fitted model has intercept 1,200, age coefficient 80, and vehicle-age coefficient 35. Write the formula structure, build the model-matrix rows, and calculate all three fitted means.

Method. The two numeric predictors enter additively. The formula is `repair_cost ~ age_c + vehicle_age`. With the default intercept, the matrix columns are the intercept, `age_c`, and `vehicle_age`.

Policy 1. Its row is \((1,-1,2)\). The linear predictor is \(1(1200)+(-1)(80)+2(35)=1200-80+70=1190\). With an identity link, the fitted mean equals the linear predictor: $1,190.

Policy 2. Its row is \((1,0,0)\). The linear predictor is \(1(1200)+0(80)+0(35)=1200\), so its fitted mean is $1,200.

Policy 3. Its row is \((1,1,1)\). The linear predictor is \(1(1200)+1(80)+1(35)=1315\), so its fitted mean is $1,315.

Check. The three rows correspond to the stated input values, and each fitted mean is the intercept plus the two predictor contributions. The method avoids treating the intercept as a predictor value that should be centered or omitted.

Categorical Predictors and Reference Levels

A categorical predictor should be represented as a factor when the categories are labels rather than values on a meaningful numerical scale. Under R's usual treatment contrasts for an unordered factor, one level is the reference level. The intercept represents the linear predictor at that reference level (and at zero for any numeric predictors in the formula). Each other level gets an indicator column whose coefficient is its difference from the reference, holding other predictors fixed.

The reference level depends on the factor's level order and contrast settings; it is not safe to assume that the alphabetically first label is always the intended reference. Set the level order deliberately when interpretation depends on it, and inspect the matrix columns. For a factor with \(K\) levels, treatment coding with an intercept uses \(K-1\) indicator columns. The omitted level is represented by zeros across those columns.

Worked Example: Interpreting a Factor in a Poisson GLM

A hypothetical service plan study models the number of monthly repair events per policy using a Poisson GLM with log link and formula `events ~ territory`. `territory` is an unordered factor with levels North, South, and West. Set North as the reference. Suppose the fitted coefficients are an intercept of \(-2.0\), a South coefficient of \(0.4\), and a West coefficient of \(-0.3\). Construct the matrix rows and calculate fitted mean event counts for one policy in each territory.

Method. Under treatment coding, North is represented by zeros in both factor columns. The matrix columns are `(Intercept)`, `territorySouth`, and `territoryWest`.

Territory(Intercept)territorySouthterritoryWestLinear predictor
North100\(-2.0\)
South110\(-2.0+0.4=-1.6\)
West101\(-2.0-0.3=-2.3\)

Convert to fitted means. The log link gives \(\mu=\exp(\eta)\). For North, \(\exp(-2.0)=0.1353\); for South, \(\exp(-1.6)=0.2019\); and for West, \(\exp(-2.3)=0.1003\). These are expected counts per policy for the modeled monthly period, not probabilities.

Check. The reference level has no territory adjustment, South is above North on the log scale by \(0.4\), and West is below North by \(0.3\). The calculations avoid interpreting the factor labels as numeric values or as separate category-specific slopes.

Interactions: When One Effect Depends on Another

An additive formula assumes that each predictor's contribution is the same regardless of the value of other predictors. An interaction allows the contribution of one predictor to vary with another. In R, `x:z` denotes the interaction term alone, while `x * z` expands to `x + z + x:z`. Thus `*` includes the main effects as well as the interaction; it does not mean “interaction only.”

For a numeric predictor and a two-level factor using treatment coding, the interaction adds a column equal to the numeric predictor multiplied by the non-reference-level indicator. This changes the slope of the numeric predictor for that factor level. A main effect for the factor still changes the level at the numeric predictor's zero point. Centering a numeric predictor can make that zero point scientifically interpretable, but it does not change which interaction columns the formula requests.

Worked Example: A Numeric-by-Factor Interaction

A hypothetical risk study uses a Bernoulli GLM with logit link to model whether a policy has an event. Its formula is `event ~ age_c * channel`, where `channel` is a factor with levels Direct and Broker, and Direct is the reference. `age_c` is centered age in decades. Suppose the fitted coefficients are: intercept \(-2.0\), `age_c` \(0.1\), `channelBroker` \(0.5\), and `age_c:channelBroker` \(-0.2\). Construct the matrix rows and calculate fitted probabilities for both channels at `age_c` equal to \(-1\) and 2.

Expand the formula. The `*` operator gives `age_c + channel + age_c:channel`. With Direct as reference, the columns are `(Intercept)`, `age_c`, `channelBroker`, and `age_c:channelBroker`.

Channel and age_cInterceptage_cchannelBrokerage_c:channelBroker
Direct, \(-1\)1\(-1\)00
Direct, \(2\)1200
Broker, \(-1\)1\(-1\)1\(-1\)
Broker, \(2\)1212

Calculate link-scale predictions. For Direct at \(-1\), \(\eta=-2.0+0.1(-1)=-2.1\). For Direct at 2, \(\eta=-2.0+0.1(2)=-1.8\). For Broker at \(-1\), \(\eta=-2.0+0.1(-1)+0.5+(-0.2)(-1)=-1.4\). For Broker at 2, \(\eta=-2.0+0.1(2)+0.5+(-0.2)(2)=-1.7\).

Convert using the inverse logit. The probability is \(p=\exp(\eta)/(1+\exp(\eta))\). The four probabilities are \(0.1091\), \(0.1419\), \(0.1978\), and \(0.1545\), respectively, to four decimal places.

Interpret and check. Direct's age slope is \(0.1\). Broker's age slope is \(0.1-0.2=-0.1\), so the interaction reverses the direction of the age effect for Broker. At `age_c` equal to zero, the Broker-versus-Direct log-odds difference is \(0.5\). The matrix and calculations avoid the common error of applying the interaction coefficient as a constant group shift: its contribution is \(-0.2 \times \text{age_c}\), not simply \(-0.2\).

Inspecting the Formula and Fitted Terms

A reliable workflow separates three checks: formula expansion, matrix construction, and coefficient interpretation. For a fitted model `fit`, `formula(fit)` records the formula used, `model.matrix(fit)` displays its design matrix, and `coef(fit)` associates estimates with matrix columns. Column names are particularly useful for catching an unintended reference level or a missing interaction.

For GLMs, `predict(fit, type="link")` returns fitted linear predictors, while `predict(fit, type="response")` returns fitted means on the response scale. `predict(fit, type="terms")` can be used to inspect the separate fitted term contributions on the link scale; the intercept is generally reported separately rather than as an ordinary term contribution. The terms must be interpreted together with the factor coding and numeric-variable units. A coefficient is not meaningful in isolation from those choices.

1
Write the scientific structure.
State whether effects are additive and which predictor relationships require an interaction.
2
Translate the structure.
Use `+` for additive terms, `:` for an interaction term alone, and `*` for the two main effects plus their interaction.
3
Check coding and columns.
Confirm factor levels, reference categories, and the actual columns shown by the model matrix.
4
Interpret fitted terms on the right scale.
Combine coefficients with the observation's matrix row to obtain \(\eta_i\), then apply the inverse link if a response-scale mean or probability is needed.

Common Formula Traps

A frequent mistake is to write `age_c + channel` when the scientific question requires the age slope to differ by channel. That formula imposes one common age slope; it does not estimate an age-by-channel interaction. The correction is `age_c * channel`, which includes the main effects and the interaction. Conversely, writing `age_c:channel` alone does not automatically add those main effects. Including the main effects is usually essential to interpret the interaction as a change from an additive baseline.

Another trap is storing a category as a number and thereby fitting a linear trend across arbitrary labels. If North, Central, and West are category names coded 1, 2, and 3, a numeric predictor asserts that moving from 1 to 2 has the same effect as moving from 2 to 3. If no such ordered, equally spaced interpretation is intended, make the variable a factor. Always inspect the formula's model matrix before interpreting estimates.

Key takeaway: R formula syntax determines the columns of the GLM design matrix. Read the formula as a model statement, not as a list of available variables: confirm whether it requests additive effects, factor contrasts, and the interactions required by the scientific question.

The ability to translate between a scientific description, an R formula, and the resulting matrix is aligned with the SOA Exam PA (Predictive Analytics) syllabus. In practice, that translation is also a basic model audit: it exposes coding decisions before coefficients are reported or predictions are communicated.

Check Your Understanding

For each problem, identify the formula structure or model-matrix implication and check the result before moving on.

  1. A Gaussian identity-link model uses numeric `experience_c` and a two-level factor `vehicle_type`, with Compact as reference. Write the formula for additive effects and give the matrix row for an SUV with `experience_c` equal to \(-2\), assuming the default intercept. (Answer: `repair_cost ~ experience_c + vehicle_type`; columns are intercept, `experience_c`, and `vehicle_typeSUV`, giving row \((1,-2,1)\).)
  2. A Poisson log-link formula is `events ~ region`, with factor levels East, Central, West and East set as reference. Coefficients are \(-1.5\) for the intercept, \(0.2\) for Central, and \(-0.4\) for West. What are the West linear predictor and fitted mean? (Answer: \(\eta=-1.5-0.4=-1.9\), so \(\mu=\exp(-1.9)=0.1496\) events.)
  3. A model has formula `score ~ duration * plan`, with Basic as reference. Coefficients are intercept \(1.0\), duration \(0.3\), planPlus \(-0.2\), and duration:planPlus \(0.1\). Find the linear predictor for a Plus plan with duration 2. (Answer: \(1.0+0.3(2)-0.2+0.1(2)=1.6\).)
  4. Explain the structural difference between `y ~ a + b` and `y ~ a * b`. (Answer: the first includes an intercept and the two main effects; the second also includes the interaction `a:b`.)
Clinical Biostats