I'm currently trying to extract and fit my tuning results from a wflow set and running into some issues with fit_best. I'm running log reg with the glmnet engine which supports case weights. I added my importance weights into my dataset
int <- int |>
group_by(SubjectID) |>
mutate(
visits = as.numeric(n()),
visits_group = case_when(
visits <= 3 ~ 'low',
visits > 3 & visits <= 6 ~ 'med',
visits > 6 ~ 'high'
)
) |>
dplyr::ungroup() |>
relocate(visits, visits_group, .before = Ethnicity) |>
mutate(is_female = as.factor(is_female),
imp_weight = importance_weights(1/visits))
I checked that they are the correct object
> hardhat::is_case_weights(int$imp_weight)
[1] TRUE
I checked that it was in my recipe
> sex_recipes[1]
$transcript
── Recipe ─────────────────────────────────────────────────────────────────────────────────────────
── Inputs
Number of variables by role
outcome: 1
predictor: 10346
case_weights: 1
ID: 1972
I made my worflow set
sex_models_set <-
workflow_set(
preproc = sex_recipes,
models = list(glmnet = tune_lg_glmnet_el),
case_weights = imp_weight,
cross = TRUE
)
I extracted my workflows to check that my case weights were there (showing one for simplicity)
transcript_wflow <- extract_workflow(sex_models_set, "transcript_glmnet")
> transcript_wflow
══ Workflow ═══════════════════════════════════════════════════════════════════════════════════════
Preprocessor: Recipe
Model: logistic_reg()
── Preprocessor ───────────────────────────────────────────────────────────────────────────────────
2 Recipe Steps
• step_zv()
• step_normalize()
── Case Weights ───────────────────────────────────────────────────────────────────────────────────
imp_weight
── Model ──────────────────────────────────────────────────────────────────────────────────────────
Logistic Regression Model Specification (classification)
Main Arguments:
penalty = tune()
mixture = tune()
Computational engine: glmnet
I tune my workdlow and it runs fine but, when I try to extract and fit my results I get this
> fit_best(protein_result, metric = "accuracy")
Error in `vec_equal()`:
! Can't combine `..1` <importance_weights> and `..2` <double>.
Run `rlang::last_trace()` to see where the error occurred.
> rlang::last_trace()
<error/vctrs_error_ptype2>
Error in `vec_equal()`:
! Can't combine `..1` <importance_weights> and `..2` <double>.
---
Backtrace:
▆
1. ├─tune::fit_best(protein_result, metric = "accuracy")
2. └─tune:::fit_best.tune_results(protein_result, metric = "accuracy")
3. ├─parsnip::fit(wflow, dat)
4. └─workflows:::fit.workflow(wflow, dat)
5. └─workflows:::toggle_sparsity(object, data)
6. ├─recipes::.recipes_estimate_sparsity(extract_preprocessor(object))
7. └─recipes:::.recipes_estimate_sparsity.recipe(extract_preprocessor(object))
8. └─sparsevctrs::sparsity(template, sample = 1000)
9. └─sparsevctrs:::sparsity_df(x)
10. └─base::vapply(x, count_zeroes, double(1))
11. └─sparsevctrs (local) FUN(X[[i]], ...)
12. └─vctrs:::`==.vctrs_vctr`(x, 0)
13. └─vctrs::vec_equal(e1, e2)
Run rlang::last_trace(drop = FALSE) to see 11 hidden frames.
> fit_best(sex_models_set)
Error in `vec_equal()`:
! Can't combine `..1` <importance_weights> and `..2` <double>.
Run `rlang::last_trace()` to see where the error occurred.
My hypothesis is that it's trying to check for sparsity in the imp_weight column even though it isn't a predictor, but I don't know how to correct this behavior. Any help is greatly appreciated?