I'm trying to use case weights (importance) and get an error during resampling (res) which I cannot resolve:
→ A | error: Can't subset columns with case_wts
.
case_wts
must be numeric or character, not a <hardhat_importance_weights/hardhat_case_weights/vctrs_vctr> object.
There were issues with some computations A: x9
Warning: All models failed. Run show_notes(.Last.tune.result)
for more information.
Any guidance is appreciated
library(tidyverse)
library(tidymodels)
library(yardstick)
y1 <- c("T", "T", "T", "T", "T", "T", "F", "F", "F", "T")
y2 <- c("A", "B", "A", "B", "A", "B", "A", "B", "C", "D")
x3 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
x4 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
x5 <- c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
wts <- c(1,1,1,1,1,1,.1,.1,.1,1)
d <- data.frame(y1, y2, x3, x4, x5, wts)
case_wts <- importance_weights(as.numeric(d$wts))
rec <- recipe(y1~., data = d) %>%
step_dummy(all_nominal_predictors(), one_hot = T, trained = F, skip = F) %>%
step_center(all_numeric_predictors(), skip = F) %>%
step_scale(all_numeric_predictors(), skip = F)
spec <- boost_tree() %>% set_mode("classification") %>% set_engine("xgboost")
param_mod <- tunable(spec) %>% filter(component_id == "main") %>% pull(name)
param_mod1 <- param_mod[! param_mod %in% c("learn_rate", "mtry" , "min_n" , "loss_reduction" ,"sample_size" , "stop_iter" )]
param_tune <- purrr::map(param_mod, ~ set_names(list(tune()), .x)) %>% reduce(c)
update_spec <- spec %>% update(param_tune)
p <- extract_parameter_set_dials(update_spec)
grid <- grid_regular(finalize(p, d), levels = 2, original = T)
wf <- workflow() %>% add_model(update_spec) %>% add_recipe(rec) %>% add_case_weights(case_wts)
cv <- vfold_cv(d, v = 3, repeats = 3, strata = NULL, breaks = 1)
metrics <- metric_set(yardstick::roc_auc, yardstick::accuracy, yardstick::sensitivity)
ctr <- control_grid(verbose = FALSE, save_pred = TRUE, event_level = "second")
res <- wf %>% tune_grid(resamples = cv, grid = grid, metrics = metrics, control = ctr)
b <- select_best(res)
m <- finalize_workflow(wf, b) %>% fit(d)