Introduction
Logistic regression models the probability of a binary outcome. It's used for classification problems.
Fitting Logistic Regression
# Fit model
model <- glm(y ~ x, data = df, family = "binomial")
# Model summary
summary(model)
# Coefficients
coef(model)
exp(coef(model)) # Odds ratios
Predictions
# Predicted probabilities
predict(model, type = "response")
# Class predictions
pred <- predict(model, type = "response") > 0.5
Model Evaluation
# Confusion matrix
library(caret)
confusionMatrix(pred, actual)
# ROC curve
library(pROC)
roc(actual, predicted_probabilities)
Multiple Predictors
model <- glm(y ~ x1 + x2 + x3,
data = df,
family = "binomial")
Summary
Logistic regression is essential for binary classification. Interpret coefficients as odds ratios.