R logistic regression
last modified November 19, 2026
Linear regression predicts a number that can take any value, but many
outcomes are binary: pass or fail, click or ignore, survive or die. Fitting a
straight line to zeros and ones produces predictions below zero and above one,
which cannot be probabilities. Logistic regression solves this by modelling
the log-odds of the outcome as a linear function of the predictors and then
mapping the result back to a probability between zero and one. In R the model
is fitted with glm and the argument
family = binomial. This article covers the coefficients, the
summary, odds ratios, predicted probabilities, classification, and a model
with two predictors.
The logistic model
The data record whether twenty students passed an exam and how many hours
each one studied. Half of the students passed, so the sample is balanced. The
response is coded as one for a pass and zero for a fail.
glm estimates two coefficients: an intercept and a slope on
hours. The slope is expressed in log-odds, not in probability points.
hours <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9)
passed <- c(0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1)
cat("attempts:", length(passed), "\n")
cat("pass rate:", mean(passed), "\n")
fit <- glm(passed ~ hours, family = binomial)
print(fit)
print(coef(fit))
The fitted equation is log-odds = -5.011 + 0.833 times hours. At six hours the linear predictor is almost exactly zero, which corresponds to a probability of one half. The residual deviance of 14.63 is smaller than the null deviance of 27.73, so the predictor improves the fit, and the AIC of 18.63 is the value to compare with other models. The model converged in five iterations.
$ Rscript fit.R
attempts: 20
pass rate: 0.5
Call: glm(formula = passed ~ hours, family = binomial)
Coefficients:
(Intercept) hours
-5.0108 0.8331
Degrees of Freedom: 19 Total (i.e. Null); 18 Residual
Null Deviance: 27.73
Residual Deviance: 14.63 AIC: 18.63
(Intercept) hours
-5.0107807 0.8331145
Reading the summary
summary reports the coefficient estimates, their standard errors,
the z statistics, and the p-values. The z statistic is the estimate divided by
its standard error, and the p-value tests whether the coefficient is zero. The
deviance lines describe the fit, and the AIC combines the fit with a penalty
for the number of parameters.
hours <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9) passed <- c(0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1) fit <- glm(passed ~ hours, family = binomial) print(summary(fit))
The intercept of -5.011 has a standard error of 2.20 and a p-value of 0.023, and the slope of 0.833 has a standard error of 0.352 and a p-value of 0.018. Both are distinguishable from zero at the five percent level, although the intercept is estimated much less precisely. The null deviance of 27.726 has nineteen degrees of freedom, and the residual deviance of 14.626 has eighteen, so one parameter was spent to reduce the deviance by about thirteen units.
$ Rscript model-summary.R
Call:
glm(formula = passed ~ hours, family = binomial)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -5.0108 2.2002 -2.277 0.0228 *
hours 0.8331 0.3515 2.370 0.0178 *
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 27.726 on 19 degrees of freedom
Residual deviance: 14.626 on 18 degrees of freedom
AIC: 18.626
Number of Fisher Scoring iterations: 5
Odds ratios
An odds ratio is the exponentiated coefficient. It says how many times the odds of the outcome multiply when the predictor increases by one unit. Odds are not probabilities: odds equal the probability divided by one minus the probability, so an odds ratio of two is not a doubling of the probability.
hours <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9) passed <- c(0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1) fit <- glm(passed ~ hours, family = binomial) print(exp(coef(fit))) print(suppressMessages(exp(confint(fit))))
The odds ratio for hours is 2.30, so each additional hour multiplies the odds of passing by about 2.3. The confidence interval runs from 1.35 to 5.87 and does not contain one, which agrees with the significant slope in the summary. The intercept odds ratio of 0.0067 refers to zero hours, which is far outside the observed range and has no practical interpretation. The interval is computed by profiling the likelihood, and the message about profiling is suppressed in the script so the output stays clean.
$ Rscript odds.R
(Intercept) hours
0.006665697 2.300472407
2.5 % 97.5 %
(Intercept) 1.942173e-05 0.190857
hours 1.354216e+00 5.874759
Predicted probabilities
predict returns the linear predictor by default, on the log-odds
scale. Adding type = "response" applies the inverse logit and
returns a probability. The two are related by the function
plogis, which is the logistic cumulative distribution.
hours <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9) passed <- c(0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1) fit <- glm(passed ~ hours, family = binomial) new <- data.frame(hours = c(2, 5, 8, 11)) print(predict(fit, new, type = "link")) print(predict(fit, new, type = "response"))
Two hours correspond to a log-odds of -3.34 and a probability of 0.034, while eleven hours correspond to 4.15 and 0.985. The probability rises quickly in the middle of the range and flattens at both ends, which is the S shape of the logistic curve. A one-hour increase changes the log-odds by the same 0.833 everywhere, but the change in probability is largest near the middle and tiny at the extremes.
$ Rscript probabilities.R
1 2 3 4
-3.3445517 -0.8452082 1.6541353 4.1534788
1 2 3 4
0.03407403 0.30043901 0.83944916 0.98453331
Classification
A probability can be turned into a class by comparing it with a threshold. The usual default is 0.5, although the right threshold depends on the cost of a false positive compared with a false negative. A confusion table crosses the predicted classes with the observed classes.
hours <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9)
passed <- c(0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1)
fit <- glm(passed ~ hours, family = binomial)
probability <- predict(fit, type = "response")
predicted <- ifelse(probability >= 0.5, 1, 0)
print(table(predicted, passed))
cat("accuracy:", mean(predicted == passed), "\n")
The table reads row by row: eight students were predicted to fail and did fail, three were predicted to fail but passed, two were predicted to pass but failed, and seven were predicted to pass and did. Accuracy is 15 of 20, or 0.75. Lowering the threshold would catch more of the three missed passes at the cost of more false alarms, so the threshold should follow from the decision, not from a habit. Accuracy on the data used to fit the model is also optimistic.
$ Rscript classify.R
passed
predicted 0 1
0 8 3
1 2 7
accuracy: 0.75
A second predictor
The built-in mtcars data set records the transmission type of
thirty-two cars along with their horsepower and weight; thirteen of the cars
have a manual transmission. The response
am is one for a manual transmission and zero for an automatic.
The model estimates the effect of each predictor while holding the other one
fixed, and the weights are measured in thousands of pounds.
fit <- glm(am ~ hp + wt, family = binomial, data = mtcars)
cat("cars:", nrow(mtcars), "\n")
cat("manual:", sum(mtcars$am), "\n")
cat("AIC:", round(summary(fit)$aic, 2), "\n")
print(summary(fit))
The intercept is 18.87, horsepower adds 0.036 log-odds per unit, and weight subtracts 8.08 log-odds per thousand pounds. Both predictors have p-values below 0.05, and the residual deviance of 10.06 is far below the null deviance of 43.23. The AIC of 16.06 belongs to a different data set, so it cannot be compared with the 18.63 of the hours-only model. The sign of the weight coefficient matches the intuition that heavier cars are less likely to be manual.
$ Rscript multiple.R
cars: 32
manual: 13
AIC: 16.06
Call:
glm(formula = am ~ hp + wt, family = binomial, data = mtcars)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 18.86630 7.44356 2.535 0.01126 *
hp 0.03626 0.01773 2.044 0.04091 *
wt -8.08348 3.06868 -2.634 0.00843 **
---
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: 10.059 on 29 degrees of freedom
AIC: 16.059
Number of Fisher Scoring iterations: 8
A complete example
The final script fits the hours model, prints the odds ratio, classifies the students at the default threshold, reports the accuracy, and predicts the probability for a student who studies eight hours. It is a compact version of the whole workflow: estimate, interpret, and apply.
hours <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 2, 3, 4, 5, 6, 7, 8, 9)
passed <- c(0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 1, 1)
fit <- glm(passed ~ hours, family = binomial)
probability <- predict(fit, type = "response")
predicted <- ifelse(probability >= 0.5, 1, 0)
print(exp(coef(fit)))
print(table(predicted, passed))
cat("accuracy:", mean(predicted == passed), "\n")
new <- data.frame(hours = 8)
cat("pass probability at 8 hours:", round(predict(fit, new, type = "response"), 3), "\n")
The odds ratio of 2.30 means that the odds of passing multiply by 2.3 for every extra hour. The confusion table is the same as before, with an accuracy of 0.75, and the predicted probability at eight hours is 0.839. That probability is a model estimate for the population, not a guarantee for an individual student, and it assumes that the logistic form and the observed range of hours are appropriate.
$ Rscript report.R
(Intercept) hours
0.006665697 2.300472407
passed
predicted 0 1
0 8 3
1 2 7
accuracy: 0.75
pass probability at 8 hours: 0.839
Next steps
The linear regression article explains
the formula syntax and the summary that glm extends, and the
hypothesis testing article covers the
p-value logic behind the z tests. The
descriptive statistics article
shows how to describe the binary response before modelling it, and the
base graphics article draws the scatter
plot that reveals the separation between the two outcomes.
Source
R documentation: family objects for glm
In this article we have fitted a binary response with
glm(family = binomial) and read its coefficients on the log-odds
scale. We interpreted the summary and the deviance, converted coefficients to
odds ratios with confidence intervals, and turned the linear predictor into
probabilities. We classified the observations at a threshold, read the
confusion table, and added a second predictor on the mtcars data.
The final example combined the odds ratio, the classification, and a
prediction for a new student. The model extends the straight line from
R linear regression to a binary
response, and the next article uses
analysis of variance to compare several group
means at once.
Author
List all R Language tutorials.