Statistical model reference
Logistic Regression
Review when to use this method, its data requirements, implementation patterns, and interpretation guidance.
Description
A specialized regression technique for binary outcomes that uses the logistic function to model the probability of a binary event occurring. It transforms the linear relationship between predictors and outcome to constrain predicted values between 0 and 1, making it ideal for classification problems, risk assessment, and inference about categorical outcomes.
Use Cases
- classification
- prediction
- inference
Requirements
- Sample Size: small, medium, large
- Missing Data: none, random, systematic
- Data Distribution: normal, non_normal
- Relationship Type: linear, non_linear
Variable Types
Dependent Variables
- binary
Independent Variables
- continuous
- categorical
- binary
Implementation
from sklearn.linear_model import LogisticRegression
model = LogisticRegression()
model.fit(X, y)
predictions = model.predict(X_test)
Documentation
model <- glm(y ~ x1 + x2, family=binomial, data=df)
summary(model)
predictions <- predict(model, newdata=test_data, type='response')
Documentation
LOGISTIC REGRESSION VARIABLES y
/METHOD=ENTER x1 x2
/CONTRAST (x1)=Indicator
/CONTRAST (x2)=Indicator
/PRINT=CI(95)
/CRITERIA=PIN(0.05) POUT(0.10) ITERATE(20) CUT(0.5)
Documentation
proc logistic data=dataset;
model y(event='1') = x1 x2;
run;
Documentation
logit y x1 x2
Documentation
Synthetic Data Example
A dataset suitable for Logistic Regression analysis
R Code for Data Generation and Analysis
# Generate synthetic data for logistic regression
set.seed(123)
n <- 200 # sample size
x1 <- rnorm(n, mean = 0, sd = 1) # continuous predictor
x2 <- rnorm(n, mean = 0, sd = 1) # another continuous predictor
x3 <- factor(sample(1:3, n, replace = TRUE)) # categorical predictor
# Generate binary outcome based on logistic model
logit <- -1 + 0.8 * x1 - 1.2 * x2 # linear predictor
prob <- 1 / (1 + exp(-logit)) # apply logistic function to get probabilities
y <- rbinom(n, 1, prob) # generate binary outcome
# Combine into a data frame
df <- data.frame(y = factor(y), x1 = x1, x2 = x2, x3 = x3)
# Descriptive statistics
summary(df)
table(df$y) # frequency of outcome
table(df$y, df$x3) # contingency table with categorical predictor
# Visualization
boxplot(x1 ~ y, data = df, main = "X1 by Outcome", xlab = "Outcome (Y)", ylab = "X1")
boxplot(x2 ~ y, data = df, main = "X2 by Outcome", xlab = "Outcome (Y)", ylab = "X2")
# Model fitting
model <- glm(y ~ x1 + x2 + x3, family = binomial(link = "logit"), data = df)
summary(model)
# Effects on odds ratios
exp(coef(model)) # exponentiated coefficients give odds ratios
exp(confint(model)) # confidence intervals for odds ratios
# Predictions
new_data <- data.frame(x1 = c(-1, 0, 1), x2 = c(1, 0, -1), x3 = factor(c(1, 2, 3), levels = 1:3))
predicted_probs <- predict(model, newdata = new_data, type = "response")
predicted_class <- ifelse(predicted_probs > 0.5, 1, 0)
print(cbind(new_data, prob = predicted_probs, class = predicted_class))
# ROC curve and AUC
library(pROC)
roc_obj <- roc(df$y, predict(model, type = "response"))
plot(roc_obj, main = "ROC Curve")
auc(roc_obj) # Area Under the Curve
Copy this code into your R environment to generate synthetic data and perform analysis with this model.
Expected Analysis Results
Console Output
> summary(df)
y x1 x2 x3
0:120 Min. :-2.95305 Min. :-3.4702 1:74
1:80 1st Qu.:-0.63543 1st Qu.:-0.6595 2:61
Median : 0.02386 Median : 0.1035 3:65
Mean : 0.02207 Mean : 0.0222
3rd Qu.: 0.70851 3rd Qu.: 0.6863
Max. : 2.81898 Max. : 3.5814
> table(df$y)
0 1
120 80
> model <- glm(y ~ x1 + x2 + x3, family = binomial(link = "logit"), data = df)
> summary(model)
Call:
glm(formula = y ~ x1 + x2 + x3, family = binomial(link = "logit"),
data = df)
Deviance Residuals:
Min 1Q Median 3Q Max
-2.1701 -0.8079 -0.4635 0.9184 2.2701
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.9879 0.2811 -3.515 0.000439 ***
x1 0.7846 0.1827 4.294 1.75e-05 ***
x2 -1.2264 0.2096 -5.853 4.82e-09 ***
x32 0.1308 0.3989 0.328 0.743023
x33 0.5486 0.3843 1.428 0.153465
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
(Dispersion parameter for binomial family taken to be 1)
Null deviance: 267.33 on 199 degrees of freedom
Residual deviance: 208.46 on 195 degrees of freedom
AIC: 218.46
Number of Fisher Scoring iterations: 4
> exp(coef(model)) # exponentiated coefficients give odds ratios
(Intercept) x1 x2 x32 x33
0.3724354 2.1916057 0.2933147 1.1396893 1.7309659
> predicted_probs <- predict(model, newdata = new_data, type = "response")
> predicted_class <- ifelse(predicted_probs > 0.5, 1, 0)
> print(cbind(new_data, prob = predicted_probs, class = predicted_class))
x1 x2 x3 prob class
1 -1 1 1 0.075095095 0
2 0 0 2 0.345347633 0
3 1 -1 3 0.824435337 1
These results are from running the R code on synthetic data. Your actual results may vary depending on your data.
Interpretation Guide
Need help interpreting the results of your Logistic Regression analysis? Our comprehensive interpretation guide explains:
- How to read and understand model outputs
- Interpreting coefficients and effect sizes correctly
- Understanding diagnostic plots and visualizations
- Common pitfalls and how to avoid them
- Making valid conclusions from your analysis