# Load data
datos_pca <- read.csv("/vocal_id_mda.csv", header = TRUE)
# Preliminary model fitting
tuneGrid <- expand.grid(subclasses = 1:10)  
fitControl <- trainControl(method = "cv", number = 10)  
model <- caret::train(type.2 ~ RC1 + RC2 + RC3 + RC4, data = datos_pca, method = "mda", trControl = fitControl, tuneGrid = tuneGrid)
print(model)
# Fit the definitive model
MDA <- mda(type.2 ~ RC1 + RC2 + RC3 + RC4,  data = datos_pca, subclasses = 9)
MDA
# Extract discriminant function coordinates
coordenadas <- as.data.frame(predict(MDA, newdata = datos_pca, type = "variates"))
predicciones <- predict(MDA, newdata = datos_pca)
# Overall confusion matrix
confusion_matrix <- confusionMatrix(predicciones, datos_pca$type.2)
print(confusion_matrix)
# Confusion matrix with percentages
confusion_table <- confusion_matrix$table
confusion_percent <- prop.table(confusion_table, margin = 1) * 100
confusion_percent <- round(confusion_percent, 2)
confusion_percent
