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 linear discriminant function analysis, follow these steps:

  1. Load the MASS and caret packages:
> library(MASS) 
> library(caret) 
  1. Read the data:
> bn <- read.csv("banknote-authentication.csv")
  1. Convert the outcome variable class to a factor:
> bn$class <- factor(bn$class) 
  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) 
> t.idx <- createDataPartition(bn$class, p = 0.7, list=FALSE) 
  1. Build the linear discriminant function (LDF) model: ...

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