From Likelihood Derivatives to Fitted Residuals
A coefficient score is the derivative of the log-likelihood with respect to a regression coefficient. In a GLM, differentiating first with respect to the linear predictor and then applying the chain rule gives a direct connection between the score and the difference between each observed response and its fitted mean. This tutorial works out that connection for three common models: Gaussian identity, Bernoulli logit, and Poisson log-link.
Assume independent observations \(Y_1,\ldots,Y_n\), fixed covariate vectors \(x_i\in\mathbb{R}^p\), and linear predictors \(\eta_i=x_i^\mathsf{T}\beta\), where \(\beta\in\mathbb{R}^p\). Write \(\mu_i=E[Y_i]\) for the model mean and \(\hat\mu_i\) for its fitted value at an estimated coefficient vector \(\hat\beta\). Define the response residual \(r_i=y_i-\mu_i\) at a given coefficient vector; at the fit this becomes \(y_i-\hat\mu_i\). A score equation sets each component of the likelihood gradient equal to zero.
The calculations below use the usual maximum-likelihood convention: the Gaussian variance \(\sigma^2>0\) is common across observations, and the Bernoulli and Poisson observations have the stated distributions. The examples have finite fitted coefficient values, so the likelihood derivatives and score equations apply at their fits.
Gaussian Identity-Link Model
For independent \(Y_i\sim N(x_i^\mathsf{T}\beta,\sigma^2)\), the identity link gives \(\mu_i=\eta_i=x_i^\mathsf{T}\beta\). Treating \(\sigma^2\) as fixed, the log-likelihood, up to no omitted \(\beta\)-dependent terms, is
For coefficient \(\beta_j\), the derivative of the squared-error term is \(\partial (y_i-x_i^\mathsf{T}\beta)^2/\partial\beta_j=-2(y_i-x_i^\mathsf{T}\beta)x_{ij}\). Therefore
Thus the fitted Gaussian coefficient equations require each covariate-weighted residual sum to be zero. The common factor \(1/\sigma^2\) does not affect their roots. Estimating a common variance as well does not change this coefficient score.
Worked Example: Gaussian Identity Regression
Take four observations with \(x_i=0,1,2,3\) and responses \(y_i=1,2,2,4\). Fit the model \(\mu_i=\beta_0+\beta_1x_i\). The means are \(\bar x=1.5\) and \(\bar y=2.25\). The least-squares slope and intercept are
The fitted means are \(0.9,1.8,2.7,3.6\), so the residuals \(y_i-\hat\mu_i\) are \(0.1,0.2,-0.7,0.4\). Check the intercept score and slope score separately:
Both coefficient scores are zero; the Gaussian score components are these sums divided by \(\sigma^2\). This checks the likelihood equations directly, not just the reported coefficient values.
Bernoulli Logit Model
For independent \(Y_i\sim\operatorname{Bernoulli}(p_i)\), the logit link specifies \(\log\{p_i/(1-p_i)\}=\eta_i=x_i^\mathsf{T}\beta\). Equivalently, \(p_i=e^{\eta_i}/(1+e^{\eta_i})\). The log-likelihood is
To differentiate, note that \(dp_i/d\eta_i=p_i(1-p_i)\). The derivative of an observation's log-likelihood with respect to \(\eta_i\) is
Since \(\partial\eta_i/\partial\beta_j=x_{ij}\), the chain rule yields \(U_j(\beta)=\sum_i x_{ij}(y_i-p_i)\). In vector form, \(U(\beta)=X^\mathsf{T}(y-p)\). Here the response residual is \(y_i-p_i\), not a difference between \(y_i\) and the linear predictor.
Worked Example: Bernoulli Logit With Two Groups
Use an intercept and a group indicator \(x_i\), equal to 0 for three observations with outcomes \(1,0,0\), and equal to 1 for four observations with outcomes \(1,1,0,1\). The model has \(\eta_i=\beta_0+\beta_1x_i\). The observed success proportions are \(1/3\) in group 0 and \(3/4\) in group 1. The fitted parameters matching these proportions are
These coefficients give fitted probabilities \(1/3\) for group 0 and \(\operatorname{logit}^{-1}(-\log2+\log6)=\operatorname{logit}^{-1}(\log3)=3/4\) for group 1. In group 0, residuals are \(2/3,-1/3,-1/3\), whose sum is zero. In group 1, residuals are \(1/4,1/4,-3/4,1/4\), also summing to zero. Consequently,
The intercept score checks the total residual, while the group-indicator score checks the residual total in group 1. Together they verify both coefficient equations.
Poisson Log-Link Model
For independent \(Y_i\sim\operatorname{Poisson}(\mu_i)\), the log link gives \(\log\mu_i=\eta_i=x_i^\mathsf{T}\beta\), so \(\mu_i=e^{\eta_i}\). The log-likelihood is
The factorial term does not depend on \(\beta\). Differentiating the remaining terms first with respect to \(\eta_i\) gives \(y_i-e^{\eta_i}=y_i-\mu_i\); applying the chain rule gives
As in the logit model, each coefficient score is a covariate-weighted sum of response residuals, but the fitted mean is now \(e^{x_i^\mathsf{T}\beta}\).
Worked Example: Poisson Log-Link With Two Groups
Let \(x_i=0\) for counts \(0,1,2\), and \(x_i=1\) for counts \(1,2,3,2\). The group mean counts are \(1\) and \(2\). Under \(\log\mu_i=\beta_0+\beta_1x_i\), the fitted coefficients are
Thus fitted means are 1 for every group-0 observation and 2 for every group-1 observation. The residuals are \(-1,0,1\) in group 0 and \(-1,0,1,0\) in group 1. Their group totals are both zero, so
The zero intercept score and zero group-indicator score confirm the fitted Poisson coefficient equations.
What the Residual Checks Establish
The three models have different likelihoods and fitted mean functions, but their coefficient scores share the same residual structure. The Gaussian score has the additional common factor \(1/\sigma^2\); for Bernoulli logit and Poisson log-link, the coefficient score is exactly \(X^\mathsf{T}(y-\mu)\). The following statement makes the consequence of a zero score explicit.
Proof. In the Gaussian model the score is \(\sigma^{-2}X^\mathsf{T}(y-\mu)\), and \(\sigma^2>0\), so a zero score implies \(X^\mathsf{T}(y-\mu)=0\). In the Bernoulli logit and Poisson log-link models, the score is already \(X^\mathsf{T}(y-\mu)\), so the same implication holds directly. If the model includes an intercept, one column of \(X\) consists of ones. The component of \(X^\mathsf{T}(y-\mu)\) corresponding to that column is \(\sum_i(y_i-\mu_i)\); because every component is zero, this sum is zero. At a fitted solution, \(\mu_i=\hat\mu_i\), giving the stated fitted-residual checks. \(\square\)
These equations express a covariate-weighted balance: positive and negative residuals must cancel after weighting by each model covariate. They do not require every individual residual to be zero. Nor do they, by themselves, prove that a zero-score point is a maximum or that it is the only solution. The existence and finiteness of solutions are separate questions.
Common Mistake: Checking the Wrong Quantity
A frequent error is to set a score equal to zero using \(y_i-\eta_i\), as if the linear predictor were the fitted response. The score depends on the likelihood through the fitted mean: it uses \(y_i-\eta_i\) only when the particular mean function makes \(\mu_i=\eta_i\), as in the Gaussian identity-link model. For Bernoulli logit, the fitted mean is \(p_i=1/(1+e^{-\eta_i})\); for Poisson log-link, it is \(\mu_i=e^{\eta_i}\). Therefore the correct residuals are \(y_i-p_i\) and \(y_i-e^{\eta_i}\), respectively.
A second related mistake is to check only that the residuals sum to zero. That verifies the intercept equation when an intercept is present, but not the other coefficient equations. Each additional covariate requires its own weighted residual sum to vanish. A reliable check computes the fitted means first, forms the response residuals, and then evaluates every column of \(X^\mathsf{T}(y-\hat\mu)\).
Use the model's mean function, not the linear predictor itself unless the identity link applies.
For each observation calculate \(y_i-\hat\mu_i\).
For each design column \(j\), calculate \(\sum_i x_{ij}(y_i-\hat\mu_i)\); for the Gaussian model, the score includes the factor \(1/\sigma^2\).
Check Your Understanding
Use the likelihood score and fitted mean—not only the form of the linear predictor—to answer these questions.
- For a Gaussian identity-link model with common variance \(\sigma^2\), write the score for coefficient \(\beta_j\).
- In the Bernoulli logit model, what is the derivative of one observation's log-likelihood with respect to \(\eta_i\)?
- For the Poisson log-link model, identify the fitted mean and write the coefficient score vector.
- Why does a zero intercept score imply that the residuals sum to zero? What additional checks are needed when there are other covariates?
- Explain why \(y_i-\eta_i\) is generally not the correct response residual in either the Bernoulli logit or Poisson log-link model.