How to implement Linear Discriminant Analysis in R ?

Description

To implement Linear Discriminant Analysis in R

Libraries required :

require(MASS)
require(ggplot2)
require(scales)
require(gridExtra)

  Load the necessary libraries

  Load the data set

  Perform Linear Discriminant analysis on the data

  Take the summary

  Visualize the result

#Load necessary libraries
require(MASS)
require(ggplot2)
require(scales)
require(gridExtra)
data=read.csv(‘/……../iris.csv’)
#To Split 80% of data as training data
smp_size train_ind train test #Perform linear discriminant analysis
lda data=train)
#Take the summary
summary(lda)
lda$prior
lda$counts
lda$means
lda$scaling
lda$svd
lda$x
#Visualize the result
prop.lda = lda$svd^2/sum(lda$svd^2)
plda = predict(object = lda,newdata = data )
dataset = data.frame(species = data[,”species”],lda = plda$x)
p1 labs(x = paste(“LD1 (“, percent(prop.lda[1]), “)”, sep=””),
y = paste(“LD2 (“, percent(prop.lda[2]), “)”, sep=””))

grid.arrange(p1)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit