FLDA Example

lda_model <- MASS::lda(bill_length_mm ~ flipper_length_mm, 
                       data = train_set)
LDA math
del_x <- 1
del_y <- lda_model$scaling[1,1]
lda_slope <- del_y / del_x

xbar <- mean(train_set$flipper_length_mm, na.rm = TRUE)
ybar <- mean(train_set$bill_length_mm, na.rm = TRUE)
lda_intercept <- ybar - lda_slope * xbar

lda_plot_1 <- penguin_2_class |>
ggplot(aes(x = flipper_length_mm, y = bill_length_mm, 
           color = species)) + 
  geom_point(size = 3) + 
  geom_abline(slope = lda_slope, intercept = lda_intercept,
              color = adelie_color, linewidth = 3) +
  labs(title = "Linear Discriminant Analysis",
       subtitle = "<span style = 'color:#fb7504'>first linear discriminant</span>",
       caption = "Data Science Learning Community") +
  scale_color_manual(values = c(chinstrap_color, gentoo_color)) +
  theme_minimal() +
  theme(plot.title = element_markdown(face = "bold", size = 24),
        plot.subtitle = element_markdown(size = 16))

lda_plot_1

Projection math
train_mat <- as.matrix(train_set)
proj_mat  <- as.matrix(c(1, lda_model$scaling[1,1]))
projection_data <- train_mat %*% proj_mat
projection_df <- cbind(penguin_2_class, projection_data)

lda_plot_2 <- projection_df |>
  ggplot(aes(x = projection_data)) +
  geom_density(aes(fill = species),
               alpha = 0.5) + 
  labs(title = "Classification via <br><span style = 'color:#fb7504'>Linear Discriminant Analysis</span>",
       subtitle = "",
       caption = "Data Science Learning Community",
       x = "(LDA1) first linear discriminant",
       y = "") +
  scale_fill_manual(values = c(chinstrap_color, gentoo_color)) +
  theme_minimal() +
  theme(axis.title.y  = element_blank(),
        axis.text.y   = element_blank(),
        axis.ticks.y  = element_blank(),
        plot.title    = element_markdown(face = "bold", size = 24),
        plot.subtitle = element_markdown(size = 16))

lda_plot_2