Logistic regression in R
Modeling a binary outcome with glm.
model <- glm(am ~ wt + hp, data = mtcars, family = binomial)
coefs <- coef(model)
odds_ratios <- exp(coef(model))
deviance <- model$deviance
probabilities <- predict(model, type = "response")
classes <- ifelse(probabilities > 0.5, 1, 0)
accuracy <- mean(classes == mtcars$am)
table_of_fit <- table(predicted = classes, actual = mtcars$am)
How it works
family = binomialselects logistic regression.exp(coef(model))converts log-odds to odds ratios.type = "response"predicts probabilities to threshold.
Keywords and builtins used here
coefexpglmifelsemeanpredicttable
The run, in numbers
- Lines
- 11
- Characters to type
- 341
- Tokens
- 85
- Three-star pace
- 100 tpm
At the three-star pace of 100 tokens a minute, this run takes about 51 seconds.
Step 3 of 4 in Correlation & regression, step 8 of 11 in Statistics.