Hi,
Im trying to model a binary classification problem where I need the f_measure (with beta = 0.5) as metric. However in my problem I need to use the second label as the relevant class. This can be done by specifying the event_level option in the f_meas function. However when using the metric_set function to set the metrics ( I need cv in my real case) I get the following Error:
"formal argument "event_level" matched by multiple actual arguments"
However, if I leave out the event_level option, the code runs through, but of course the metric is now on the first class. I know that i could change the levels, but I need the metric for both classes.
Here is my reprex, what is wrong?
library(tidyverse)
library(tidymodels)
library(DALEX)
set.seed(123)
data("titanic_imputed", package = "DALEX")
f_meas_first <- function(data,
truth,
estimate,
na_rm = TRUE,
...) {
f_meas(
data,
truth = !! rlang::enquo(truth),
estimate = !! rlang::enquo(estimate),
beta = 0.5,
na_rm = na_rm,
#event_level = "second",
...
)
}
f_meas_first <- new_class_metric(f_meas_first, "maximize")
metrics_first <- metric_set(f_meas_first)
f_meas_second <- function(data,
truth,
estimate,
na_rm = TRUE,
...) {
f_meas(
data,
truth = !! rlang::enquo(truth),
estimate = !! rlang::enquo(estimate),
beta = 0.5,
na_rm = na_rm,
event_level = "second",
...
)
}
f_meas_second <- new_class_metric(f_meas_second, "maximize")
metrics_second <- metric_set(f_meas_second)
data <- titanic_imputed %>%
as_tibble() %>%
mutate(label = as.factor(survived)) %>% select(-survived)
#%>% select(age,fare,sibsp,parch,survivedlabel)
model.glm <-
logistic_reg() %>%
set_engine("glm")
workflow.glm <-
workflow() %>%
add_formula(label ~ .) %>%
add_model(model.glm)
fit.glm <-
workflow.glm %>%
fit(data = data)
prediction <- bind_cols(
data %>% select(label),
predict(fit.glm, new_data = data, type = "class"))
metrics_first(prediction, truth = label, estimate =.pred_class )
f_meas_first(prediction, truth = label, estimate = .pred_class )
metrics_second(prediction, truth = label, estimate =.pred_class )
f_meas_second(prediction, truth = label, estimate = .pred_class )