Skip to contents

Computes the correlation between x and y with every method that applies to their type combination and lines the results up side by side. Use it to check how much the method choice changes the estimate.

Usage

compare_methods(
  x,
  y,
  x_type = NULL,
  y_type = NULL,
  assume_latent_normal = "auto",
  ordinal_threshold = 10,
  ignore_na = TRUE,
  conf_level = 0.95,
  bootstrap = "auto",
  n_boot = 500,
  n_perm = 500,
  seed = NULL,
  verbose = TRUE
)

Arguments

x, y

Vectors of the same length. May be numeric, factor, character, or logical.

x_type, y_type

Optional character strings to override automatic type detection. One of "continuous", "count", "binary", "ordinal", or "categorical".

assume_latent_normal

"auto" (default) or logical. Decides which method gets the recommended flag, exactly as in smart_cor().

ordinal_threshold

Integer passed to detect_type(). Default: 10.

ignore_na

Logical. If TRUE (default), rows with missing values are removed. If FALSE, the function errors when NA values are present.

conf_level

Numeric between 0 and 1. Confidence level for every interval in the table. Default: 0.95.

bootstrap

Character or logical: "auto" (default) bootstraps only the methods without an analytic CI, TRUE bootstraps all of them, FALSE none.

n_boot

Integer. Bootstrap replications per method. Default: 500.

n_perm

Integer. Permutation shuffles for the Theil's U p-value. Default: 500.

seed

Optional integer seed for the bootstrap and permutation draws; the global RNG state is saved and restored.

verbose

Logical. If TRUE (default), prints informative messages.

Value

An object of class "smartcor_comparison": a list with elements:

results

A tibble::tibble() with columns method, method_label, estimate, statistic, p.value, ci_lower, ci_upper, ci_method, ci_source, and recommended (logical flag for the default method).

x_name

Name of the first variable.

y_name

Name of the second variable.

x_type

Detected (or specified) type of x.

y_type

Detected (or specified) type of y.

n_complete

Number of complete observations used.

conf_level

Confidence level used for the intervals.

Details

The function computes every method listed by available_methods() for the detected type pair. The recommended column marks the method that smart_cor() would select under the assume_latent_normal setting, so the recommended row always matches what smart_cor() and smart_cormat() return.

When one variable is continuous or count and the other is categorical, the continuous or count variable is binned into quantiles before Cramer's V is computed, exactly as smart_cor() does.

Methods that fail (a latent-variable optimiser that does not converge, say) are kept in the output with estimate = NA.

Examples

csv = system.file("extdata", "gss_2024_casestudy.csv", package = "smartcor")
gss = read.csv(csv)

# Two ordinal variables: Kendall, Spearman, gamma, and polychoric
compare_methods(gss$degree, gss$happy,
                assume_latent_normal = FALSE, bootstrap = FALSE)
#> 
#> ── Comparing methods ──
#> 
#> gss$degree ("ordinal") × gss$happy ("ordinal")
#> N: 3000
#> Methods: "polychoric", "kendall", "spearman", and "gamma"
#> 
#> "polychoric": -0.1031
#> "kendall": -0.0795 ← recommended
#> "spearman": -0.0906
#> "gamma": -0.1261
#> 
#> ── Method Comparison ───────────────────────────────────────────────────────────
#> Variables: gss$degree ("ordinal") × gss$happy ("ordinal")
#> N: 3000
#> 
#> Polychoric Correlation
#> r = -0.1031 p < 0.001 95% CI [-0.1456, -0.0601]
#> Kendall's Tau-b ← recommended
#> r = -0.0795 p < 0.001 95% CI [-0.1030, -0.0559]
#> Spearman Rank Correlation
#> r = -0.0906 p < 0.001 95% CI [-0.1261, -0.0549]
#> Goodman-Kruskal's Gamma
#> r = -0.1261 p < 0.001 95% CI [-0.1746, -0.0769]