Why the Rating-Model Matrix Matters
A generalized linear model (GLM) uses recorded policy characteristics to construct a linear predictor. Before the model can produce a fitted rate or expected claim count, each record must be represented by a row of numbers in a design matrix. Those numbers depend on the chosen coding of categorical factors, any transformations of continuous predictors, and any interaction terms in the model.
Building a row is a translation task: start with a policy record and the model specification, then calculate each column in the specified order. The same factor levels can produce different coefficient values under different coding schemes, but a correctly specified model can still produce the same fitted values. This tutorial focuses on constructing and checking rows, not on choosing among competing transformations or assessing model fit.
The matrix does not contain the response or the fitted coefficients. It contains the values used to combine with those coefficients. A column of ones is commonly included as the intercept. The offset, when present, is known and is added to the linear predictor; it is not an estimated coefficient and does not occupy a design-matrix column.
Reference Coding for Categorical Factors
For a factor with \(K\) levels, reference coding selects one level as the reference and uses \(K-1\) indicator columns. An indicator is 1 when the record has the named level and 0 otherwise. The reference level has 0 in all the factor's indicator columns. Consequently, the intercept corresponds to the reference level when the other predictors are at their zero values, subject to any other model terms.
For example, suppose territory has levels Central, North, and South, with Central as the reference. The factor columns are Territory-North and Territory-South. A Central record receives \((0,0)\), a North record \((1,0)\), and a South record \((0,1)\). Factor column names should make the represented level explicit; a column labelled only “Territory” can obscure which value it represents.
Worked Example: Coding Two Rating Factors
A hypothetical rating model uses territory, with Central as the reference, and vehicle class, with Sedan as the reference. Construct rows in the column order: intercept, Territory-North, Territory-South, Vehicle-SUV. The records are: (a) Central/Sedan, (b) North/SUV, and (c) South/Sedan.
Step 1: Set the column rules. The intercept is 1 for every record. Territory-North is 1 only for a North record; Territory-South is 1 only for a South record. Vehicle-SUV is 1 only for an SUV.
Step 2: Encode each record. Central/Sedan gives \([1,0,0,0]\). North/SUV gives \([1,1,0,1]\). South/Sedan gives \([1,0,1,0]\).
Step 3: Check the coding. Each record has exactly one territory level, so the two territory indicators sum to either 0 for Central or 1 for North or South. The SUV indicator is 0 for both Sedan records and 1 for the SUV record. The rows therefore match the stated levels and column order.
Including an indicator for every level as well as an intercept creates exact linear dependence: for every record, the sum of all \(K\) level indicators equals the intercept column. With three territory indicators, for instance, Territory-Central + Territory-North + Territory-South = 1. This is why reference coding omits one level indicator. Other coding schemes are possible, but the row construction must match the scheme used for the coefficient estimates.
Continuous Transformations and Interaction Columns
A continuous-predictor column contains the value specified by the model, which may not be the raw recorded value. If the model uses a centered and scaled variable \(z=(x-c)/s\), enter that calculated value. If it includes a polynomial term \(z^2\), calculate the square of the same \(z\); do not square the raw \(x\) instead. A specified transformation such as \(\ln(1+x/a)\) must likewise be applied using the stated scale \(a\).
An interaction column is the product of the model-matrix columns for its component terms. For a factor indicator \(I\) and continuous predictor \(z\), the interaction column is \(Iz\). It is zero when \(I=0\), and equals \(z\) when \(I=1\). For two factor indicators \(I_A\) and \(I_B\), their interaction column is \(I_AI_B\), which equals 1 only when both indicators equal 1. The interaction is an additional column; it does not replace either main-effect column.
Worked Example: Transforming Predictors and Calculating a Fitted Count
A hypothetical Poisson log-link model predicts claim count for a policy period. Its predictors are territory (Central reference, with a North indicator), vehicle class (Sedan reference, with an SUV indicator), \(z=(\text{age}-40)/10\), \(z^2\), North-by-SUV, and \(z\)-by-SUV. The column order is intercept, North, SUV, \(z\), \(z^2\), North-by-SUV, \(z\)-by-SUV. The coefficients in that order are \(\ln(0.01), 0.10, 0.20, 0.05, -0.01, 0.03, -0.02\). The policy is North, SUV, age 50, with exposure 0.5. Assume the offset is the log of exposure, as appropriate for this count model.
Step 1: Transform the age. \(z=(50-40)/10=1\), so \(z^2=1^2=1\).
Step 2: Calculate indicators and interactions. North \(=1\), SUV \(=1\), North-by-SUV \(=1\times1=1\), and \(z\)-by-SUV \(=1\times1=1\). The row is \([1,1,1,1,1,1,1]\).
Step 3: Form the linear predictor. The offset is \(\ln(0.5)\). Thus
Step 4: Calculate the fitted mean. Under the Poisson log-link specification, the expected count is
The fitted expected claim count is \(0.0071\) to four decimal places. This is a mean, not a statement that a fraction of a claim will occur. The row, coefficient order, and offset have all been included in the calculation.
A Matrix Row from Start to Finish
A row is reliable only when its column order and definitions are fixed before values are inserted. For each record, first calculate transformed continuous variables, then indicators, and then interactions from the relevant columns. Finally, check that the row has one entry for every model coefficient.
List the intercept, factor indicators, transformed continuous terms, and selected interactions in a fixed order.
Apply the stated centering, scaling, or transformation, and code each factor relative to its specified reference level.
Multiply the component model-matrix values for each interaction in the specification.
Confirm its length and order, then multiply by the coefficient vector and add any offset to obtain the linear predictor.
Worked Example: Constructing Rows and Checking Fitted Rates
A hypothetical insurer models annual indicated rate with a log link and no offset. Predictors are territory (East reference, with a West indicator), payment plan (Annual reference, with a Monthly indicator), \(z=(\text{age}-40)/10\), \(q=\ln(1+\text{annual miles}/10{,}000)\), and West-by-Monthly. The column order is intercept, West, Monthly, \(z\), \(q\), West-by-Monthly. The coefficients are \(\ln(200), 0.08, 0.12, 0.04, 0.03, -0.05\). Construct and use rows for (a) West/Monthly, age 35, 30,000 miles, and (b) East/Annual, age 45, 10,000 miles.
Step 1: Calculate the continuous terms. For record (a), \(z=(35-40)/10=-0.5\) and \(q=\ln(1+30{,}000/10{,}000)=\ln(4)=1.386294\ldots\). For record (b), \(z=(45-40)/10=0.5\) and \(q=\ln(1+10{,}000/10{,}000)=\ln(2)=0.693147\ldots\).
Step 2: Code factors and the interaction. Record (a) has West \(=1\), Monthly \(=1\), and West-by-Monthly \(=1\times1=1\). Its row is \([1,1,1,-0.5,\ln(4),1]\). Record (b) has West \(=0\), Monthly \(=0\), and West-by-Monthly \(=0\). Its row is \([1,0,0,0.5,\ln(2),0]\).
Step 3: Calculate the rate for record (a). Its linear predictor is
Therefore, \(\mu_a=200\exp(0.1715888\ldots)=200(1.187189\ldots)=237.44\) dollars, rounded to the nearest cent.
Step 4: Calculate the rate for record (b) as a check. Its predictor is
Thus \(\mu_b=200\exp(0.0407944\ldots)=200(1.041638\ldots)=208.33\) dollars to the nearest cent. The zero factor and interaction entries for the reference/reference record leave only its continuous effects beyond the intercept.
Why Coding and Column Order Must Agree
The linear predictor is a dot product: each row entry multiplies the coefficient in the corresponding position. If the predictor columns are ordered as \([1,\text{North},z]\), then the coefficient vector must be ordered as \([\beta_0,\beta_{\text{North}},\beta_z]\). Swapping the last two coefficients changes the model calculation even if the row contains all the right numbers.
There is a simple algebraic reason for omitting one level indicator when an intercept is present. For a factor whose levels are exhaustive and mutually exclusive, each record belongs to exactly one level. Therefore the sum of its level indicators is always 1, the same as the intercept column. The columns are linearly dependent, so their coefficients cannot all be uniquely determined from the data. Omitting one indicator removes this particular dependence; it does not guarantee that no other collinearity exists.
Interactions require similar care. If the model specifies North-by-SUV, its column is the product of the North and SUV indicators. A North/Sedan record therefore has an interaction value of \(1\times0=0\). If the model also specifies \(z\)-by-SUV, a Central/SUV record with \(z=1\) has North-by-SUV \(=0\) but \(z\)-by-SUV \(=1\times1=1\). Treating all interaction columns as if they were the same term is a common row-construction error.
Worked Example: Correcting a Mis-coded Interaction
A hypothetical model has Central as the territory reference, Sedan as the vehicle reference, and includes North-by-SUV and \(z\)-by-SUV. The row order is intercept, North, SUV, \(z\), North-by-SUV, \(z\)-by-SUV. A Central/SUV record has \(z=1\). Identify and correct a proposed row \([1,0,1,1,1,1]\).
Step 1: Check the main-effect columns. The intercept is 1, North is 0 because the territory is Central, SUV is 1, and \(z\) is 1. Those four entries are correct.
Step 2: Recalculate each interaction separately. North-by-SUV is \(0\times1=0\). The proposed value 1 in that column is wrong. The \(z\)-by-SUV value is \(1\times1=1\), so the final entry is correct.
Correction. The correct row is \([1,0,1,1,0,1]\). This check avoids the mistake of assigning a nonzero interaction simply because one of its components is nonzero.
Check Your Understanding
For each problem, construct the requested entries or fitted quantity using the stated coding and column order.
- A factor has levels Coastal (reference), Inland, and Metro. With columns Inland and Metro, what entries does a Metro record receive, and what does a Coastal record receive? (Answer: Metro \((1,0)\); Coastal \((0,0)\).)
- A model uses \(z=(\text{age}-40)/10\) and \(z^2\). For age 55, calculate both model-matrix entries. (Answer: \(z=1.5\), \(z^2=2.25\).)
- A model uses \(q=\ln(1+\text{miles}/10{,}000)\) and a West indicator \(W\), with interaction \(Wq\). For a West record with 30,000 miles, calculate \(q\) and \(Wq\). (Answer: \(q=\ln(4)=1.3863\) to four decimals; \(Wq=1.3863\).)
- A log-link model has \(\ln(\mu)=\ln(80)+0.10W+0.20z-0.05Wz\). For \(W=1\) and \(z=2\), calculate \(\mu\) to the nearest cent. (Answer: \(\ln(\mu)=\ln(80)+0.10+0.40-0.10=\ln(80)+0.40\), so \(\mu=80e^{0.40}=119.35\).)
- A factor has three exhaustive levels and a model includes an intercept plus all three level indicators. State the column relationship and the reference-coding correction. (Answer: The three indicators sum to the intercept column for every record, creating linear dependence; omit one level indicator, such as the reference level.)