3. Consider the Gini index, classification error, and entropy in a simple classification setting with two classes. Create a single plot that displays each of these quantities as a function of ˆp m1 . The x-axis should display ˆp m1 , ranging from 0 to 1, and the y-axis should display the value of the Gini index, classification error, and entropy. Hint: In a setting with two classes, ˆp m1 = 1 − ˆp m2 . You could make this plot by hand, but it will be much easier to make in R.

p_m1 <- seq(0, 1, length.out = 1000)
p_m2 <- 1 - p_m1

df <- data.frame(
  p_m1 = p_m1,
  gini = (p_m1 - p_m1^2) + (p_m2 - p_m2^2),
  class_error = 1 - pmax(p_m1, p_m2),
  entropy = (-p_m1 * log(p_m1 + 1e-10)) + (-p_m2 * log(p_m2 + 1e-10))
)

df_long <- pivot_longer(df, cols = -p_m1, names_to = "Measure", values_to = "Value")

ggplot(df_long, aes(x = p_m1, y = Value, color = Measure, linetype = Measure)) +
  geom_line() +
  labs(
    title = "Gini Index, Classification Error, and Entropy",
    x = expression(hat(p)[m1]),
    y = "Value"
  ) +
  theme(plot.title = element_text(hjust = 0.5, size = rel(1.5)))