You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Logistic回归模型的Accuracy ratio计算工具函数咨询

计算Logistic回归模型的Accuracy Ratio

在R中没有直接命名为“Accuracy ratio”的内置函数,但可以通过AUC(ROC曲线下面积)间接计算——Accuracy Ratio(AR)= 2*AUC - 1,下面提供两种可行的实现方式:

方法一:使用pROC包(推荐)

pROC是专门用于ROC分析的工具包,能快速计算AUC并推导AR:

  1. 安装并加载包:
install.packages("pROC")
library(pROC)
  1. 提取模型的预测概率:
pred_probs <- predict(glm_fit, type = "response")
  1. 计算AUC并转换为Accuracy Ratio:
roc_obj <- roc(binary ~ pred_probs)
accuracy_ratio <- 2 * roc_obj$auc - 1
print(accuracy_ratio)

方法二:手动计算(无需额外包)

如果不想依赖第三方包,可以手动基于排序后的真实值和预测分数计算:

# 获取真实标签和预测概率
actual_labels <- binary
pred_scores <- predict(glm_fit, type = "response")

# 按预测分数降序排序数据
sorted_data <- data.frame(actual = actual_labels, score = pred_scores)
sorted_data <- sorted_data[order(-sorted_data$score), ]

# 计算累计正例数和累计样本数
cum_positive <- cumsum(sorted_data$actual)
total_positive <- sum(sorted_data$actual)
cum_total <- 1:nrow(sorted_data)
total_samples <- nrow(sorted_data)

# 计算Gini系数并转换为Accuracy Ratio
gini <- (sum(cum_positive)/total_positive - (total_samples + 1)/2) / total_samples
accuracy_ratio <- 2 * gini
print(accuracy_ratio)

注意:你的模拟数据集只有9个样本,计算出的Accuracy Ratio随机性极强,不具备实际参考意义,实际建模时建议使用更大规模的数据集。

内容的提问来源于stack exchange,提问作者Daniel Lobo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 19:52:33