Skip to content

Conversation

@EmilHvitfeldt
Copy link
Member

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

@EmilHvitfeldt EmilHvitfeldt merged commit 21019d9 into main Aug 31, 2023
@EmilHvitfeldt EmilHvitfeldt deleted the fix124 branch August 31, 2023 21:34
@github-actions
Copy link

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.

@github-actions github-actions bot locked and limited conversation to collaborators Sep 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

2 participants