typestar

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

  1. family = binomial selects logistic regression.
  2. exp(coef(model)) converts log-odds to odds ratios.
  3. type = "response" predicts probabilities to threshold.

Keywords and builtins used here

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.

Type this snippet

Step 3 of 4 in Correlation & regression, step 8 of 11 in Statistics.

← Previous Next →

Logistic regression in other languages