Tutorials › Generalized Linear Models › Summing Predictions Across Groups

Count Effects and Predictions · Tutorial 481 of 981

Summing Predictions Across Groups

Learn to add fitted group means and quantify uncertainty in their total while accounting for the shared coefficient estimates.

Intermediate 11 min read

What You'll Learn

  • Define a total as the sum of fitted conditional means across specified groups
  • Derive the gradient of that total with respect to the regression coefficients
  • Estimate its variance using the coefficient covariance matrix
  • Calculate covariance between fitted means for different groups
  • Recognize why treating shared-coefficient predictions as independent is generally wrong
  • Distinguish uncertainty in a fitted expected total from a future observed count

From Group Predictions to an Expected Total

A fitted count model often produces a predicted mean for each of several groups: for example, expected admissions in each hospital or expected events in each age band. If the question concerns the expected number across all the groups together, the group means must be summed. The uncertainty of that sum also matters. Because each group prediction usually uses the same estimated regression coefficients, their errors are generally correlated.

This tutorial develops the sum and its coefficient-based variance. The target is the total of the groups’ conditional expected counts, not the realized count that might be observed later. The distinction is important: adding fitted means answers an expectation question, while predicting a future observed count requires additional distributional variation.

Definition: For \(G\) specified groups, let \(\boldsymbol{x}_g\) be the regression design vector for group \(g\), \(o_g\) a fixed offset, and \(\boldsymbol{\beta}\) the coefficient vector. Under a log link, the fitted conditional mean at coefficient value \(\boldsymbol{\beta}\) is \(\mu_g(\boldsymbol{\beta})=\exp(o_g+\boldsymbol{x}_g^\mathsf{T}\boldsymbol{\beta})\). The aggregate expected total is \(T(\boldsymbol{\beta})=\sum_{g=1}^G\mu_g(\boldsymbol{\beta})\). Its fitted value is \(\widehat T=\sum_{g=1}^G\widehat\mu_g\), where \(\widehat\mu_g=\mu_g(\widehat{\boldsymbol{\beta}})\).

Each \(\boldsymbol{x}_g\) describes the covariate profile used for that group. The offset can, for example, represent a known exposure on the log scale. It is treated as fixed here. If the target is a weighted total, with fixed weights \(w_g\), replace the definition by \(T(\boldsymbol{\beta})=\sum_g w_g\mu_g(\boldsymbol{\beta})\); the same derivation applies with \(w_g\) included in each term.

Deriving the Variance of the Sum

Write \(\widehat{\boldsymbol V}\) for the estimated covariance matrix of \(\widehat{\boldsymbol{\beta}}\). In a large-sample calculation, the delta method propagates this coefficient uncertainty through a differentiable function of the coefficients. For a fitted mean, differentiating the exponential gives \(\nabla_{\boldsymbol{\beta}}\mu_g(\boldsymbol{\beta})=\mu_g(\boldsymbol{\beta})\boldsymbol{x}_g\). Differentiation is linear, so the gradient of the total is the sum of these group gradients.

Result: Under the regularity conditions that \(\widehat{\boldsymbol{\beta}}\) is a consistent, asymptotically normal estimator with a consistently estimated covariance matrix, and that the offsets and design vectors are fixed, the delta-method variance estimate for the fitted total is \(\widehat{\operatorname{Var}}(\widehat T)=\boldsymbol{a}^\mathsf{T}\widehat{\boldsymbol V}\boldsymbol{a}\), where \(\boldsymbol{a}=\sum_{g=1}^G\widehat\mu_g\boldsymbol{x}_g\). This is an estimated large-sample variance, not generally an exact finite-sample variance.

The result follows by applying the multivariate delta method to \(T\). To state the approximation precisely, suppose that for the true coefficient vector \(\boldsymbol{\beta}_0\), \(\sqrt n(\widehat{\boldsymbol{\beta}}-\boldsymbol{\beta}_0)\xrightarrow{d}N(\boldsymbol{0},\boldsymbol{\Sigma})\), with \(\widehat{\boldsymbol V}\) consistently estimating \(\boldsymbol{\Sigma}/n\). The function \(T\) is differentiable, and its gradient at \(\boldsymbol{\beta}_0\) is \(\boldsymbol{a}_0=\sum_g\mu_g(\boldsymbol{\beta}_0)\boldsymbol{x}_g\). The delta method therefore gives \(\sqrt n\{T(\widehat{\boldsymbol{\beta}})-T(\boldsymbol{\beta}_0)\}\xrightarrow{d}N(0,\boldsymbol{a}_0^\mathsf{T}\boldsymbol{\Sigma}\boldsymbol{a}_0)\). Replacing the unknown gradient and covariance by their estimates yields the stated variance estimate.

