Skip to contents

1 Setup

We use the palmerpenguins data for illustration.

penguins_small <- palmerpenguins::penguins |>
  dplyr::select(-year) |>
  tidyr::drop_na()

2 Two types of presets

Some presets are mainly convenient shortcuts for combinations of mdist() arguments. For example, the "u_dep" preset corresponds to an unbiased association-aware distance construction using categorical association information, numerical preprocessing, and commensurability.

The available presets can be listed from dist_methods_tbl().

dist_methods_tbl() |>
  dplyr::filter(argument == "preset", method != "custom") |>
  dplyr::select(
    preset = method,
    response_aware,
    engine
  ) |>
  kableExtra::kbl()
preset response_aware engine
euclidean FALSE manydist
gower FALSE manydist
hl FALSE manydist
u_dep TRUE manydist
u_indep FALSE manydist
u_mix TRUE manydist
dkss FALSE kdml
gudmm FALSE external_port
mod_gower FALSE external_port

The engine column helps distinguish presets implemented directly in manydist from presets based on external constructions or ports. For example, "u_indep", "u_dep", "u_mix", and "hl" are implemented directly in manydist, whereas "dkss" uses the kdml implementation and "gudmm" and "mod_gower" are external ports.

3 Presets as argument shortcuts

Consider the "u_dep" preset, which computes an unbiased association-aware distance measure.

x_penguins <- penguins_small |>
  dplyr::select(-species)

d_u_dep_preset <- mdist(
  x_penguins,
  preset = "u_dep"
)

d_u_dep_explicit <- mdist(
  x_penguins,
  method_cat = "tvd",
  method_num = "pc_scores",
  commensurable = TRUE
)

all.equal(
  as.matrix(d_u_dep_preset$distance),
  as.matrix(d_u_dep_explicit$distance)
)
[1] TRUE

The result shows that, in this case, the preset is equivalent to the corresponding explicit specification of method_cat, method_num, and commensurable.

4 Presets based on specific distance constructions

Other presets are not merely combinations of the basic arguments above. Instead, they correspond to specific distance constructions, implementations, or external ports. For example, the "mod_gower" preset computes a modified version of Gower’s distance.

d_mod_gower <- mdist(
  x_penguins,
  preset = "mod_gower"
)

d_mod_gower
MDist object
  preset : mod_gower
  number of observations : 333
  number of continuous variables   : 4
  number of categorical variables   : 2 

Similarly, presets such as "gower", "gudmm", and "dkss" refer to specific distance constructions. In these cases, the preset identifies the distance method itself, rather than simply selecting a combination of method_cat, method_num, and commensurable.

The "euclidean" preset is slightly different: it uses a dedicated preprocessing strategy, converting categorical variables into one-hot dummy variables before computing Euclidean distances on the column-wise standardized dataset.

We can compare it with the standard Euclidean distance obtained from dist() on the numerical variables only, provided that the numerical variables are standardized first.

x_penguins_num <- penguins_small |>
  dplyr::select(where(is.numeric))

d_euc_mdist <- mdist(
  x_penguins_num,
  preset = "euclidean"
)

d_euc_dist <- dist(
  x_penguins_num |>
    dplyr::mutate(
      dplyr::across(
        dplyr::everything(),
        ~ as.numeric(scale(.x))
      )
    ),
  method = "euclidean"
)

all.equal(
  as.matrix(d_euc_mdist$distance),
  as.matrix(d_euc_dist)
)
[1] TRUE

The default remains method_num = "std", so the numerical-only Euclidean preset agrees with dist() applied after standardization. When all predictors are numeric, however, method_num can be overridden. For example, method_num = "none" skips numerical preprocessing and reproduces ordinary Euclidean distance on the original variables.

d_euc_unscaled <- mdist(
  x_penguins_num,
  preset = "euclidean",
  method_num = "none"
)

d_euc_dist_unscaled <- dist(
  x_penguins_num,
  method = "euclidean"
)

all.equal(
  as.matrix(d_euc_unscaled$distance),
  as.matrix(d_euc_dist_unscaled)
)
[1] TRUE

For numerical-only inputs, the supported choices are "none", "std", "range", "robust", and "pc_scores". This override is restricted to numerical-only data. When categorical variables are present, the "euclidean" preset retains its standard preprocessing strategy: categorical variables are one-hot encoded and method_num = "std" is used.