tidymodels / tidyclust

A tidy unified interface to clustering models
https://tidyclust.tidymodels.org/
Other
109 stars 17 forks source link

Fix bug where tune_cluster doesn't like id variables in recipes #168

Closed EmilHvitfeldt closed 1 year ago

EmilHvitfeldt commented 1 year ago

To close #124

pacman::p_load(tidyverse, tidymodels, tidyclust, janitor, ClusterR)

data <- mtcars %>%
  rownames_to_column(var = "model")

kmeans_spec <- k_means(num_clusters = 4) %>%
  set_engine("ClusterR")

rec_spec <- recipe(~., data = data) %>%
  update_role(model, new_role = "id variable") %>%
  step_dummy(all_nominal_predictors()) %>%
  step_zv(all_predictors()) %>%
  step_normalize(all_numeric_predictors()) %>%
  step_pca(all_numeric_predictors(), threshold = 0.8)

kmeans_spec <- kmeans_spec %>%
  set_args(num_clusters = tune())

kmeans_wf <- workflow(rec_spec, kmeans_spec)

set.seed(1234)

boots <- bootstraps(data, times = 10)

tune_res <- tune_cluster(
  kmeans_wf,
  resamples = boots
)

tune_res
#> # Tuning results
#> # Bootstrap sampling 
#> # A tibble: 10 × 4
#>    splits          id          .metrics          .notes          
#>    <list>          <chr>       <list>            <list>          
#>  1 <split [32/13]> Bootstrap01 <tibble [16 × 5]> <tibble [0 × 3]>
#>  2 <split [32/7]>  Bootstrap02 <tibble [16 × 5]> <tibble [0 × 3]>
#>  3 <split [32/12]> Bootstrap03 <tibble [16 × 5]> <tibble [0 × 3]>
#>  4 <split [32/11]> Bootstrap04 <tibble [16 × 5]> <tibble [0 × 3]>
#>  5 <split [32/11]> Bootstrap05 <tibble [16 × 5]> <tibble [0 × 3]>
#>  6 <split [32/10]> Bootstrap06 <tibble [16 × 5]> <tibble [0 × 3]>
#>  7 <split [32/9]>  Bootstrap07 <tibble [16 × 5]> <tibble [0 × 3]>
#>  8 <split [32/11]> Bootstrap08 <tibble [16 × 5]> <tibble [0 × 3]>
#>  9 <split [32/14]> Bootstrap09 <tibble [16 × 5]> <tibble [0 × 3]>
#> 10 <split [32/11]> Bootstrap10 <tibble [16 × 5]> <tibble [0 × 3]>

Created on 2023-08-31 with reprex v2.0.2

github-actions[bot] commented 1 year ago

This pull request has been automatically locked. If you believe you have found a related problem, please file a new issue (with a reprex: https://reprex.tidyverse.org) and link to this issue.