$$ \nabla_{\boldsymbol{\beta}}T(\boldsymbol{\beta}) =\sum_{g=1}^G \nabla_{\boldsymbol{\beta}}\mu_g(\boldsymbol{\beta}) =\sum_{g=1}^G \mu_g(\boldsymbol{\beta})\boldsymbol{x}_g. $$

The same calculation makes the covariance structure explicit. Let \(\widehat{\boldsymbol{\mu}}\) be the vector of fitted means. The Jacobian matrix of the map from \(\boldsymbol{\beta}\) to this vector has row \(g\) equal to \(\widehat\mu_g\boldsymbol{x}_g^\mathsf{T}\). Thus the delta-method covariance estimate for the vector of means is \(\widehat{\boldsymbol J}\widehat{\boldsymbol V}\widehat{\boldsymbol J}^\mathsf{T}\). Its \((g,h)\) entry is \(\widehat\mu_g\widehat\mu_h\boldsymbol{x}_g^\mathsf{T}\widehat{\boldsymbol V}\boldsymbol{x}_h\).

Equivalent form: The variance estimate for the total can be written as \(\sum_{g=1}^G\sum_{h=1}^G\widehat{\operatorname{Cov}}(\widehat\mu_g,\widehat\mu_h)\), where \(\widehat{\operatorname{Cov}}(\widehat\mu_g,\widehat\mu_h)=\widehat\mu_g\widehat\mu_h\boldsymbol{x}_g^\mathsf{T}\widehat{\boldsymbol V}\boldsymbol{x}_h\). The diagonal terms are the fitted-mean variances; the off-diagonal terms are essential whenever predictions share uncertain coefficients.

To verify the equivalent form, set \(\widehat{\boldsymbol a}=\sum_g\widehat\mu_g\boldsymbol{x}_g\). Expanding the matrix product gives \(\widehat{\boldsymbol a}^\mathsf{T}\widehat{\boldsymbol V}\widehat{\boldsymbol a} =\sum_g\sum_h\widehat\mu_g\widehat\mu_h\boldsymbol{x}_g^\mathsf{T}\widehat{\boldsymbol V}\boldsymbol{x}_h\). This is precisely the sum of the entries of the estimated covariance matrix of the fitted means, as required for the variance of their sum. In particular, the off-diagonal terms need not all be positive; their signs depend on the coefficient covariance and the group design vectors. The overall variance estimate remains nonnegative when \(\widehat{\boldsymbol V}\) is positive semidefinite, because it is a quadratic form in that matrix.

Worked Calculations

Worked Example: Two Groups Sharing an Intercept

Suppose a fitted model has coefficients \(\widehat{\boldsymbol{\beta}}=(\widehat\beta_0,\widehat\beta_1)^\mathsf{T}=(\log 10,\log 2)^\mathsf{T}\). The group design vectors are \(\boldsymbol{x}_1=(1,0)^\mathsf{T}\) and \(\boldsymbol{x}_2=(1,1)^\mathsf{T}\), with zero offsets. The estimated coefficient covariance matrix is \(\widehat{\boldsymbol V}=\begin{pmatrix}0.04&0.01\\0.01&0.0225\end{pmatrix}\). Find the fitted expected total and its delta-method variance estimate.

The two fitted means are \(\widehat\mu_1=\exp(\log 10)=10\) and \(\widehat\mu_2=\exp(\log 10+\log 2)=20\). Hence \(\widehat T=10+20=30\).

The gradient of the total at the fitted coefficients is \(\widehat{\boldsymbol a}=10(1,0)^\mathsf{T}+20(1,1)^\mathsf{T}=(30,20)^\mathsf{T}\). Therefore \[ \widehat{\operatorname{Var}}(\widehat T) =(30,20) \begin{pmatrix}0.04&0.01\\0.01&0.0225\end{pmatrix} \begin{pmatrix}30\\20\end{pmatrix} =36+12+9=57. \] The estimated standard error is \(\sqrt{57}\approx7.55\).

The covariance accounting gives the same answer. The first fitted mean has estimated variance \(10^2(0.04)=4\). For the second, \(\boldsymbol{x}_2^\mathsf{T}\widehat{\boldsymbol V}\boldsymbol{x}_2=0.04+0.01+0.01+0.0225=0.0825\), so its estimated variance is \(20^2(0.0825)=33\). Their estimated covariance is \(10(20)(0.04+0.01)=10\). Thus the total variance is \(4+33+2(10)=57\).

