library(MASS)
## Warning: package 'MASS' was built under R version 4.3.3
table(iris$Species)
## 
##     setosa versicolor  virginica 
##         50         50         50
lookup <- c(setosa='blue', versicola='green', virginica='orange')
col.ind <- lookup[iris$Species]
pairs(iris[-5], pch=21, col="gray", bg=col.ind)

lda.fit <- lda(Species ~ ., data = iris)
lda.fit
## Call:
## lda(Species ~ ., data = iris)
## 
## Prior probabilities of groups:
##     setosa versicolor  virginica 
##  0.3333333  0.3333333  0.3333333 
## 
## Group means:
##            Sepal.Length Sepal.Width Petal.Length Petal.Width
## setosa            5.006       3.428        1.462       0.246
## versicolor        5.936       2.770        4.260       1.326
## virginica         6.588       2.974        5.552       2.026
## 
## Coefficients of linear discriminants:
##                     LD1         LD2
## Sepal.Length  0.8293776 -0.02410215
## Sepal.Width   1.5344731 -2.16452123
## Petal.Length -2.2012117  0.93192121
## Petal.Width  -2.8104603 -2.83918785
## 
## Proportion of trace:
##    LD1    LD2 
## 0.9912 0.0088
plot(Sepal.Width ~ Sepal.Length, data = iris, pch=21, col="gray", bg= col.ind)
points(lda.fit$means[,1], lda.fit$means[,2], pch=21, cex=2,
       col="black", bg=lookup)

lda.pred <- predict(lda.fit)
head(lda.pred$x)
##        LD1        LD2
## 1 8.061800 -0.3004206
## 2 7.128688  0.7866604
## 3 7.489828  0.2653845
## 4 6.813201  0.6706311
## 5 8.132309 -0.5144625
## 6 7.701947 -1.4617210
plot(LD2 ~ LD1, data = lda.pred$x, pch=21, col="gray", bg=col.ind)

table(pred=lda.pred$class, true=iris$Species)
##             true
## pred         setosa versicolor virginica
##   setosa         50          0         0
##   versicolor      0         48         1
##   virginica       0          2        49
1 - mean(lda.pred$class == iris$Species)
## [1] 0.02
lda.cv <- predict(lda.fit, CV=TRUE)