Answer

Logistic vs linear regression: when should you use which?

A frequently asked statistics question ·

regressionlogistic regressionclassification

The short answer

Use linear regression when the outcome is a continuous number, such as weight or income, and logistic regression when it is binary, such as bought or did not buy. Logistic regression models the probability of the event, keeps predictions between 0 and 1, and gives coefficients that become odds ratios. A straight line fitted to a 0/1 outcome can predict impossible probabilities and assumes the effect is the same everywhere.

The outcome decides

The choice between logistic and linear regression depends on what you are predicting, not on your predictors. Both can use any mix of continuous and categorical predictors, and both are fitted and read in similar ways. What differs is the outcome they are built for.

What goes wrong with a straight line for a 0/1 outcome

Nothing stops you running ordinary least squares on an outcome coded 0 and 1. The result is called a linear probability model, and its fitted values are read as probabilities. It has known problems:

The linear probability model is not useless. When fitted probabilities stay roughly between .2 and .8, its slope is easy to read as a change in percentage points and is often close to the average effect from a logistic model, which is why some fields, especially economics, use it with robust standard errors. For predicting individual probabilities, or when the outcome is rare or near-certain for some people, logistic regression is the safer choice.

Side by side in R and Python

The example simulates 300 customers, their number of website visits last month (0 to 20) and whether they bought something, with the true probability following a logistic curve. It fits a linear probability model and a logistic regression to the same data. The Python tab uses scikit-learn; setting C=np.inf removes its default penalty so the estimates are plain maximum likelihood, which matches R's glm() to several decimal places on identical data. Standard errors are computed by hand because scikit-learn does not report them.

R

set.seed(808)
n <- 300
visits <- round(runif(n, 0, 20))                    # website visits last month
buy    <- rbinom(n, 1, plogis(-4 + 0.4 * visits))   # yes/no outcome; true model is logistic

lpm   <- lm(buy ~ visits)                           # linear regression (linear probability model)
logit <- glm(buy ~ visits, family = binomial)       # logistic regression

round(rbind(linear = coef(lpm), logistic = coef(logit)), 3)

# The straight line runs past 0 and 1 at the extremes
round(range(fitted(lpm)), 3)
sum(fitted(lpm) < 0 | fitted(lpm) > 1)              # people with an impossible "probability"

# Predicted probability of buying at 0, 5, 10, 15 and 20 visits
new <- data.frame(visits = c(0, 5, 10, 15, 20))
round(rbind(linear   = predict(lpm, new),
            logistic = predict(logit, new, type = "response")), 3)

# Logistic slope as an odds ratio per extra visit, with a Wald 95% CI
b <- coef(summary(logit))["visits", ]
round(c(b[1:3], OR = exp(b[[1]]),
        lower = exp(b[[1]] - 1.96 * b[[2]]), upper = exp(b[[1]] + 1.96 * b[[2]])), 3)
format.pval(b[[4]], digits = 3)

# Average change in probability per visit: logistic vs the linear slope
round(c(logistic_AME = mean(dlogis(predict(logit))) * coef(logit)[["visits"]],
        linear_slope = coef(lpm)[["visits"]]), 4)

Python

import numpy as np
from scipy import stats
from scipy.special import expit
from sklearn.linear_model import LinearRegression, LogisticRegression

rng = np.random.default_rng(808)
n = 300
visits = np.round(rng.uniform(0, 20, n))                # website visits last month
buy = rng.binomial(1, expit(-4 + 0.4 * visits))         # yes/no outcome; true model is logistic
X = visits.reshape(-1, 1)

lpm = LinearRegression().fit(X, buy)                    # linear regression (linear probability model)
# C=np.inf switches off sklearn's default penalty, so this is plain maximum likelihood, as in R's glm()
logit = LogisticRegression(C=np.inf, tol=1e-10, max_iter=1000).fit(X, buy)
b_lin = np.array([lpm.intercept_, lpm.coef_[0]])
b_log = np.array([logit.intercept_[0], logit.coef_[0, 0]])
print("linear  ", np.round(b_lin, 3), "\nlogistic", np.round(b_log, 3))

# The straight line runs past 0 and 1 at the extremes
fit_lin = lpm.predict(X)
print("range of linear fitted values", np.round([fit_lin.min(), fit_lin.max()], 3))
print("impossible 'probabilities':", int(np.sum((fit_lin < 0) | (fit_lin > 1))))

# Predicted probability of buying at 0, 5, 10, 15 and 20 visits
new = np.array([[0], [5], [10], [15], [20]])
print("linear  ", np.round(lpm.predict(new), 3))
print("logistic", np.round(logit.predict_proba(new)[:, 1], 3))

# Logistic slope as an odds ratio per extra visit, with a Wald 95% CI
Xd = np.column_stack([np.ones(n), visits])
p = expit(Xd @ b_log)
se = np.sqrt(np.diag(np.linalg.inv(Xd.T @ (Xd * (p * (1 - p))[:, None]))))
z = b_log[1] / se[1]
print("b", round(b_log[1], 3), "SE", round(se[1], 3), "z", round(z, 3), "p", 2 * stats.norm.sf(abs(z)))
print("OR", round(np.exp(b_log[1]), 3), "CI", np.round(np.exp(b_log[1] + np.array([-1.96, 1.96]) * se[1]), 3))

# Average change in probability per visit: logistic vs the linear slope
print("logistic AME", round(np.mean(p * (1 - p)) * b_log[1], 4), " linear slope", round(b_lin[1], 4))

The figures below come from one seeded run of the R code. Python's random numbers differ from R's, so its figures differ slightly (for example 60 impossible predictions instead of 71, and an odds ratio of 1.437 instead of 1.570), but the pattern is the same.

Related choices people ask about

How to report logistic regression in APA style (7th edition)

Name the model, how the outcome was coded and how the confidence intervals were computed in the method section. Using the logistic model above:

"A binary logistic regression showed that the number of website visits predicted purchasing, b = 0.45, SE = 0.05, z = 9.23, p < .001. Each additional visit was associated with 1.57 times higher odds of buying, OR = 1.57, 95% CI [1.43, 1.73]."

For a linear regression, report b, SE, t and p for each predictor, plus R² and the overall F test. In a table of logistic results, give each predictor's b, SE, p and odds ratio with its 95% CI, and list the sample size and the number of events in a note. Predicted probabilities at a few meaningful predictor values help readers see the size of the effect.

Related tools and guides

More answered questions

Working with your own data?

General answers only go so far. Send us your situation and we'll reply by email within two business days.

Ask your question

Need more than an answer?

Drafted with AI assistance and reviewed by Nedim Yel, PhD, research methodologist at DASS. This is general information, not advice for your specific data or study. When the results matter, check your approach with a qualified statistician.