Skip to contents

A thin wrapper around smart_cormat() for pipeline work. It never prompts, prints nothing, and returns two plain data frames (the estimates and the method used for each pair) instead of an S3 object.

Usage

smart_cor_df(
  data,
  cols = NULL,
  types = NULL,
  assume_latent_normal = "auto",
  ordinal_threshold = 10,
  ignore_na = TRUE,
  conf_level = 0.95,
  alpha = 0.05,
  bootstrap = "auto",
  n_boot = 500,
  n_perm = 500,
  seed = NULL,
  polyserial_ml = FALSE,
  nearPD = FALSE
)

Arguments

data

A data frame or tibble.

cols

Optional character vector of column names to include. Default: all columns.

types

Optional named list mapping column names to types ("continuous", "count", "binary", "ordinal", "categorical"). Columns not listed are auto-detected.

assume_latent_normal

"auto" (default) or logical, passed to smart_cormat(). NULL is not useful here because this wrapper never prompts.

ordinal_threshold

Integer passed to detect_type(). Default: 10.

ignore_na

Logical. If TRUE (default), missing values are handled pairwise. If FALSE, pairs with missing values come back NA.

conf_level

Numeric between 0 and 1. Confidence level passed through to each smart_cor() call. Default: 0.95.

alpha

Significance level for the diagnostic tests, passed through to each smart_cor() call. Default: 0.05.

bootstrap

Character or logical, passed through: "auto" (default), TRUE, or FALSE.

n_boot

Integer. Bootstrap replications per pair. Default: 500.

n_perm

Integer. Permutation shuffles for the Theil's U and Freeman's theta p-values. Default: 500.

seed

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

polyserial_ml

Logical, passed to smart_cormat(). Default: FALSE; TRUE is recommended (see smart_cor()).

nearPD

Logical, passed to smart_cormat(). Default: FALSE. If TRUE, correlations contains the positive-definite adjustment; methods describes the original pairwise estimates. Use smart_cormat() to retain the original matrix and adjustment diagnostics.

Value

A named list with two elements:

correlations

A data frame of correlation estimates (numeric).

methods

A data frame of method codes (character) used for each pair.

Details

By default it uses assume_latent_normal = "auto", so each pair is screened with the diagnostics described in smart_cor() and the method follows them (a saturated 2 x 2 pair gets phi).

The two returned data frames share identical row and column names (the variable names from data). Diagonal entries in correlations are 1; diagonal entries in methods name the method a self-pair of that type would get, matching smart_cormat().

Examples

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

# Correlations and methods for four columns in the frozen GSS extract
result = smart_cor_df(
  gss[, c("age", "coninc", "degree", "sex")],
  assume_latent_normal = FALSE
)
result$correlations
#>                age     coninc     degree         sex
#> age     1.00000000  0.0369073 0.02074942 -0.01038809
#> coninc  0.03690730  1.0000000 0.35688067 -0.13360217
#> degree  0.02074942  0.3568807 1.00000000  0.01535207
#> sex    -0.01038809 -0.1336022 0.01535207  1.00000000
result$methods
#>                  age        coninc        degree           sex
#> age          pearson      spearman       kendall rank_biserial
#> coninc      spearman       pearson       kendall rank_biserial
#> degree       kendall       kendall       kendall rank_biserial
#> sex    rank_biserial rank_biserial rank_biserial       yules_q

# Missing values are handled pairwise by default
example = gss[, c("age", "coninc", "sex")]
example$coninc[1:3] = NA
smart_cor_df(example, assume_latent_normal = FALSE)
#> $correlations
#>                age      coninc         sex
#> age     1.00000000  0.03701115 -0.01038809
#> coninc  0.03701115  1.00000000 -0.13314710
#> sex    -0.01038809 -0.13314710  1.00000000
#> 
#> $methods
#>                  age        coninc           sex
#> age          pearson      spearman rank_biserial
#> coninc      spearman       pearson rank_biserial
#> sex    rank_biserial rank_biserial       yules_q
#>