library(MASS)
library(stats)
library(dplyr)
library(caret)

Linear Discriminant Analysis (LDA)

Linear discriminant analysis, also known as LDA, allows for the scaling of large amounts of data to be projected onto two axes that permit efficient separation. For instance, assume two variable X1 and X2 are projected onto a two-dimensional graph. By using LDA, those two variables will be projected onto a one dimensional plane that maximizes the separation of those two variables. This is achieved by maximizing the distance between both means of X1 and X2, then minimizing the variances respectively. 
In the following example below, a “perfect dataset” is taken into consideration to blatantly illustrate LDA using R and the iris dataset. In this case, the species is split into a training and testing dataset. Then, both sets are run through caret::preProcess() to transform the data via centering and scaling the data. Then, via MASS::lda(), a model was built to predict the species. We obtained a summary of the model which provided the prior probabilities of each species, the means of the predictor variables, and the coefficients of linear discriminants. The species were then plotted on a scatter plot to visualize the separation as achieved by LDA. 
set.seed(1)

data("iris")
head(iris,2)
##   Sepal.Length Sepal.Width Petal.Length Petal.Width Species
## 1          5.1         3.5          1.4         0.2  setosa
## 2          4.9         3.0          1.4         0.2  setosa
training_tree <- iris$Species %>%
  createDataPartition(p = 0.8, list = FALSE)

train_data <- iris[training_tree,]
test_data <- iris[-training_tree,]

preprocess_param <- train_data %>%
  preProcess(method = c("center", "scale"))

train_transform <- preprocess_param %>% predict(train_data)
test_transform <- preprocess_param %>% predict(test_data)

model <- lda(Species~., data = train_transform)

predictions <- model %>% predict(test_transform)

model <- lda(Species~., data = train_transform)
model
## Call:
## lda(Species ~ ., data = train_transform)
## 
## Prior probabilities of groups:
##     setosa versicolor  virginica 
##  0.3333333  0.3333333  0.3333333 
## 
## Group means:
##            Sepal.Length Sepal.Width Petal.Length Petal.Width
## setosa       -1.0403185   0.8430286   -1.3160834   -1.257539
## versicolor    0.2031872  -0.5979621    0.3285433    0.183870
## virginica     0.8371313  -0.2450664    0.9875400    1.073669
## 
## Coefficients of linear discriminants:
##                     LD1         LD2
## Sepal.Length  0.6603021 -0.03112867
## Sepal.Width   0.6032864  0.77207797
## Petal.Length -4.0979045 -2.32179601
## Petal.Width  -1.9769248  2.81185685
## 
## Proportion of trace:
##    LD1    LD2 
## 0.9916 0.0084

Plotting the Results

library(ggplot2)
library(MASS)
library(mvtnorm)

lda_plot <- cbind(train_data, predict(model)$x)

ggplot(lda_plot, aes(LD1, LD2)) +
  geom_point(aes(color = Species))