From Coefficients to Predictions
A fitted model’s coefficients become useful when they are translated into predictions for particular profiles. With treatment coding, each coefficient is defined relative to a reference category. To predict for a profile, start with the intercept, add the coefficients for its nonreference categories, and include any interaction coefficients that apply to that profile. The calculation is simple once the coding is made explicit; omitting an applicable term or adding one for the reference category changes the prediction.
This tutorial focuses on calculating fitted values from supplied treatment-coded coefficients. It does not require fitting a model. The outcome and prediction scale matter: in a linear model the coefficient calculation is on the outcome scale, while in a generalized linear model it first gives a linear predictor that may need to be transformed to obtain a response-scale prediction.
Building a Prediction Row
Consider a linear model with one factor, whose reference level is Direct and whose other levels are Broker and Online. Its treatment-coded form is
Here, \(I(\text{Broker})\) is 1 if the profile is Broker and 0 otherwise; \(I(\text{Online})\) is defined similarly. For Direct, both indicators are 0, so its fitted value is the intercept. For Broker, the Broker coefficient is added, and for Online, the Online coefficient is added. A factor profile cannot simultaneously be both Broker and Online, so only one of these indicators is 1.
Worked Example: Predicting for One Factor Level
A hypothetical linear model predicts annual service cost in dollars from a customer’s service channel. The reference level is Direct. The estimated intercept is 120, the Broker coefficient is 18, and the Online coefficient is \(-7\). Calculate the fitted cost for each channel and verify the Online prediction in R.
Method. Use the treatment-coded equation and set each indicator according to the requested channel.
For Direct, both nonreference indicators are 0:
For Broker, the Broker indicator is 1 and the Online indicator is 0:
For Online, the Broker indicator is 0 and the Online indicator is 1:
In R, if the factor levels are set in the order Direct, Broker, Online, the treatment-coded reference is Direct by default. The command `predict(fit, newdata = data.frame(channel = factor("Online", levels = levels(d$channel))))` returns 113. The values agree with the hand calculation. The common mistake is to add both factor coefficients for Online; that would give \(120+18-7=131\), even though the Broker indicator is 0 for an Online profile.
Predictions When Factors Interact
With two factors and an interaction, the model includes an additional coefficient for combinations of nonreference levels. Suppose Plan has levels Basic and Plus, with Basic as reference, and Zone has levels North and South, with North as reference. A linear model with their interaction can be written as
The interaction indicator is 1 only for the Plus-South profile. It is 0 for each of the other three profiles. Thus, the Plus coefficient describes the Plus-versus-Basic difference in the reference zone, North; the South coefficient describes the South-versus-North difference for the reference plan, Basic. The interaction coefficient adjusts the combination of those two nonreference levels.
Worked Example: Calculating All Profiles With an Interaction
A hypothetical linear model predicts a service measure from Plan and Zone. Basic and North are the reference levels. The fitted coefficients are: intercept 410, Plus coefficient 60, South coefficient \(-25\), and Plus-by-South interaction coefficient 18. Calculate the fitted value for each of the four profiles and verify them using R.
Method. Write the model’s indicator terms and evaluate them for each profile. For Basic-North, all indicators are zero:
For Plus-North, only the Plus indicator is 1. The interaction indicator is 0 because Zone is at its reference level:
For Basic-South, only the South indicator is 1:
For Plus-South, both main-effect indicators and their product are 1:
The interaction coefficient can also be checked as a difference in differences. The Plus-versus-Basic difference in North is \(470-410=60\). The same difference in South is \(463-385=78\). Their difference is \(78-60=18\). Equivalently,
In R, fit the specified model with `lm(y ~ plan * zone, data = d)`, after setting factor levels so Basic and North are the references. Create a new data frame containing the four plan-zone combinations, then use `predict(fit, newdata = new_profiles)`. In the order Basic-North, Plus-North, Basic-South, Plus-South, the predictions are 410, 470, 385, and 463. This check depends on the new-data factors having the same levels and reference coding as the fitted model.
Common mistake. Leaving out the interaction gives \(410+60-25=445\) for Plus-South. That is not the fitted value from this model: the Plus-South interaction indicator is 1, so the interaction coefficient must also be included, giving \(445+18=463\).
Why the Indicator Calculation Works
A treatment-coded model is a linear combination of columns in its design matrix. For a specified profile, each factor indicator has a known value, either 0 or 1. Substituting those values into the model gives the profile’s row of the design matrix, denoted by \(x\). Multiplying that row by the estimated coefficient vector \(\widehat{\beta}\) gives the fitted value.
For the two-factor example, the row for Plus-South is \(x^\mathsf{T}=(1,1,1,1)\), corresponding to the intercept, Plus indicator, South indicator, and interaction indicator. The coefficient vector is \(\widehat{\beta}^\mathsf{T}=(410,60,-25,18)\), so
This derivation explains why profile prediction is addition of the applicable terms: the fitted value is the dot product of the profile’s design row and the model’s coefficient vector. If the model uses a different contrast scheme, the design row and coefficient meanings change; the same dot-product principle still applies, but the treatment-coded shortcut must not be used without checking the contrasts.
The difference-in-differences identity follows from the same model. Let the four cell predictions be \(m_{BN},m_{PN},m_{BS},m_{PS}\), where the first subscript denotes Basic or Plus and the second denotes North or South. Subtracting the Basic prediction from the Plus prediction in each zone gives
Subtract the first difference from the second. The main-effect coefficient \(\widehat{\beta}_{P}\) cancels, leaving \(\widehat{\beta}_{PS}\). Rearranging gives
This is a useful arithmetic check: a calculated interaction coefficient should equal the difference between the two group differences.
Linear Predictors and Response-Scale Predictions
For a generalized linear model, the coefficient calculation first produces the linear predictor, not necessarily the predicted mean on the outcome scale. In a binomial logistic model, the link is the logit: \(\eta=\log\{p/(1-p)\}\). The response-scale prediction is \(p=1/(1+\exp(-\eta))\). Therefore, check whether an R prediction is requested on the link scale or the response scale.
Worked Example: Converting a Logit Prediction to a Probability
A hypothetical logistic regression predicts whether a submitted application is accepted, using channel as a factor. Standard is the reference level. The fitted intercept is \(-1.0\), the Broker coefficient is \(0.4\), and the Online coefficient is \(0.8\). Calculate the predicted acceptance probability for an Online application and compare the link-scale and response-scale R predictions.
Method. First calculate the linear predictor using the Online indicator. The Standard indicator is implicit in the intercept, and the Online coefficient is added:
Convert this logit to a probability:
The probability is 0.4502, or 45.02% when expressed as a percentage. In R, `predict(fit, newdata = online_profile, type = "link")` returns \(-0.2\), while `predict(fit, newdata = online_profile, type = "response")` returns approximately 0.4502. The common mistake is to report \(-0.2\) as a probability; it is a logit and can be negative. Round the probability to four decimal places. The calculation is checked because applying the inverse-logit to \(-0.2\) returns approximately 0.4502.
Practical Checks and Exam Traps
A reliable prediction calculation starts by writing down the reference levels and the model terms. Then record which indicators equal 1 for the requested profile. This prevents a common error: treating the reference category as though it had its own coefficient to add. The reference contribution is already represented by the intercept.
Before accepting an R check, confirm that the requested factor values are valid levels and that their reference levels match those used when fitting the model. A prediction for an unseen level is not obtained by assigning it a coefficient of zero; it is generally unavailable from the fitted model without additional assumptions or a changed model. Also distinguish a prediction on the link scale from one on the response scale for a GLM.
Include the intercept, the relevant main-effect indicators, and any interactions present in the fitted model.
Set each indicator to 1 if the profile has that nonreference level and 0 otherwise. An interaction indicator is the product of its component indicators.
Sum the applicable coefficient terms. For a GLM, transform the linear predictor if the requested prediction is on the response scale.
Use matching factor levels and a prediction type that matches the scale of the hand calculation.
These steps make coefficient-based predictions auditable: every term has a clear reason for being included or excluded. They also provide a quick way to diagnose disagreement with R. A discrepancy often indicates a different reference level, an omitted interaction, or a mismatch between link and response scales.
Check Your Understanding
For each problem, identify applicable terms before calculating. Values are fitted coefficients, not estimates from real data.
- A linear model has reference level Local, intercept 72, and coefficients Regional \(=9\) and National \(=-4\). Calculate the fitted values for Local and National. (Answer: Local \(=72\); National \(=68\))
- A linear model has reference levels Basic and East, with intercept 300, Plus coefficient 20, West coefficient \(-10\), and Plus-by-West coefficient 6. Calculate the fitted value for Plus-West. (Answer: \(316\))
- For the model in Question 2, calculate the Plus-versus-Basic difference in East and in West. What is the difference between those differences? (Answer: East difference \(=20\); West difference \(=26\); difference-in-differences \(=6\))
- A logistic model has reference channel Desk, intercept \(-0.6\), and Mobile coefficient \(0.6\). Calculate the Mobile linear predictor and its response-scale probability, rounded to four decimals. (Answer: linear predictor \(=0\); probability \(=0.5000\))
- A linear interaction model predicts an outcome for two factors A and B. The reference-reference prediction is 250; changing A alone gives 270; changing B alone gives 240; and changing both gives 268. Calculate the interaction coefficient using the difference-in-differences identity. (Answer: \(268-240-270+250=8\))