setwd("E:/xx")
data <- read.csv("data.csv")
data <- na.omit(data)
data_scaled <- scale(data[,3:ncol(data)])
data_normalized <- as.data.frame(data_scaled)
data0 <- data.frame(group = data$group, data_normalized)
library(caret)
set.seed(2020)
trainlist <- createDataPartition(data0$group, p = 0.7, list = FALSE)
trainset0 <- data0[trainlist, ]
testset0 <- data0[-trainlist, ]

knn_results <- list()
group_mapping <- c("Model_0" = 0, "Model_1" = 1, "Model_2" = 2, "Model_3" = 3, "Model_4" = 4, "Model_5" = 5, "Model_6" = 6)
for (model_name in names(group_mapping)) { 
  current_group <- group_mapping[[model_name]]
  xyz <- as.matrix(trainset0[trainset0$group == current_group, -which(names(trainset0) == "group")])
  knn_values <- numeric(nrow(data0))
  for (i in 1:nrow(data0)) {
  point <- as.matrix(data0[i, -which(names(data0) == "group")])
    library(FNN)
    K <- 1
    knn_result <- get.knnx(xyz, point, k = K)
    nearest_distances <- knn_result$nn.dist
    avg_knn_distance <- mean(nearest_distances)
    knn_values[i] <- avg_knn_distance
  }
  knn_results[[model_name]] <- knn_values
}
knn_df <- data.frame(Group = data0$group) 
for (model_name in names(group_mapping)){
  knn_df[[model_name]] <- knn_results[[model_name]]
}
print(head(knn_df))

knn_df$Group <- as.factor(knn_df$Group)
train_index <- createDataPartition(knn_df$Group, p = 0.7, list = FALSE)
train_data <- knn_df[train_index, ]
test_data <- knn_df[-train_index, ]
logistic_model <- train(Group ~ .,data = train_data,method = "multinom",trControl = trainControl(method = "cv", number = 6))
print(logistic_model)
test_predictions <- predict(logistic_model, newdata = test_data)
confusion_matrix <- confusionMatrix(test_predictions, test_data$Group)
print(confusion_matrix)

library(ggplot2)
test_data0 <- knn_df[-train_index, ]
knn_long <- reshape2::melt(test_data0, id.vars = "Group", variable.name = "Model", value.name = "KNN_Distance")
ggplot(knn_long, aes(x = Group, y = KNN_Distance, fill = Model)) +
  geom_boxplot() +
  labs(title = "KNN Distance Distribution by Group and Model", x = "Group", y = "KNN Distance") +
  theme_minimal()
boxplot_colors <- scales::hue_pal()(length(unique(knn_long$Model)))
print(boxplot_colors)

library(caret)
library(ggplot2)
library(reshape2)
conf_matrix_data <- as.data.frame(confusion_matrix$table)
ggplot(data = conf_matrix_data, aes(x = Prediction, y = Reference, fill = Freq)) +
  geom_tile() +
  geom_text(aes(label = Freq), color = "white") +
  labs(title = "Confusion Matrix Heatmap", x = "Predicted", y = "Actual") +
  scale_fill_gradient(low = "lightblue", high = "blue") +
  theme_minimal()

library(pROC)
predicted_probabilities_train <- predict(logistic_model, newdata = train_data, type = "prob")
train_labels <- as.factor(train_data$Group)
roc_list_train <- list()
custom_labels_train <- c("Cyst", "type A", "type AB", "type B1", "type B2", "type B3", "type C")
max_length_train <- max(nchar(custom_labels_train))
formatted_labels_train <- sapply(custom_labels_train, function(label) {
  sprintf(paste0("%-", max_length_train, "s"), label)
})
text_x_train <- 0.4
text_y_train <- 0.3
auc_values_train <- numeric(length(levels(train_labels)))
for (i in 1:length(levels(train_labels))) {
  class_label_train <- formatted_labels_train[i]
  binary_labels_train <- as.numeric(train_labels == levels(train_labels)[i])
  roc_obj_train <- roc(binary_labels_train, predicted_probabilities_train[, i])
  auc_values_train[i] <- round(roc_obj_train$auc, 2) 
  roc_list_train[[class_label_train]] <- roc_obj_train
  if (i == 1) {
    plot(roc_obj_train, col = boxplot_colors[i], main = "ROC Curve of Training Set (Logistic Regression)", lwd = 2)
  } else {
    plot(roc_obj_train, col = boxplot_colors[i], add = TRUE, lwd = 2)
  }
}
for (i in 1:length(auc_values_train)) {
  auc_label_train <- sprintf("AUC = %.2f", auc_values_train[i])
  text(text_x_train-0.1, text_y_train+0.05, formatted_labels_train[i], col = boxplot_colors[i])
  text(text_x_train-0.3, text_y_train+0.05, auc_label_train, col = boxplot_colors[i]) 
  text_y_train <- text_y_train - 0.05 
}

library(pROC)
predicted_probabilities <- predict(logistic_model, newdata = test_data, type = "prob")
test_labels <- as.factor(test_data$Group)
roc_list <- list()
custom_labels <- c("Cyst", "type A", "type AB", "type B1", "type B2", "type B3", "type C")
max_length <- max(nchar(custom_labels))
formatted_labels <- sapply(custom_labels, function(label) {
  sprintf(paste0("%-", max_length, "s"), label)
})
text_x <- 0.4
text_y <- 0.3
auc_values <- numeric(length(levels(test_labels)))
for (i in 1:length(levels(test_labels))) {
  class_label <- formatted_labels[i]
  binary_labels <- as.numeric(test_labels == levels(test_labels)[i])
  roc_obj <- roc(binary_labels, predicted_probabilities[, i])
  auc_values[i] <- round(roc_obj$auc, 2) 
  roc_list[[class_label]] <- roc_obj
  if (i == 1) {
    plot(roc_obj, col = boxplot_colors[i], main = "ROC Curve of Validation Set (Logistic Regression)", lwd = 2)
  } else {
    plot(roc_obj, col = boxplot_colors[i], add = TRUE, lwd = 2)
  }
}
for (i in 1:length(auc_values)) {
  auc_label <- sprintf("AUC = %.2f", auc_values[i])
  text(text_x-0.1, text_y+0.05, formatted_labels[i], col = boxplot_colors[i])
  text(text_x-0.3, text_y+0.05, auc_label, col = boxplot_colors[i]) 
  text_y <- text_y - 0.05 
}