如何使用 tidymodels 设置结果变量中的“事件”级别?

叶全伟*_*叶全伟 2 r tidymodels

我正在使用进行机器学习,我想预测二元响应/结果。如何指定哪个级别的结果是“事件”或积极案例?

这种情况发生在食谱中还是其他地方?


##split the data
anxiety_split <- initial_split(anxiety_df, strata = anxiety)


anxiety_train <- training(anxiety_split)
anxiety_test <- testing(anxiety_split)


set.seed(1222) 
anxiety_cv <- vfold_cv(anxiety_train, strata = anxiety)

anxiety_rec <- recipe(anxiety ~ ., data = anxiety_train, positive = 'pos') %>%
  step_corr(all_numeric()) %>%
  step_dummy(all_nominal(), -all_outcomes()) %>%
  step_zv(all_numeric()) %>%
  step_normalize(all_numeric())
Run Code Online (Sandbox Code Playgroud)

Jul*_*lge 8

在评估模型之前,您不需要设置结果变量的哪个级别是“事件”。您可以使用event_level大多数尺度函数的参数来完成此操作。例如,查看如何执行此操作yardstick::roc_curve()

library(yardstick)
#> For binary classification, the first factor level is assumed to be the event.
#> Use the argument `event_level = "second"` to alter this as needed.
library(tidyverse)

data(two_class_example)


## looks good!
two_class_example %>%
  roc_curve(truth, Class1, event_level = "first") %>%
  autoplot()
Run Code Online (Sandbox Code Playgroud)



## YIKES!! we got this backwards
two_class_example %>%
  roc_curve(truth, Class1, event_level = "second") %>%
  autoplot()
Run Code Online (Sandbox Code Playgroud)

由reprex 包于 2020 年 8 月 2 日创建(v0.3.0.9001)

注意标准启动时的消息;假设第一个因素级别是事件。这与基本 R 的作用类似。您只需要担心event_level您的“事件”是否不是第一因素级别。