Build and Evaluate A Logistic Regression Classifier

Written by Matt Dancho



This article is part of a R-Tips Weekly, a weekly video tutorial that shows you step-by-step how to do common R coding tasks.


Logistic regression is a simple, yet powerful classification model. In this tutorial, learn how to build a predictive classifier that classifies the age of a vehicle. Then use ggplot to tell the story!

Here are the links to get set up. 👇

The Story

In this analysis we learn that newer vehicles are MORE EFFICIENT, and we’ll make a data visualization that tells the story.

(Click image to play tutorial)

How did we make this plot?

  1. Our logistic regression classifier modeled the data
  2. We used VIP to find the most important features
  3. We visualized with ggplot 💥

Making a Logistic Regression Classifier

Logistic regression is a must-know tool in your data science arsenal.

  • Logistic Regression is easy to explain
  • The classifier has no tuning parameters (no knobs that need adjusted)

Simply split our dataset, train on the training set, evaluate on the testing set.

Folks, it’s that simple. 👏

Full code in the video Github Repository

Evaluating Our Classification Model

Question: How do we know our if our model is good?
Answer: Area Under the Curve (AUC)!

About AUC:

  • Simple measure.
  • We want greater than 0.5.
  • Closer to 1.0, the better our model is.
  • Bonus: ROC Plot - A way to visualize the AUC.


Full code in the video Github Repository

Telling the Story

What can we do with a Logistic Regression Classifier? Let’s develop a story to communicate our insight!


1. First, find the most important features (predictors) using vip().


Full code in the video Github Repository


2. Next, use ggplot() to make a visualization that focuses on the top features:

  • HWY: The highway fuel economy (miles per gallon)
  • CLASS: The Vehicle Class (e.g. pickup, subcompact, SUV)


Full code in the video Github Repository

What did we learn using Logistic Regression?

It’s clear now:

  • Vehicles have become more efficient over time.
  • Highway fuel economy has gone up for every single class of vehicle.


Your story-telling skills are amazing. Santa approves. 👇


But if you really want to improve your productivity…

Here’s how to master R programming and become powered by R. 👇

What happens after you learn R for Business.

Your Job Performance Review after you’ve launched your first Shiny App. 👇

This is career acceleration.


SETUP R-TIPS WEEKLY PROJECT

  1. Get the Code

  2. Check out the R-Tips Setup Video.

Once you take these actions, you’ll be set up to receive R-Tips with Code every week. =)



👇 Top R-Tips Tutorials you might like:

  1. mmtable2: ggplot2 for tables
  2. ggdist: Make a Raincloud Plot to Visualize Distribution in ggplot2
  3. ggside: Plot linear regression with marginal distributions
  4. DataEditR: Interactive Data Editing in R
  5. openxlsx: How to Automate Excel in R
  6. officer: How to Automate PowerPoint in R
  7. DataExplorer: Fast EDA in R
  8. esquisse: Interactive ggplot2 builder
  9. gghalves: Half-plots with ggplot2
  10. rmarkdown: How to Automate PDF Reporting
  11. patchwork: How to combine multiple ggplots
  12. Geospatial Map Visualizations in R

Want these tips every week? Join R-Tips Weekly.