Worked Example: Three Groups and a Negative Covariance

Consider three profiles with design vectors \((1,0)^\mathsf{T}\), \((1,1)^\mathsf{T}\), and \((1,2)^\mathsf{T}\), zero offsets, and fitted means \(10\), \(20\), and \(40\). Suppose \(\widehat{\boldsymbol V}=\begin{pmatrix}0.01&-0.002\\-0.002&0.003\end{pmatrix}\). Calculate the fitted total and its variance estimate.

The total is \(\widehat T=10+20+40=70\), and its gradient is \(\widehat{\boldsymbol a}=10(1,0)^\mathsf{T}+20(1,1)^\mathsf{T}+40(1,2)^\mathsf{T}=(70,100)^\mathsf{T}\). Consequently, \[ \widehat{\operatorname{Var}}(\widehat T) =70^2(0.01)+2(70)(100)(-0.002)+100^2(0.003) =49-28+30=51. \] The estimated standard error is \(\sqrt{51}\approx7.14\).

The negative cross-product term is not an error: the estimated covariance between the two coefficients is negative. As a check by components, the three individual mean variances are \(1\), \(3.6\), and \(22.4\). The three pairwise mean covariances are \(1.6\), \(2.4\), and \(8\). The total variance is therefore \(1+3.6+22.4+2(1.6+2.4+8)=51\), in agreement with the gradient calculation.

Worked Example: Exposure Offsets in a Group Total

A model uses an intercept and a binary group indicator, with coefficient estimates \((\log 2,\log 3)^\mathsf{T}\). The groups have known exposures \(4\) and \(5\), so their offsets are \(\log 4\) and \(\log 5\). Their design vectors are \((1,0)^\mathsf{T}\) and \((1,1)^\mathsf{T}\). Suppose the coefficient covariance estimate is \(\widehat{\boldsymbol V}=\begin{pmatrix}0.01&0\\0&0.04\end{pmatrix}\).

The first fitted mean is \(\exp(\log4+\log2)=8\). The second is \(\exp(\log5+\log2+\log3)=30\). The fitted expected total is \(8+30=38\). The offsets are fixed, so they affect the means but contribute no coefficient uncertainty.

The total gradient is \(8(1,0)^\mathsf{T}+30(1,1)^\mathsf{T}=(38,30)^\mathsf{T}\), giving \[ \widehat{\operatorname{Var}}(\widehat T) =38^2(0.01)+30^2(0.04) =14.44+36=50.44. \] Equivalently, the separate variances are \(8^2(0.01)=0.64\) and \(30^2(0.01+0.04)=45\), while their covariance is \(8(30)(0.01)=2.4\). Thus \(0.64+45+2(2.4)=50.44\).

Why the Covariance Cannot Usually Be Dropped

A frequent mistake is to calculate a standard error for each fitted mean, square those standard errors, and add them as if the predictions were independent. In the first example, that would give \(4+33=37\), rather than \(57\), because it omits twice the covariance of \(10\). Predictions for groups sharing an intercept or other coefficients are typically dependent as estimates, even when the groups themselves are distinct.

The fitted total is a sum of functions of one common coefficient estimate. Its variance must reflect the uncertainty in that common estimate. In some cases covariances are negative, and omitting them can instead overstate the variance. The safe calculation is to form the total gradient first and evaluate its quadratic form with the coefficient covariance matrix, or to sum all entries of the fitted-means covariance matrix.

This calculation concerns uncertainty in the model-based expected total arising from estimating the coefficients. It does not add the random variation of a future count around its conditional mean. Nor does it automatically account for uncertainty in offsets, group definitions, or model specification. Those require additional assumptions and calculations beyond this coefficient-based variance.

Key takeaway: Add the fitted group means to obtain the aggregate expected count. For its delta-method variance, use the gradient \(\sum_g\widehat\mu_g\boldsymbol{x}_g\) and the coefficient covariance matrix. Shared coefficients induce covariances among the group predictions, so separate standard errors cannot generally be combined as if independent.

Check Your Understanding

Work through these questions using the group-sum and covariance formulas.

  1. For two fitted means, explain why the variance of their sum contains twice their covariance.
  2. How does a fixed log-exposure offset affect a group’s fitted mean and the gradient with respect to the coefficients?
  3. What assumptions allow the delta-method variance estimate for a fitted total to be interpreted as a large-sample approximation?
  4. In what circumstance could omitting a covariance term make the estimated variance too large rather than too small?
  5. Does the coefficient-based variance of a fitted expected total include the random variation of a future observed count? Explain.
Clinical Biostats