【R语言】单个分类模型性能评估、两个分类模型性能对比、统计检验

单个模型评估

R 复制代码
# install.packages("pROC")
library(pROC)

calculate_metrics <- function(label, prediction) {
  # 加载所需的包
  library(pROC)
  
  # 计算ROC曲线和AUC
  roc_obj <- roc(label, prediction)
  auc_value <- auc(roc_obj)
  
  # 寻找最佳cutoff
  cutoff <- coords(roc_obj, "best", ret = "threshold")
  
  # 根据cutoff计算混淆矩阵
  predicted_label <- ifelse(prediction >= cutoff$threshold, 1, 0)
  confusion_matrix <- table(predicted_label, label)
  
  # 提取混淆矩阵中的TP,TN,FP和FN
  TP <- confusion_matrix[2, 2]
  TN <- confusion_matrix[1, 1]
  FP <- confusion_matrix[2, 1]
  FN <- confusion_matrix[1, 2]
  
  # 计算敏感度和特异度
  sensitivity <- TP / (TP + FN)
  specificity <- TN / (TN + FP)
  
  # 返回结果
  result <- list(
    AUC = auc_value,
    Cutoff = cutoff$threshold,
    Confusion_Matrix = confusion_matrix,
    TP = TP,
    TN = TN,
    FP = FP,
    FN = FN,
    Sensitivity = sensitivity,
    Specificity = specificity
  )
  
  return(result)
}

# 使用示例数据调用函数
set.seed(123)
label <- sample(0:1, 100, replace = TRUE)
pred <- runif(100)
metrics <- calculate_metrics(label, pred)
print(metrics)

多个模型对比分析及统计检验

R 复制代码
# install.packages("pROC")
library(pROC)
compare_models <- function(label, pred1, pred2) {
  # 加载所需的包
  library(pROC)
  
  # 计算ROC曲线
  roc1 <- roc(label, pred1)
  roc2 <- roc(label, pred2)
  
  # Delong检验
  delong_test_result <- roc.test(roc1, roc2, method = "delong")
  
  # 寻找最佳阈值
  cutoff1 <- coords(roc1, "best", ret = "threshold")
  cutoff2 <- coords(roc2, "best", ret = "threshold")
  
  # 计算敏感度和特异度
  predicted_label1 <- ifelse(pred1 >= cutoff1$threshold, 1, 0)
  predicted_label2 <- ifelse(pred2 >= cutoff2$threshold, 1, 0)
  
  sensitivity1 <- sum(predicted_label1 == 1 & label == 1) / sum(label == 1)
  sensitivity2 <- sum(predicted_label2 == 1 & label == 1) / sum(label == 1)
  
  specificity1 <- sum(predicted_label1 == 0 & label == 0) / sum(label == 0)
  specificity2 <- sum(predicted_label2 == 0 & label == 0) / sum(label == 0)
  
  # 比例检验
  sensitivity_test <- prop.test(
    x = c(sum(predicted_label1 == 1 & label == 1), sum(predicted_label2 == 1 & label == 1)),
    n = c(sum(label == 1), sum(label == 1)),
    correct = FALSE
  )
  
  specificity_test <- prop.test(
    x = c(sum(predicted_label1 == 0 & label == 0), sum(predicted_label2 == 0 & label == 0)),
    n = c(sum(label == 0), sum(label == 0)),
    correct = FALSE
  )
  
  # 返回结果
  result <- list(
    Delong_Test_Result = delong_test_result,
    Sensitivity_Test_Result = sensitivity_test,
    Specificity_Test_Result = specificity_test,
    Sensitivity1 = sensitivity1,
    Sensitivity2 = sensitivity2,
    Specificity1 = specificity1,
    Specificity2 = specificity2,
    Cutoff1 = cutoff1$threshold,
    Cutoff2 = cutoff2$threshold
  )
  
  return(result)
}

# 使用示例数据调用函数
set.seed(123)
label <- sample(0:1, 100, replace = TRUE)
pred1 <- runif(100)
pred2 <- runif(100)

comparison_result <- compare_models(label, pred1, pred2)
print(comparison_result)
相关推荐
大只因bug几秒前
基于Hadoop的热门旅游景点推荐数据分析与可视化系统(基于Django大数据技术的热门旅游景点数据分析与可视化)
大数据·hadoop·数据分析·旅游景点可视化分析系统·热门旅游景点推荐系统·旅游景点分析与可视化系统·hadoop智能推荐系统
watersink21 分钟前
Dify框架下的基于RAG流程的政务检索平台
人工智能·深度学习·机器学习
lizz3131 分钟前
机器学习中的线性代数:奇异值分解 SVD
线性代数·算法·机器学习
程序员Linc39 分钟前
计算机视觉 vs 机器视觉 | 机器学习 vs 深度学习:核心差异与行业启示
深度学习·机器学习·计算机视觉·机器视觉
不去幼儿园39 分钟前
【启发式算法】Dijkstra算法详细介绍(Python)
人工智能·python·算法·机器学习·启发式算法·图搜索算法
serve the people1 小时前
神经网络中梯度计算求和公式求导问题
神经网络·算法·机器学习
大数据追光猿1 小时前
【大模型技术】LlamaFactory 的原理解析与应用
人工智能·python·机器学习·docker·语言模型·github·transformer
神秘的土鸡2 小时前
如何在WPS中接入DeepSeek并使用OfficeAI助手(超细!成功版本)
人工智能·机器学习·自然语言处理·数据分析·llama·wps
潘达斯奈基~2 小时前
机器学习4-PCA降维
人工智能·深度学习·机器学习
終不似少年遊*3 小时前
综合使用pandas、numpy、matplotlib、seaborn库做数据分析、挖掘、可视化项目
开发语言·python·机器学习·numpy·pandas·matplotlib·seaborn