“If all you have is a hammer, everything looks like a nail.” - Bernard Baruch
Up to now the response variable has been quantitative. Logistic regression is for a categorical response, most often one with two possible outcomes: yes or no, success or failure, present or absent.
When \(Y\) is binary, the mean response is a probability. Logistic regression models that probability in a way that keeps the fitted values between 0 and 1. This chapter covers why a straight line is a poor model for a probability, where the S-shaped logistic curve comes from, how the model is fit, and how to read its coefficients. It ends with some problems that come up only with binary data. The next chapter extends the model to several predictors and uses it for classification.
23.1 Binary Response Variable
In many regression problems the response has only two possible qualitative outcomes, so it can be represented by an indicator variable that takes the values 0 and 1. Such a response is called a binary or dichotomous response.
We first look at what the response function means when the response is binary, and then at the problems a binary response causes for ordinary least squares.
23.1.1 Response Function Meaning
Consider the simple linear regression model \[
\begin{align*}
y_{i} & =\beta_{0}+\beta_{1}x_{i}+\varepsilon_{i}\qquad y_{i}=0,1
\end{align*}
\] where the outcome \(y_{i}\) is either 0 or 1.
The expected response \(E\left[ y_{i}\right]\) has a special meaning here. Since \(E\left[ \varepsilon_{i}\right] =0\), we have \[
\begin{align*}
E\left[ y_{i}\right] & =\beta_{0}+\beta_{1}x_{i}
\end{align*}
\]
Now think of \(y_{i}\) as a Bernoulli random variable with probability distribution \[
\begin{align*}
y_{i} & \quad\textbf{ Probability}\\
1 & \quad P\left(y_{i}=1\right)={\pi_{i}}\\
0 & \quad P\left(y_{i}=0\right)={1-\pi_{i}}
\end{align*}
\]
So \(\pi_{i}\) is the probability that \(y_{i}=1\), and \(1-\pi_{i}\) is the probability that \(y_{i}=0\). By the definition of the expected value of a discrete random variable, \[
\begin{align*}
E\left[ y_{i}\right] & =1\left(\pi_{i}\right)+0\left(1-\pi_{i}\right)\\
& =\pi_{i}\\
& =P\left(y_{i}=1\right)
\end{align*}
\]
Putting the two together, \[
\begin{align*}
E\left[ y_{i}\right] & =\beta_{0}+\beta_{1}x_{i}\\
& =\pi_{i}
\end{align*}
\]
The mean response given by the response function is the probability that \(y_i = 1\) when the predictor is at the level \(x_i\). This holds whatever the form of the response function, whether it is a simple linear one, as here, or a complicated multiple regression one. When the response is a 0/1 indicator, the mean response is always the probability that \(Y = 1\) at the given levels of the predictors.
Example 23.1
ExampleThe mean of a binary response is a probability
Suppose \(Y\) indicates whether a student passes an exam:
\(Y = 1\) means the student passes,
\(Y = 0\) means the student does not pass.
If the probability of passing is \(\pi = 0.80\), then
\[
E(Y) = 1(0.80) + 0(0.20) = 0.80.
\]
For a binary response, the expected value is the probability that the outcome equals 1. It is not an average outcome in the usual sense; no student scores “0.80 of a pass.”
The sample mean works the same way. If 18 of 25 students in a class pass, the sample mean of the 0/1 responses is \(18/25 = 0.72\), which is the proportion who passed.
23.1.2 Problems When the Response Is Binary
A binary response causes three problems for the linear model. We look at each using simple linear regression.
Nonnormal error terms. When the response is 0 or 1, each error term can take only two values: \[
\begin{align*}
\text{When }y_{i}=1: & \quad\varepsilon_{i}=1-\beta_{0}-\beta_{1}x_{i}\\
\text{When }y_{i}=0: & \quad\varepsilon_{i}=0-\beta_{0}-\beta_{1}x_{i}
\end{align*}
\] A variable with two possible values cannot be normally distributed, so the normal error regression model does not apply.
Nonconstant error variance. The error terms do not have equal variances. Since \(y_i\) is Bernoulli, \[
\begin{align*}
Var[\varepsilon_i] = Var[y_i] = \pi_i(1-\pi_i)
\end{align*}
\] which depends on \(\pi_i\), and \(\pi_i\) usually depends on \(x_i\). The error variances therefore differ at different levels of \(X\), and ordinary least squares is no longer the best linear unbiased method.
Constraints on the response function. Since the response function gives probabilities, the mean responses have to satisfy \[
0\le E\{Y\}=\pi \le 1
\]
This third problem is the most serious one. Weighted least squares can handle unequal variances, and with large samples the least squares estimators are approximately normal under fairly general conditions even when the errors are far from normal. A straight line, though, will eventually go below 0 or above 1 if \(x\) ranges far enough, and that often rules out a linear response function altogether.
The variance \(\pi(1-\pi)\) is largest at \(\pi = 0.5\) and shrinks toward zero as \(\pi\) approaches 0 or 1.
library(tidyverse)library(broom)tibble(probability =seq(0, 1, length.out =200)) |>mutate(variance = probability * (1- probability)) |>ggplot(aes(x = probability, y = variance)) +geom_line(linewidth =1, color ="#0072B2") +labs(x =expression(pi),y =expression(pi(1- pi)),title ="The variance of a binary response depends on its mean" ) +theme_minimal()
An observation with \(\pi = 0.5\) has variance 0.25, and one with \(\pi = 0.05\) has variance 0.0475, about one fifth as much. Ordinary least squares weights them equally.
Example 23.2
ExampleA straight line fit to a binary response
In the mtcars data, am equals 1 for a manual transmission and 0 for an automatic. We fit a straight line to am using weight, wt, in thousands of pounds. A linear model fit to a binary response is called a linear probability model. For comparison we also fit the logistic regression model that this chapter builds up to.
mtcars_lpm <-lm(am ~ wt, data = mtcars)mtcars_logit <-glm(am ~ wt, data = mtcars, family = binomial)mtcars |>mutate(lpm_fitted =fitted(mtcars_lpm)) |>summarize(min_fitted =min(lpm_fitted),max_fitted =max(lpm_fitted),n_below_0 =sum(lpm_fitted <0),n_above_1 =sum(lpm_fitted >1) )
wt_grid <-tibble(wt =seq(1, 6, length.out =300))lpm_curves <-bind_rows( wt_grid |>mutate(fitted =predict(mtcars_lpm, newdata = wt_grid),model ="Straight line (least squares)" ), wt_grid |>mutate(fitted =predict(mtcars_logit, newdata = wt_grid, type ="response"),model ="Logistic curve" ))set.seed(23)ggplot() +annotate("rect", xmin =1, xmax =6, ymin =-0.6, ymax =0,fill ="#D55E00", alpha =0.12) +annotate("rect", xmin =1, xmax =6, ymin =1, ymax =1.2,fill ="#D55E00", alpha =0.12) +geom_jitter(data = mtcars, aes(x = wt, y = am),width =0, height =0.03, alpha =0.7) +geom_line(data = lpm_curves,aes(x = wt, y = fitted, color = model), linewidth =1) +geom_hline(yintercept =c(0, 1), linetype ="dashed", color ="gray50") +scale_color_manual(values =c("Logistic curve"="#0072B2","Straight line (least squares)"="#D55E00")) +labs(x ="Weight (1000 lbs)",y ="Manual transmission (1 = yes)",color =NULL,title ="Two models for P(manual) as a function of weight",subtitle ="Shaded regions are impossible values for a probability" ) +theme_minimal() +theme(legend.position ="bottom")
The straight line gives negative fitted probabilities for the three heaviest cars, as low as -0.37, and a fitted probability slightly above 1 for the lightest car. The logistic curve flattens out near 0 and 1 and stays inside the possible range for every weight.
The residuals from the linear probability model show the first two problems directly.
augment(mtcars_lpm) |>ggplot(aes(x = .fitted, y = .resid, color =factor(am))) +geom_point(size =2) +geom_hline(yintercept =0, linetype ="dashed") +scale_color_manual(values =c("0"="#D55E00", "1"="#0072B2")) +labs(x ="Fitted value",y ="Residual",color ="am",title ="Residuals from a straight line fit to a 0/1 response" ) +theme_minimal()
Every residual falls on one of two parallel lines, \(1 - \hat{y}\) for the manual cars and \(0 - \hat{y}\) for the automatic cars. No transformation or outlier removal will make that look like a normal distribution with constant variance. A residual plot with two diagonal bands like this is the signature of a binary response.
NoteThe biggest problem is the probability scale
For binary responses, nonnormality and nonconstant variance matter. The most visible problem, though, is that a straight-line model can predict values below 0 or above 1.
Logistic regression handles this by modeling a transformed probability as a linear function of the predictors, which keeps the fitted probability itself between 0 and 1.
The linear probability model is not useless. When all the fitted probabilities stay between about 0.2 and 0.8, the logistic curve is close to a straight line, and the two models give similar answers. Some fields, economics in particular, use it often for that reason, because its coefficients are changes in probability and are easy to explain. The trouble comes near 0 and 1, and whenever the model is used to predict outside the range of the data.
23.2 Sigmoidal Response Functions
This section looks at two response functions for binary responses. Both are bounded between 0 and 1, have a sigmoidal (S) shape, and approach 0 and 1 asymptotically.
These functions come up naturally when the binary response is a 0/1 recoding (a dichotomization) of an underlying continuous variable. They often work well for binary responses that have no underlying continuous variable, too.
23.2.1 Probit Mean Response Function
Consider a health researcher studying the effect of a mother’s alcohol use during pregnancy (\(x\), an index of the degree of use) on the length of the pregnancy (\(y^C\)). The superscript \(c\) is a reminder that pregnancy length is a continuous response. It can be represented by a simple linear regression model \[
y^C_i = \beta_0^c+\beta_1^c x_i +\varepsilon_i^c
\] where \(\varepsilon^c_i\) is normally distributed with mean zero and variance \(\sigma^2_c\).
If pregnancy length were available, we could proceed with simple linear regression. Suppose instead that the researchers recorded each pregnancy only as preterm or full term, using the rule \[
\begin{align*}
y_{i} & =\begin{cases}
1 & \text{ if }y_{i}^{c}\le38\text{ weeks (preterm)}\\
0 & \text{ if }y_{i}^{c}>38\text{ weeks (full term)}
\end{cases}
\end{align*}
\]
Then \[
\begin{align*}
P\left(y_{i}=1\right)=\pi_{i} & =P\left(y_{i}^{c}\le38\right)\\
& =P\left(\beta_{0}^{c}+\beta_{1}^{c}x_{i}+\varepsilon_{i}^{c}\le38\right)\\
& =P\left(\varepsilon_{i}^{c}\le38-\beta_{0}^{c}-\beta_{1}^{c}x_{i}\right)\\
& =P\left(\frac{\varepsilon_{i}^{c}}{\sigma_{c}}\le\frac{38-\beta_{0}^{c}}{\sigma_{c}}-\frac{\beta_{1}^{c}}{\sigma_{c}}x_{i}\right)\\
& =P\left(Z\le\beta_{0}^{*}+\beta_{1}^{*}x_{i}\right)
\end{align*}
\] where \[
\begin{align*}
\beta_{0}^{*} & =\frac{38-\beta_{0}^{c}}{\sigma_{c}}\\
\beta_{1}^{*} & =-\frac{\beta_{1}^{c}}{\sigma_{c}}\\
Z & =\frac{\varepsilon_{i}^{c}}{\sigma_{c}}.
\end{align*}
\]
\(Z\) has a standard normal distribution. Writing \(P\left(Z\le z\right)=\Phi\left(z\right)\), \[
\begin{align*}
P\left(y_{i}=1\right) & =\Phi\left(\beta_{0}^{*}+\beta_{1}^{*}x_{i}\right)
\end{align*}
\]
This gives the probit mean response function \[
\begin{align*}
E\left[ y_{i}\right] & =\pi_{i}=\Phi\left(\beta_{0}^{*}+\beta_{1}^{*}x_{i}\right)
\end{align*}
\]
The inverse \(\Phi^{-1}\) of the standard normal CDF is called the probit transformation. Applying it to both sides gives \[
\begin{align*}
\Phi^{-1}\left(\pi_{i}\right) & =\pi_{i}^{\prime}=\beta_{0}^{*}+\beta_{1}^{*}x_{i}
\end{align*}
\]
The expression \(\pi_{i}^{\prime}=\beta_{0}^{*}+\beta_{1}^{*}x_{i}\) is called the probit response function or, more generally, the linear predictor.
The derivation shows two more things. First, we can only estimate \(\beta_0^c\) and \(\beta_1^c\) divided by \(\sigma_c\). Dichotomizing throws away the scale of the continuous response, so the binary data cannot tell us how many weeks of pregnancy a unit of \(x\) is worth, only how much it shifts the probability of a preterm birth. Second, the sign flips: alcohol use that shortens pregnancy (\(\beta_1^c < 0\)) raises the probability of a preterm birth (\(\beta_1^* > 0\)), because \(y = 1\) was defined as the short outcome.
Example 23.3
ExampleHow probit coefficients affect the S-shaped curve
The probit mean response function is
\[
\pi_i = \Phi(\beta_0^*+\beta_1^*x_i).
\]
The intercept shifts the curve left or right, while the slope controls how quickly the probability changes.
Both curves stay between 0 and 1. On the probability scale they are S-shaped; on the probit scale, \(\Phi^{-1}(\pi)\), they are straight lines in \(x\).
23.3 Logistic Mean Response Function
Assuming normal errors for the underlying continuous response led to the standard normal CDF, \(\Phi\), as the model for \(\pi_i\). Another error distribution, very similar to the normal, is the logistic distribution. The standard normal density and the logistic density with mean zero and variance one look much alike. The logistic has slightly heavier tails.
Example 23.4
ExampleNormal and logistic densities are similar
The following plot compares the standard normal density to a rescaled logistic density with mean 0 and variance 1.
density_grid <-tibble(x =seq(-4, 4, length.out =400)) |>mutate(normal =dnorm(x),logistic =dlogis(x, scale =sqrt(3) / pi) ) |>pivot_longer(cols =c(normal, logistic),names_to ="distribution",values_to ="density" )ggplot(density_grid, aes(x = x, y = density, color = distribution)) +geom_line(linewidth =1) +scale_color_manual(values =c(logistic ="#0072B2", normal ="#D55E00")) +labs(x ="value",y ="density",color ="Distribution",title ="Normal and logistic densities, both with variance 1" ) +theme_minimal()
The logistic density is a little taller in the middle and a little heavier in the tails. Because the two are so close, probit and logistic regression usually give very similar fitted probabilities.
The cumulative distribution function of a logistic random variable \(\varepsilon_{L}\) with mean 0 and standard deviation \(\sigma=\pi/\sqrt{3}\) is \[
\begin{align*}
F_{L}\left(\varepsilon_{L}\right) & =\frac{\exp\left(\varepsilon_{L}\right)}{1+\exp\left(\varepsilon_{L}\right)}
\end{align*}
\] (Here \(\pi\) is the number 3.14159…, not a probability. The notation is unfortunate but standard.)
Suppose now that \(\varepsilon_{i}^{c}\) has a logistic distribution with mean 0 and standard deviation \(\sigma_{c}\). Then \[
\begin{align*}
P\left(y_{i}=1\right) & =P\left(\frac{\varepsilon_{i}^{c}}{\sigma_{c}}\le\beta_{0}^{*}+\beta_{1}^{*}x_{i}\right)
\end{align*}
\] where \(\varepsilon_{i}^{c}/\sigma_{c}\) has a logistic distribution with mean zero and standard deviation one. Multiplying both sides of the inequality by \(\pi/\sqrt{3}\) gives \[
\begin{align*}
P\left(y_{i}=1\right)=\pi_{i} & =P\left(\frac{\pi}{\sqrt{3}}\frac{\varepsilon_{i}^{c}}{\sigma_{c}}\le\frac{\pi}{\sqrt{3}}\beta_{0}^{*}+\frac{\pi}{\sqrt{3}}\beta_{1}^{*}x_{i}\right)\\
& =P\left(\varepsilon_{L}\le\beta_{0}+\beta_{1}x_{i}\right)\\
& =F_{L}\left(\beta_{0}+\beta_{1}x_{i}\right)\\
& =\frac{\exp\left(\beta_{0}+\beta_{1}x_{i}\right)}{1+\exp\left(\beta_{0}+\beta_{1}x_{i}\right)}
\end{align*}
\] where \[
\begin{align*}
\beta_{0} & =\frac{\pi}{\sqrt{3}}\beta_{0}^{*}\\
\beta_{1} & =\frac{\pi}{\sqrt{3}}\beta_{1}^{*}
\end{align*}
\] are the logistic regression parameters.
To summarize, the logistic mean response function is \[
\begin{align*}
E\left[ y_{i}\right] & =\pi_{i}\\
& =F_{L}\left(\beta_{0}+\beta_{1}x_{i}\right)\\
& =\frac{\exp\left(\beta_{0}+\beta_{1}x_{i}\right)}{1+\exp\left(\beta_{0}+\beta_{1}x_{i}\right)}\\
& =\frac{1}{1+\exp\left(-\beta_{0}-\beta_{1}x_{i}\right)}
\end{align*}
\]
Applying the inverse of \(F_{L}\) gives \[
\begin{align*}
F_{L}^{-1}\left(\pi_{i}\right) & =\beta_{0}+\beta_{1}x_{i}=\pi_{i}^{\prime}
\end{align*}
\]
\(F_{L}^{-1}\left(\pi_{i}\right)\) is called the logit transformation: \[
\begin{align*}
F_{L}^{-1}\left(\pi_{i}\right) & =\log\left(\frac{\pi_{i}}{1-\pi_{i}}\right)
\end{align*}
\] where the ratio \(\pi_{i}/\left(1-\pi_{i}\right)\) is called the odds.
The relationship \(\beta = (\pi/\sqrt{3})\,\beta^* \approx 1.81\,\beta^*\) also tells us roughly how logistic and probit coefficients compare when both models are fit to the same data. The logistic coefficients come out about 1.6 to 1.8 times as large. The fitted probabilities are nearly the same; only the scale of the coefficients differs. Never compare a logistic coefficient from one study directly with a probit coefficient from another.
Example 23.5
ExampleHow logistic coefficients affect the probability curve
The sign of \(\beta_1\) determines whether the probability increases or decreases as \(x\) increases. Its magnitude controls how steep the transition is.
The relationship is linear on the logit scale and S-shaped on the probability scale. With \(\beta_0 = 0\), every curve passes through \(\pi = 0.5\) at \(x = 0\). In general the curve crosses \(0.5\) where the linear predictor is zero, at \(x = -\beta_0/\beta_1\).
Example 23.6
ExampleProbabilities, odds, and logits
The logit transformation converts a probability to log-odds:
\[
\log\left(\frac{\pi}{1-\pi}\right).
\]
The table below shows how probabilities, odds, and logits are connected.
When \(\pi = 0.50\), the odds are 1 and the logit is 0. Probabilities below 0.50 have negative logits, and probabilities above 0.50 have positive logits. The logit is symmetric: \(\pi\) and \(1 - \pi\) have logits of the same size and opposite sign.
The odds are not symmetric. A probability of 0.99 has odds of 99, while a probability of 0.01 has odds of about 0.0101. Odds run from 0 to infinity, so they stretch out a great deal near \(\pi = 1\) and are squashed near \(\pi = 0\). The plot shows the three scales side by side.
scale_levels <-c("Probability", "Odds", "Logit (log-odds)")tibble(eta =seq(-4, 4, length.out =200)) |>mutate(Probability =plogis(eta),Odds =exp(eta),`Logit (log-odds)`= eta ) |>pivot_longer(-eta, names_to ="scale", values_to ="value") |>mutate(scale =factor(scale, levels = scale_levels)) |>ggplot(aes(x = eta, y = value)) +geom_line(linewidth =1, color ="#0072B2") +facet_wrap(~ scale, scales ="free_y") +labs(x =expression(eta == beta[0] + beta[1] * x),y =NULL,title ="The same model on three scales" ) +theme_minimal()
A logistic model is a straight line on the logit scale, an exponential curve on the odds scale, and an S-curve on the probability scale.
23.3.1 Other Links
The function that connects the mean to the linear predictor, the logit \(\log(\pi/(1-\pi))\) or the probit \(\Phi^{-1}(\pi)\), is called the link function. A third link sometimes used for binary data is the complementary log-log, \[
\log\left(-\log(1-\pi)\right) = \beta_0 + \beta_1 x.
\] The logit and probit curves are symmetric about \(\pi = 0.5\): they approach 0 and 1 at the same rate. The complementary log-log curve is not. It rises slowly from 0 and approaches 1 quickly, which can fit better when the event of interest is rare at one end of the predictor range. In R the link is set with family = binomial(link = "logit"), "probit", or "cloglog".
Example 23.7
ExampleLogit and probit fits to the same data
We fit the transmission-by-weight model with all three links and compare the coefficients and fitted curves.
The logit slope is about 1.78 times the probit slope, in the range we expected. The fitted curves are hard to tell apart where there are data. With 32 cars there is no hope of distinguishing the links from the data alone. In practice the logit is the default because of the odds interpretation in the next section.
23.4 Fitting the Model: Maximum Likelihood
Least squares is not used to fit a logistic regression. Instead the coefficients are chosen by maximum likelihood: we choose the values of \(\beta_0\) and \(\beta_1\) that make the observed 0s and 1s as probable as possible.
Each \(y_i\) is Bernoulli with probability \(\pi_i\), so the probability of observing \(y_i\) can be written \(\pi_i^{y_i}(1-\pi_i)^{1-y_i}\). This equals \(\pi_i\) when \(y_i = 1\) and \(1 - \pi_i\) when \(y_i = 0\). If the observations are independent, the probability of the whole sample is the product, and its logarithm is the log-likelihood \[
\ell(\beta_0, \beta_1) = \sum_{i=1}^n \left[ y_i \log \pi_i + (1 - y_i)\log(1 - \pi_i) \right],
\qquad \pi_i = \frac{1}{1 + \exp(-\beta_0 - \beta_1 x_i)}.
\]
Unlike least squares for the linear model, there is no closed-form solution for the maximum. glm() finds it numerically with an algorithm called iteratively reweighted least squares (IRLS). Each step is a weighted least squares fit, with weights \(\pi_i(1-\pi_i)\) from the current estimates, so the observations with the largest variance get the least weight. The “Number of Fisher Scoring iterations” line in summary() reports how many steps it took.
Example 23.8
ExampleThe log-likelihood surface for the transmission model
We evaluate the log-likelihood for the mtcars model over a grid of \((\beta_0, \beta_1)\) values and mark the estimates from glm().
log_likelihood <-function(b0, b1, x = mtcars$wt, y = mtcars$am) { p <-plogis(b0 + b1 * x)sum(y *log(p) + (1- y) *log(1- p))}mle <-coef(mtcars_logit)mle
(Intercept) wt
12.04037 -4.02397
loglik_grid <-expand_grid(b0 =seq(2, 24, length.out =150),b1 =seq(-8, -0.5, length.out =150)) |>mutate(loglik =map2_dbl(b0, b1, log_likelihood))loglik_grid |>filter(loglik >-40) |>ggplot(aes(x = b0, y = b1, z = loglik)) +geom_contour_filled(breaks =c(-40, -30, -25, -20, -15, -12, -10, -9.6)) +annotate("point", x = mle[1], y = mle[2], color ="white", size =3) +labs(x =expression(beta[0]),y =expression(beta[1]),fill ="Log-likelihood",title ="Log-likelihood for am ~ wt",subtitle ="White point = maximum likelihood estimate from glm()" ) +theme_minimal()
The maximum of the surface is at the glm() estimates, and logLik() reports the same value we computed by hand. The surface is a long, narrow ridge running diagonally. Along the ridge, raising the intercept and making the slope more negative leaves the fit nearly unchanged, because all the cars weigh between 1.5 and 5.4 thousand pounds and \(\beta_0\) is the log-odds at a weight of zero, far from any data. This is the same correlation between intercept and slope estimates that we saw in linear regression when \(x\) is far from 0. Centering wt would turn the ridge into a rounder hill without changing the fitted probabilities.
The deviance reported by glm() is \(-2\) times the log-likelihood (plus a constant that is 0 for 0/1 data). It plays the role the residual sum of squares plays in linear regression: smaller is better, and the drop in deviance when a predictor is added is the basis for likelihood ratio tests, covered in the next chapter.
23.5 Interpretation of the Coefficients
The estimated coefficient \(\hat{\beta}_1\) in a fitted logistic model is not interpreted the way a slope in linear regression is. The effect of a unit increase in \(x\) on the probability depends on where on the \(x\) scale you start.
What does stay constant is the effect on the odds. For any unit increase in \(x\), the estimated odds \[
\frac{\hat{\pi}}{1-\hat{\pi}}
\] are multiplied by \[
\exp(\hat{\beta}_1).
\]
To see this, take the value of the fitted logit response function at \(X = x_j\): \[
\hat{\pi}^\prime (x_j)={\hat{\beta}_0+\hat{\beta}_1 x_j}
\] The notation \(\hat{\pi}^\prime (x_j)\) indicates the \(x\) level associated with the fitted value. At \(X = x_j + 1\), \[
\hat{\pi}^\prime (x_j+1)={\hat{\beta}_0+\hat{\beta}_1( x_j+1)}
\] The difference between the two is \[
\begin{align*}
\hat{\pi}^\prime (x_j+1)-\hat{\pi}^\prime (x_j)& = \hat{\beta}_0+\hat{\beta}_1( x_j+1) - \hat{\beta}_0-\hat{\beta}_1 x_j \\
& = \hat{\beta}_1
\end{align*}
\]
\(\hat{\pi}^\prime (x_j)\) is the log of the estimated odds when \(X=x_j\); call it \(\ln(\text{odds}_1)\). Likewise \(\hat{\pi}^\prime (x_j+1)\) is the log of the estimated odds when \(X=x_j+1\); call it \(\ln(\text{odds}_2)\). Then \[
\begin{align*}
\ln(\text{odds}_2)-\ln(\text{odds}_1) = \ln\left(\frac{\text{odds}_2}{\text{odds}_1}\right) = \hat{\beta}_1
\end{align*}
\]
Exponentiating both sides, the estimated ratio of the odds, called the odds ratio \(\hat{OR}\), is \[
\hat{OR} = \frac{\text{odds}_2}{\text{odds}_1}=\exp(\hat{\beta}_1)
\]
For an increase of \(c\) units instead of 1, the same argument gives an odds ratio of \(\exp(c\hat{\beta}_1)\).
NoteA logistic slope is not a probability slope
In simple linear regression, a slope is an additive change in the mean response. In logistic regression, \(\beta_1\) is an additive change in log-odds.
After exponentiating, \(\exp(\beta_1)\) is a multiplicative change in odds. It is not an additive change in probability.
Example 23.9
ExampleFitting a logistic regression model in R
We return to the transmission model. We want the probability that a car has a manual transmission as a function of weight, wt, in thousands of pounds.
mtcars_logistic_fit <-glm( am ~ wt,data = mtcars,family =binomial(link ="logit"))summary(mtcars_logistic_fit)
Call:
glm(formula = am ~ wt, family = binomial(link = "logit"), data = mtcars)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 12.040 4.510 2.670 0.00759 **
wt -4.024 1.436 -2.801 0.00509 **
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 43.230 on 31 degrees of freedom
Residual deviance: 19.176 on 30 degrees of freedom
AIC: 23.176
Number of Fisher Scoring iterations: 6
The coefficient of wt is negative, so heavier cars have lower estimated odds of a manual transmission. Each additional 1,000 pounds multiplies the odds by \(\exp(\hat\beta_1) \approx\) 0.0179, a decrease of about 98%.
A ton is a big change for a car, so a smaller unit may be easier to talk about. For 100 pounds, \(c = 0.1\):
exp(0.1*coef(mtcars_logistic_fit)["wt"])
wt
0.6687152
Each additional 100 pounds multiplies the odds of a manual transmission by about 0.67, a decrease of about 33%.
We can also compute fitted probabilities for particular weights.
The probability decreases as weight increases. How much it decreases for a given change in weight depends on where we are on the S-shaped curve.
23.5.1 Confidence Intervals for Odds Ratios
A confidence interval for an odds ratio is found by building the interval for \(\beta_1\) and then exponentiating both ends. The simplest interval is the Wald interval, \(\hat\beta_1 \pm z_{\alpha/2}\,SE(\hat\beta_1)\). R’s confint() for a glm instead computes a profile likelihood interval, which is based on the shape of the log-likelihood and usually behaves better in small samples. Setting conf.int = TRUE and exponentiate = TRUE in tidy() gives the profile interval on the odds ratio scale.
Example 23.10
ExampleWald and profile intervals for the weight odds ratio
Two things stand out. The intervals are not symmetric about the estimated odds ratio, because they were symmetric on the log-odds scale before exponentiating. Do not report an odds ratio as “estimate plus or minus something.” And the two methods give noticeably different intervals with only 32 cars. Both exclude 1, so both say heavier cars have lower odds of a manual transmission, but the profile interval is the one to trust here. With large samples the two agree closely.
23.5.2 Effects on the Probability Scale
People usually think in probabilities, not odds. The change in probability for a one-unit change in \(x\) is not constant, but there is a simple way to see how big it can be. The slope of the logistic curve at a point is \[
\frac{d\pi}{dx} = \beta_1\,\pi(1-\pi).
\] The factor \(\pi(1-\pi)\) is largest, 0.25, at \(\pi = 0.5\). So the steepest the curve ever gets is \(\beta_1/4\). This is sometimes called the divide-by-4 rule: \(\beta_1/4\) is an upper bound on the change in probability per unit of \(x\), and it is close to the actual change near the middle of the curve.
Example 23.11
ExampleThe same change in weight, different changes in probability
For the transmission model, \(\hat\beta_1/4 \approx\) -1.01 per 1,000 pounds. That number is a slope, so it describes small changes: near the middle of the curve, 100 more pounds lowers the probability of a manual transmission by about 0.1. It cannot be applied to a full 1,000-pound step here, since a probability cannot drop by more than 1. When \(|\beta_1|\) is large relative to the unit of \(x\), the curve bends a lot within one unit, and the rule only works for a fraction of a unit. We compute the actual changes for several 1,000-pound steps.
wt_grid |>mutate(probability =predict(mtcars_logistic_fit, newdata = wt_grid,type ="response")) |>ggplot(aes(x = wt, y = probability)) +geom_line(linewidth =1, color ="#0072B2") +geom_segment(data = steps,aes(x = wt_start, xend = wt_end, y = p_start, yend = p_start),linetype ="dotted") +geom_segment(data = steps,aes(x = wt_end, xend = wt_end, y = p_start, yend = p_end),color ="#D55E00", linewidth =1,arrow =arrow(length =unit(0.15, "cm"))) +geom_text(data = steps,aes(x = wt_end +0.08, y = (p_start + p_end) /2,label =sprintf("%.2f", change)),hjust =0, size =3.5, color ="#D55E00") +labs(x ="Weight (1000 lbs)",y ="P(manual)",title ="Each step is 1,000 lbs; the odds ratio is the same for all of them" ) +theme_minimal()
Every step has the same odds ratio, 0.0179. The change in probability ranges from almost nothing at the ends of the weight range to -0.76 in the middle, where the curve is steepest. A sentence like “each additional 1,000 pounds lowers the probability of a manual transmission by 0.2” would be wrong for most cars. If you want to report an effect on the probability scale, report it at specific values of \(x\), as in the table.
23.5.3 The Intercept
\(\beta_0\) is the log-odds that \(Y = 1\) when \(x = 0\), so \(\exp(\beta_0)\) is the odds and \(1/(1 + \exp(-\beta_0))\) is the probability at \(x = 0\). For the transmission model, that is the probability that a weightless car has a manual transmission, which means nothing. As in linear regression, the intercept has a useful interpretation only when \(x = 0\) is a meaningful value inside the range of the data. Centering the predictor (using \(x - \bar{x}\)) makes \(\beta_0\) the log-odds at the average \(x\) and does not change \(\beta_1\) or the fitted probabilities.
23.5.4 Odds Ratios Are Not Relative Risks
An odds ratio of 2 means the odds are doubled. It does not mean the probability is doubled. The ratio of two probabilities, \(\pi_2/\pi_1\), is called the relative risk, and it is a different quantity from the odds ratio. The two are close only when both probabilities are small, since then \(1 - \pi \approx 1\) and the odds are about the same as the probability.
Example 23.12
ExampleThe same odds ratio at different baseline probabilities
Suppose an exposure multiplies the odds of an event by 3. We compute the relative risk this implies for different baseline probabilities.
or_rr |>ggplot(aes(x = p_baseline, y = relative_risk)) +geom_hline(yintercept = odds_ratio, linetype ="dashed") +geom_line(linewidth =1, color ="#0072B2") +annotate("text", x =0.75, y =2.85, label ="odds ratio = 3") +labs(x ="Probability without the exposure",y ="Relative risk",title ="An odds ratio of 3 is a relative risk of 3 only for rare events" ) +theme_minimal()
When the event is rare without the exposure (a baseline probability near 1%), an odds ratio of 3 means the probability roughly triples. When the baseline probability is 50%, the same odds ratio raises the probability to 75%, a relative risk of only 1.5. News reports of “three times as likely,” based on an odds ratio from a study of a common outcome, often overstate the effect in exactly this way.
23.5.5 A Binary Predictor
When the predictor is itself a 0/1 indicator, the logistic model reproduces the odds ratio from the \(2 \times 2\) table of the two variables.
Example 23.13
ExampleLogistic regression on an indicator reproduces the 2 x 2 table
In mtcars, vs is 1 for a straight engine and 0 for a V-shaped engine. We model am on vs.
vs_fit <-glm(am ~ vs, data = mtcars, family = binomial)exp(coef(vs_fit))
(Intercept) vs
0.5 2.0
\(\exp(\hat\beta_0)\) is the odds of a manual transmission for V-shaped engines (the group with vs = 0), and \(\exp(\hat\beta_1)\) is the odds ratio comparing straight engines with V-shaped ones, both exactly as computed from the table. With a single binary predictor, logistic regression is a \(2 \times 2\) table analysis. Its advantage shows up when there are other predictors to adjust for, in the next chapter.
23.6 Grouped Binomial Data
Sometimes the data come already grouped: at each value of \(x\) we know how many trials there were and how many were successes. A dose-response experiment might give 50 insects each of six doses and record how many died at each dose. There is no need to expand this into one row per insect. If \(y_j\) is the number of successes out of \(n_j\) trials at \(x_j\), then \(y_j\) is binomial with probability \(\pi_j\), and the same logistic model applies.
In R, give glm() a two-column response of successes and failures with cbind().
Example 23.14
ExampleAge at menarche
The menarche data in the MASS package come from a 1965 study of 3,918 girls in Warsaw. For each of 25 age groups, it gives the mean age (Age), the number of girls (Total), and the number who had reached menarche (Menarche).
age_grid <-tibble(Age =seq(9, 18, length.out =200))menarche |>mutate(proportion = Menarche / Total) |>ggplot(aes(x = Age, y = proportion)) +geom_line(data = age_grid |>mutate(proportion =predict(menarche_fit, newdata = age_grid,type ="response")),color ="#0072B2", linewidth =1 ) +geom_point(aes(size = Total), alpha =0.6) +geom_vline(xintercept = median_age, linetype ="dashed") +annotate("text", x = median_age +0.15, y =0.1, hjust =0,label =sprintf("50%% at age %.1f", median_age)) +labs(x ="Age (years)",y ="Proportion who had reached menarche",size ="Girls in group",title ="Logistic regression fit to grouped binomial data" ) +theme_minimal()
With grouped data we can plot observed proportions against \(x\), which is not possible with 0/1 data, and the plot shows the logistic curve fitting these data very well. The age at which the fitted probability is 0.5 is \(-\hat\beta_0/\hat\beta_1 \approx\) 13.01 years. In dose-response studies the corresponding quantity, the dose at which half the subjects respond, is called the median effective dose, or LD50 when the response is death.
The same model can be fit three ways: with cbind(successes, failures), with the observed proportion as the response and weights = Total, or by expanding the data to one 0/1 row per individual. The coefficient estimates and standard errors are identical. The deviance and its degrees of freedom are not, because the deviance compares the model with a saturated model, and the saturated model is different for 25 groups than for 3,918 individuals.
So compare deviances only between models fit to data in the same form.
23.7 Things to Watch Out For
Logistic regression has some ways of going wrong that do not come up in linear regression. Here are the common ones.
23.7.1 Which Outcome Is Being Modeled?
With a 0/1 numeric response, glm() models \(P(Y = 1)\). With a factor response, it models the probability of the second level, treating the first level as failure. Factor levels are in alphabetical order unless you set them, so a response with levels “no” and “yes” models \(P(\text{yes})\), but one with levels “automatic” and “manual” models \(P(\text{manual})\) only because of the alphabet. If you relevel, the probability being modeled changes and every coefficient changes sign.
Example 23.15
ExampleChanging the reference level flips the coefficients
The first model gives the log-odds of manual, the second the log-odds of automatic. The estimates have the same size and opposite signs, and the odds ratios are reciprocals. Neither is wrong, but if you do not check which level is the event, you can report an effect backward.
A character response is not converted automatically, and glm() stops with an error:
tryCatch(glm(as.character(transmission) ~ wt, data = mtcars_trans, family = binomial),error = \(e) conditionMessage(e))
[1] "y values must be 0 <= y <= 1"
Be careful when moving between glm() and the tidymodels tools in the next chapter. The yardstick metrics treat the first factor level as the event by default (this can be changed with event_level = "second"), which is the opposite of glm().
23.7.2 Complete Separation
If some value of the predictor splits the data perfectly, with every \(y = 0\) on one side and every \(y = 1\) on the other, the maximum likelihood estimate does not exist. The likelihood keeps increasing as the curve gets steeper, so the estimated slope runs off toward infinity. This is called complete separation. If the split is perfect except for ties at the boundary, it is called quasi-complete separation, and some of the estimates still run off to infinity.
R does not stop with an error. glm() returns huge coefficients with even larger standard errors, usually with the warning “fitted probabilities numerically 0 or 1 occurred” and sometimes “algorithm did not converge.” Neither warning appears every time, so do not rely on them.
Example 23.16
ExampleSeparation in simulated data and in `mtcars`
We simulate 30 observations where every \(x\) below 5 has \(y = 0\) and every \(x\) above 5 has \(y = 1\).
set.seed(2301)separated <-tibble(x =c(runif(15, 0, 4.8), runif(15, 5.2, 10)),y =rep(c(0, 1), each =15))separated_fit <-glm(y ~ x, data = separated, family = binomial)
Warning: glm.fit: algorithm did not converge
Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
tidy(separated_fit)
# A tibble: 2 × 5
term estimate std.error statistic p.value
<chr> <dbl> <dbl> <dbl> <dbl>
1 (Intercept) -167. 159150. -0.00105 0.999
2 x 30.5 28718. 0.00106 0.999
separated_fit$iter
[1] 25
x_grid <-tibble(x =seq(0, 10, length.out =400))separated |>ggplot(aes(x = x, y = y)) +geom_line(data = x_grid |>mutate(y =predict(separated_fit, newdata = x_grid, type ="response")),color ="#0072B2", linewidth =1 ) +geom_point(size =2, alpha =0.7) +labs(x ="x",y ="y",title ="Perfectly separated data give a step function, not an S-curve" ) +theme_minimal()
The slope estimate is enormous, its standard error is larger still, and the \(z\) test reports a \(p\)-value near 1, even though \(x\) predicts \(y\) perfectly. The numbers in the table are wherever the algorithm stopped after 25 iterations, not estimates of anything. This is a case where the Wald test is badly misleading.
Separation also happens with categorical predictors when some category has only 0s or only 1s. In mtcars, every car with 3 gears is an automatic and every car with 5 gears is a manual.
mtcars |>count(gear, am)
gear am n
1 3 0 15
2 4 0 4
3 4 1 8
4 5 1 5
glm(am ~factor(gear), data = mtcars, family = binomial) |>tidy()
The coefficients for the 4- and 5-gear categories are compared with 3 gears, where the estimated probability of a manual transmission is 0. Those estimates and their standard errors are meaningless for the same reason. This time glm() gave no warning at all.
Separation is most likely with small samples, many predictors, rare outcomes, or a predictor that is nearly a stand-in for the response. Signs to look for are coefficients larger than about 10 in absolute value, standard errors many times larger than the coefficients, fitted probabilities of exactly 0 or 1, and many more iterations than usual. What to do depends on the situation:
If the separation is real and the predictor determines the outcome, that is a finding. Report it directly (“every car with 3 gears was automatic”) rather than as an odds ratio.
Combining sparse categories can remove separation caused by a factor level.
Penalized methods give finite estimates. Firth’s bias-reduced logistic regression (the logistf package) is the usual choice, and the ridge and lasso penalties from Chapter 19 also work for logistic regression.
Likelihood ratio tests and profile likelihood intervals behave much better than Wald tests when an estimate is large.
23.7.3 Small Samples and Few Events
Maximum likelihood estimates are only approximately unbiased and normal, and the approximation needs more data for logistic regression than for linear regression. What matters is not \(n\) alone but the number of observations in the less common outcome. With few of those, the estimates tend to be too large in absolute value, and separation becomes likely.
Example 23.17
ExampleSampling distribution of the slope at three sample sizes
We simulate data from the model \(\text{logit}(\pi) = -1 + 1 \cdot x\) with \(x\) standard normal, fit the logistic regression, and repeat 1,000 times for each of \(n = 20\), \(50\), and \(200\). We count fits with separation (an estimated slope over 10 in absolute value is a reliable sign here) and plot the slope estimates from the rest.
slope_sims |>filter(!is.na(slope), abs(slope) <=10) |>mutate(n =factor(paste("n =", n), levels =paste("n =", c(20, 50, 200)))) |>ggplot(aes(x = slope)) +geom_histogram(binwidth =0.2, fill ="gray70", color ="white") +geom_vline(xintercept =1, color ="#D55E00", linewidth =1) +facet_wrap(~ n, ncol =1, scales ="free_y") +coord_cartesian(xlim =c(-1, 5)) +labs(x =expression(hat(beta)[1]),y ="Count",title ="Estimated slopes from 1,000 samples at each size",subtitle ="Orange line = true slope of 1; separated fits removed" ) +theme_minimal()
With \(n = 20\) the estimates are spread widely, a few samples are separated, and even with those removed the average estimate is noticeably larger than 1. With \(n = 200\) the estimates center on the true value with a much smaller spread. A common rule of thumb is to have at least 10 to 20 observations in the less frequent outcome for each coefficient in the model. Like all such rules it is rough, but a model with 15 events and 6 predictors deserves suspicion.
23.7.4 Fitted Probabilities Are Never Exactly 0 or 1
The logistic curve approaches 0 and 1 but never reaches them. A fitted probability of 0.9999 is still a probability, and with the S-curve, very small or very large fitted probabilities come from extrapolating the curve into its tails, where it is controlled by the assumed logistic shape more than by data. In the transmission model, the fitted probability for a 6,000-pound car is tiny, but no car in the data weighs more than 5,424 pounds. The cautions about extrapolation in earlier chapters apply here too.
23.7.5 Residuals and \(R^2\)
The diagnostic tools from linear regression do not carry over directly. Raw residuals \(y_i - \hat\pi_i\) always fall in two bands, as in Example 23.2, so the usual residual plot is hard to read. There is no single \(R^2\); the several “pseudo-\(R^2\)” measures that exist do not agree with each other and do not mean “proportion of variance explained.” The next chapter takes up how to assess a logistic model, mainly through predictions: confusion matrices, thresholds, and ROC curves.
23.8 Recap
This chapter introduced logistic regression as a model for a binary response.
Idea
Meaning
Binary response
A response variable with two possible outcomes, usually coded 0 and 1.
Bernoulli response
A random variable that equals 1 with probability \(\pi\) and 0 with probability \(1-\pi\).
Mean of a binary response
For \(Y \in \{0,1\}\), \(E(Y)=P(Y=1)=\pi\).
Linear probability model
A linear model fit directly to a binary response; it can give fitted values outside \([0,1]\).
Nonconstant variance
For a Bernoulli response, \(Var(Y)=\pi(1-\pi)\), so the variance depends on the mean.
Sigmoidal response function
An S-shaped function that stays between 0 and 1.
Probit model
A binary-response model using the standard normal CDF, \(\Phi\).
Logistic model
A binary-response model using the logistic CDF.
Link function
The function connecting the mean to the linear predictor: logit, probit, or complementary log-log.
Linear predictor
\(\eta_i=\beta_0+\beta_1x_i\), linear in the parameters.
Logit transformation
\(\log\left(\frac{\pi}{1-\pi}\right)\), the log-odds.
Odds
\(\frac{\pi}{1-\pi}\), the probability of success divided by the probability of failure.
Maximum likelihood
Choosing the coefficients that make the observed data most probable; glm() does it by IRLS.
Deviance
\(-2\) times the log-likelihood, up to a constant; the logistic counterpart of SSE.
Odds ratio
A ratio of two odds; \(\exp(\beta_1)\) for a one-unit increase in \(x\), \(\exp(c\beta_1)\) for \(c\) units.
Coefficient interpretation
Additive on the log-odds scale, multiplicative on the odds scale, and not constant on the probability scale.
Divide-by-4 rule
\(\beta_1/4\) is the largest possible change in probability per unit of \(x\).
Relative risk
A ratio of two probabilities; close to the odds ratio only when the probabilities are small.
Grouped binomial data
Successes out of \(n_j\) trials at each \(x_j\); fit with cbind(successes, failures).
Event level
glm() models the probability of the second factor level; yardstick treats the first as the event by default.
Complete separation
A predictor that splits the outcomes perfectly; the MLE does not exist and the reported estimates are meaningless.
Fitted probability
A model-based estimate of \(P(Y=1)\) for given predictor values.
23.9 Check your understanding
NoteProblems
Why does the mean of a binary response equal a probability?
What are the two possible values of a Bernoulli response coded as 0 and 1?
Why can ordinary least squares be awkward for binary response data?
What is the most serious limitation of using a straight-line response function for probabilities?
What pattern does a residual-versus-fitted plot show when a straight line is fit to a 0/1 response, and why?
When does a linear probability model give answers close to logistic regression?
What does it mean for a response function to be sigmoidal?
How are the probit and logistic mean response functions similar? About how do their coefficients compare when both are fit to the same data?
In the probit derivation, why can we estimate only \(\beta_1^c/\sigma_c\) and not \(\beta_1^c\) itself?
What is the linear predictor in simple logistic regression?
What is the logit transformation?
What are odds, in words? If a probability is 0.50, what are the odds and the logit? If it is 0.90?
Why does glm() use maximum likelihood rather than least squares for logistic regression?
In Example 23.8, why is the log-likelihood surface a long diagonal ridge, and what would change it?
Why is a logistic regression slope not interpreted like a linear regression slope?
What does \(\exp(\beta_1)\) represent in simple logistic regression? What represents the odds ratio for a 10-unit increase?
If \(\exp(\beta_1)>1\), what does that imply about the odds as \(x\) increases? If \(\exp(\beta_1)<1\)?
A 95% confidence interval for \(\beta_1\) is \((0.2, 0.8)\). What is the interval for the odds ratio, and why is it not symmetric about \(\exp(\hat\beta_1)\)?
Why can the effect of \(x\) on probability depend on the starting value of \(x\)? What is the largest possible change in probability per unit of \(x\)?
A study of a common outcome reports an odds ratio of 3, and a headline says the exposure makes the outcome “three times as likely.” What is wrong?
For a logistic regression on a single 0/1 predictor, what do \(\exp(\hat\beta_0)\) and \(\exp(\hat\beta_1)\) equal?
How do you fit a logistic regression when the data give the number of successes out of \(n_j\) trials at each \(x_j\)? Why do the deviances differ between the grouped and individual forms when the coefficients are the same?
A response factor has levels “survived” and “died”. Which probability does glm() model?
A logistic regression reports a slope of 24.7 with a standard error of 3,100 and a \(p\)-value of 0.99. What is the likely problem, and what can you do?
Why are logistic regression estimates unreliable when the less common outcome has few observations?
What does type = "response" return when using predict() with a logistic regression model in R? What does the default, type = "link", return?
TipSolutions
A binary response can only be 0 or 1, so its expected value is \(1\cdot P(Y=1)+0\cdot P(Y=0)=P(Y=1)\).
The response equals 0 or 1. Usually 1 represents the event of interest and 0 its absence.
Binary data violate several assumptions of the linear model. The errors take only two values and are not normal, the variance \(\pi(1-\pi)\) is not constant, and fitted values can fall outside the range of a probability.
Probabilities must stay between 0 and 1, and a straight line will go below 0 or above 1 if \(x\) ranges far enough.
The points fall on two parallel diagonal lines. For each fitted value \(\hat y\), the residual is either \(1 - \hat y\) or \(0 - \hat y\), so the residuals for the 1s lie on one line and those for the 0s on another.
When all the fitted probabilities are in the middle range, roughly 0.2 to 0.8, where the logistic curve is close to a straight line.
It is S-shaped and bounded, approaching 0 and 1 gradually without ever leaving the probability range.
Both map a linear predictor to a probability with a symmetric S-shaped curve, and their fitted probabilities are usually very close. Probit uses the normal CDF and logistic regression the logistic CDF. Logistic coefficients are typically about 1.6 to 1.8 times the probit coefficients.
Dichotomizing the continuous response throws away its scale. The binary outcome depends only on whether \(y^c\) is above or below the cutoff, which is unchanged if \(y^c\), its coefficients, and \(\sigma_c\) are all multiplied by the same constant.
\(\eta_i=\beta_0+\beta_1x_i\), the part of the model that is linear in the coefficients.
The log of the odds, \(\log\left(\frac{\pi}{1-\pi}\right)\).
The odds are the probability of success divided by the probability of failure. At \(\pi = 0.50\) the odds are 1 and the logit is 0. At \(\pi = 0.90\) the odds are 9 (nine successes for every failure) and the logit is \(\log 9 \approx 2.20\).
The response is Bernoulli, not normal, and its variance depends on the mean. Maximum likelihood uses the Bernoulli distribution directly, and the fitted curve is nonlinear in the parameters on the probability scale, so least squares has no special justification.
The intercept is the log-odds at a weight of 0, far outside the data. Many combinations of a larger intercept and a steeper negative slope give nearly the same curve over the observed weights. Centering wt would make the intercept the log-odds at the average weight and turn the ridge into a rounder peak.
The slope is on the log-odds scale. It is the additive change in log-odds for a one-unit increase in \(x\), not an additive change in probability.
\(\exp(\beta_1)\) is the odds ratio for a one-unit increase in \(x\): the factor by which the odds are multiplied. For a 10-unit increase it is \(\exp(10\beta_1)\).
If \(\exp(\beta_1)>1\), the odds increase as \(x\) increases. If \(\exp(\beta_1)<1\), the odds decrease.
\((\exp(0.2), \exp(0.8)) = (1.22, 2.23)\). The interval is symmetric on the log-odds scale, and exponentiating stretches the upper end more than the lower end.
The probability curve is S-shaped, so its slope, \(\beta_1\pi(1-\pi)\), is steepest at \(\pi = 0.5\) and flattens near 0 and 1. The largest possible change per unit of \(x\) is about \(\beta_1/4\).
An odds ratio of 3 is a relative risk of 3 only when the outcome is rare. For a common outcome the probability goes up by much less than a factor of 3. With a baseline probability of 0.5, for example, an odds ratio of 3 raises the probability to 0.75, a relative risk of 1.5.
\(\exp(\hat\beta_0)\) is the sample odds of \(Y = 1\) in the group with \(x = 0\), and \(\exp(\hat\beta_1)\) is the sample odds ratio from the \(2 \times 2\) table, exactly.
Use cbind(successes, trials - successes) as the response in glm(), or use the proportion as the response with weights equal to the number of trials. The deviance compares the model with a saturated model, which has one parameter per row, so its value depends on whether the rows are groups or individuals. Compare deviances only between models fit to data in the same form.
Factor levels are in alphabetical order unless set otherwise, so the levels are “died” then “survived,” and glm() models the probability of the second level, \(P(\text{survived})\).
Probably complete or quasi-complete separation: the predictor splits the outcomes perfectly, so the estimate is running off to infinity and the Wald test is meaningless. Check a table or plot of the outcome against the predictor. Report the separation directly if it is real, combine sparse categories if a factor level is the cause, or use a penalized method such as Firth’s logistic regression. Use likelihood ratio tests rather than Wald tests.
The normal approximation for maximum likelihood estimates needs enough information about both outcomes. With few events, the estimates are biased away from zero, highly variable, and prone to separation. A rough guideline is 10 to 20 events per coefficient.
type = "response" returns fitted probabilities. The default, type = "link", returns values of the linear predictor on the log-odds scale.