I've created an example below that uses {tidyclust} and {tidymodels} to calculate resampling metrics. My hope was to use an l^\infty /supremum/Chebyshev norm rather than the conventional Euclidean one, but you can see by running the code that the metric values are too high to be consistent with the method="chebyshev" option for {philentropy}'s distance functions. I don't know how to specify a dist_fun in {tidyclust} other than within the model, and it doesn't seem to be used for calculating metrics. Thanks in advance for your time!
library(philentropy)
library(rlang)
library(tibble)
library(tidyclust)
library(tidymodels)
get_data <- function(dimension = 100, number_of_points = 30) {
matrix(
rnorm(number_of_points * dimension),
nrow = number_of_points,
ncol = dimension
) |>
as_tibble(.name_repair = "unique") |>
set_names(paste0("x_", seq_len(dimension)))
}
get_metrics <- function(dataset, distance_method = "euclidean") {
distance_function1 <- function(x) philentropy::distance(x, method = distance_method)
distance_function2 <- function(x, y) philentropy::dist_many_many(x, y, method = distance_method)
a_cv <- vfold_cv(dataset, v = 5)
a_model <- hier_clust(
num_clusters = tune(),
linkage_method = "complete",
dist_fun = distance_function1
) |>
set_engine('stats') |>
set_mode('partition')
a_model
a_recipe <- recipe(dataset) |>
update_role(starts_with("x_"), new_role = "predictor")
summary(a_recipe)
a_workflow <- workflow() |>
add_model(a_model) |>
add_recipe(a_recipe)
n_clusters_grid <- grid_regular(num_clusters(range = c(10L, 25L)), levels = 16)
a_resample <- tune_cluster(
a_workflow,
resamples = a_cv,
grid = n_clusters_grid,
control = control_grid(save_pred = TRUE, extract = identity),
metrics = cluster_metric_set(
sse_total,
sse_within_total,
sse_ratio,
silhouette_avg
)
)
a_resample_metrics <- a_resample |>
collect_metrics()
a_resample_metrics
}
dataset <- get_data()
e1 <- get_metrics(dataset, distance_method = "euclidean")
# These are too high to be based on the $l^\infty$/supremum/Chebyshev norm.
c1 <- get_metrics(dataset, distance_method = "chebyshev")