首页 > 解决方案 > 为什么 AUC 仅略有改善,而分类预测变量的影响却相当显着?

问题描述

我正在尝试用 gam (mgcv) 对 R 中网球比赛的结果进行建模。结果是特定玩家的输赢(0或1)。在我的数据集中有一个分类预测器。如果我不将此预测器添加到模型中,我可以清楚地看到此预测器的影响,因为如果我对训练和测试数据进行以下查询:

  my_data %>%
  group_by(categorical predictor) %>%
  summarize(wf_train = mean(wf) %>%
  arrange(desc(wf_train)) 

并 cbind 数据框,我得到了这个结果:

在此处输入图像描述

训练和测试数据中的模式是相同的。虽然如果我将分类预测器添加到我的模型中。AUC 仅从 0.7141 提高到 0.7146。我想知道可能是什么原因。出于这个原因,我构建了一些示例数据,看看这是否会产生类似的结果。请参阅下面的代码。

结果与我的真实数据有些相似。AUC 仅从 0.6349 略微提高到 0.6436。虽然比我的真实数据要多得多。尽管基于类别的差异,我所期望的仍然非常少。虽然这只是直觉上的。有什么解释为什么改进如此之小?

我还注意到分类变量中的每个级别都有不同的 AUC 级别(请参见下表中的最后两列)。这是正确的还是表明它不是最佳建模的?

library(mgcv)
library(dplyr)
library(caret)
library(mltools)


# generate sample data:
set.seed(1234)
size_dataset <- 10000
x1 <- rep(seq(1, 100), size_dataset)
x2 <- runif(n = length(x1), min = 0, max = 0.2)
x3 <- sin(0.1*x1)/30
y_prob <- 0.50 + ((x1 + x1^3 - 0.007 * x1^4) * 0.000002 + x2 + x3) / 2

x4 <- sample(x = c(0:4), size = length(x1), replace = TRUE)
x4 <- x4/50
y_prob <- y_prob + x4

y_win <- rbinom(length(y_prob), size = 1, prob=y_prob)

df <- cbind(x1, x2, x3, x4, y_win)
df <- as.data.frame(df)
df$x4 <- as.factor(df$x4)


# split 75/25 train/test : 
smp_size <- floor(0.75 * nrow(df))
set.seed(123)
train_ind <- sample(seq_len(nrow(df)), size = smp_size)
train <- df[train_ind, ]
test <- df[-train_ind, ]


# fit the model without the catagorical variable:
mygam1 <- bam(y_win ~ s(x1, k = 3), 
              method = 'REML',
              family = binomial,
              data = train)

p0 <- predict(mygam1, type="response") # predict op traindata

train <- cbind(train, p0)


# fit the model inclusive the catagorical variable:
mygam2 <- bam(y_win ~ s(x1, k = 3) + x4, 
              method = 'REML',
              family = binomial,
              data = train)

p1 <- predict(mygam1, type="response", newdata = test)
p2 <- predict(mygam2, type="response", newdata = test)

test <- left_join(test, vb2, by = "x4")
test <- cbind(test, p1, p2)

# contruct the table with outcomes:
output_metrics <- test %>%
  group_by(x4) %>%
  summarise(count_n = n(), mean_y_win = mean(y_win), mean_p1 = mean(p1), mean_p2 = mean(p2))

output2 <- test %>%
  group_by(x4) %>% 
  summarize(AUC_p1 = auc_roc(p1, y_win))

output3 <- test %>%
  group_by(x4) %>% 
  summarize(AUC_p2 = auc_roc(p2, y_win))

output <- left_join(output_metrics, output2, by = "x4")
output <- left_join(output, output3, by = "x4")

print(output)

auc_roc(test$p1, test$y_win)
auc_roc(test$p2, test$y_win)

输出是:

# A tibble: 5 x 7
  x4    count_n mean_y_win mean_p1 mean_p2 AUC_p1 AUC_p2
  <fct>   <int>      <dbl>   <dbl>   <dbl>  <dbl>  <dbl>
1 0       49783      0.665   0.704   0.664  0.626  0.626
2 0.02    50040      0.683   0.704   0.683  0.628  0.628
3 0.04    49893      0.706   0.704   0.705  0.635  0.635
4 0.06    50166      0.725   0.704   0.725  0.642  0.642
5 0.08    50118      0.746   0.704   0.745  0.649  0.649
> 
> auc_roc(test$p1, test$y_win)
[1] 0.6348791
> auc_roc(test$p2, test$y_win)
[1] 0.6436047

谢谢!

标签: raucmgcv

解决方案


推荐阅读