O'Reilly logo

R Data Analysis Cookbook - Second Edition by Kuntal Ganguly

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

To classify using logistic regression, follow these steps:

  1. Load the caret package:
> library(caret) 
  1. Read the data:
> bh <- read.csv("boston-housing-logistic.csv") 
  1. Convert the outcome variable class to a factor:
> bh$CLASS <- factor(bh$CLASS, levels = c(0,1)) 
  1. Partition the data. The predictor variables are already numeric and the outcome variable CLASS is already 0-1, so we do not have to do any data preparation. Refer to Creating random data partitions in Chapter 2, What's In There? - Exploratory Data Analysis, for details on how the following command works:
> set.seed(1000) 
> train.idx <- createDataPartition(bh$CLASS, p=0.7, list = FALSE) 
  1. Build the logistic regression model:
> logit <- glm(CLASS~., data = bh[train.idx,], ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required