smartcor detects variable types and selects a suitable correlation method for each pair. It supports continuous, count, binary, ordinal, and categorical variables and returns the estimate, inference, selected method, and rationale. The package is available in two languages, kept at feature parity: smartcor for R, on CRAN, and pysmartcor for Python, on PyPI.
Installation
Install the R package from CRAN:
install.packages("smartcor")Install the Python package from PyPI:
pip install pysmartcorThe two implementations share the same decision logic, estimators, and inference; see smartcor in Python for the full R-to-Python translation table.
Quick start in R
The package bundles a frozen extract of the 2024 General Social Survey, used in all examples.
library(smartcor)
csv = system.file("extdata", "gss_2024_casestudy.csv", package = "smartcor")
gss = read.csv(csv)Correlate one pair and get the estimate, the selected method, and the reasoning:
result = smart_cor(gss$coninc, gss$age, verbose = FALSE)
print(result)
#>
#> ── Smart Correlation ───────────────────────────────────────────────────────────
#> Estimate: 0.0192
#> Method: Pearson Correlation (count treated as continuous)
#> Variables: gss$coninc ("continuous") × gss$age ("count")
#> N: 3000
#> p-value: 0.2921
#> Test: t-test on r (cor.test)
#> H0: rho = 0
#> Small p-values (e.g., p < 0.05) indicate evidence against H0.
#> 95% CI: [-0.0166, 0.0550]
#> Source: Fisher z (cor.test)
#>
#> One variable is continuous and the other is a count. Counts are treated as
#> numeric continuous variables; Pearson correlation selected (matches base R
#> cor()).
#>
#> ℹ Alternatives: "spearman" and "kendall" (pass `method = "..."` to use)Build a matrix across mixed variable types:
columns = c("age", "coninc", "degree", "happy", "sex", "region")
matrix = smart_cormat(
gss[, columns],
assume_latent_normal = FALSE,
verbose = FALSE
)
round(matrix$correlations, 3)
#> age coninc degree happy sex region
#> age 1.000 0.019 0.028 -0.059 -0.009 0.068
#> coninc 0.019 1.000 0.451 -0.168 -0.101 0.092
#> degree 0.028 0.451 1.000 -0.079 0.015 0.058
#> happy -0.059 -0.168 -0.079 1.000 -0.005 0.035
#> sex -0.009 -0.101 0.015 -0.005 1.000 0.063
#> region 0.068 0.092 0.058 0.035 0.063 1.000
matrix$methods
#> age coninc degree happy
#> age "pearson" "pearson" "spearman" "spearman"
#> coninc "pearson" "pearson" "spearman" "spearman"
#> degree "spearman" "spearman" "kendall" "kendall"
#> happy "spearman" "spearman" "kendall" "kendall"
#> sex "point_biserial" "point_biserial" "rank_biserial" "rank_biserial"
#> region "cramers_v" "cramers_v" "cramers_v" "cramers_v"
#> sex region
#> age "point_biserial" "cramers_v"
#> coninc "point_biserial" "cramers_v"
#> degree "rank_biserial" "cramers_v"
#> happy "rank_biserial" "cramers_v"
#> sex "phi" "cramers_v"
#> region "cramers_v" "cramers_v"Plot it:
ggcor_heatmap(matrix)
Quick start in Python
The same workflow in Python, with the same bundled data:
from importlib.resources import as_file, files
import pandas as pd
from pysmartcor import smart_cor, smart_cormat
with as_file(files("pysmartcor").joinpath("data/gss_2024_casestudy.csv")) as csv:
gss = pd.read_csv(csv)
result = smart_cor(gss["coninc"], gss["age"], verbose=False)
print(result)Smart Correlation
Estimate: 0.0192
Method: Pearson Correlation
Variables: coninc (continuous) x age (count)
N: 3000
p-value: 0.2921 (exact null distribution of r, equivalent to the t-test on r (scipy.stats.pearsonr))
H0: rho = 0
95% CI: [-0.0166, 0.0550] (Fisher z (scipy.stats.pearsonr))
One variable is continuous and the other is a count (treated as continuous); Pearson correlation selected.
Alternatives: spearman, kendallcolumns = ["age", "coninc", "degree", "happy", "sex", "region"]
matrix = smart_cormat(gss[columns], assume_latent_normal=False, verbose=False)
print(matrix.correlations.round(3)) age coninc degree happy sex region
age 1.000 0.019 0.028 -0.059 -0.009 0.068
coninc 0.019 1.000 0.451 -0.168 -0.101 0.092
degree 0.028 0.451 1.000 -0.079 0.015 0.058
happy -0.059 -0.168 -0.079 1.000 -0.005 0.035
sex -0.009 -0.101 0.015 -0.005 1.000 0.063
region 0.068 0.092 0.058 0.035 0.063 1.000Methods
The package implements Pearson, Spearman, Kendall’s tau, point-biserial, rank-biserial, phi, tetrachoric, Yule’s Q, polychoric, polyserial, Cramer’s V, Theil’s U, Tschuprow’s T, and Goodman-Kruskal’s gamma.
Paper
The accompanying paper, smartcor: Intelligent Correlation Method Selection for Mixed Variable Types by M. Harshvardhan and Pritam Ranjan (2026), is available as an arXiv preprint: arXiv:2607.22285 (doi:10.48550/arXiv.2607.22285). The package vignettes cover the same material: method selection, the underlying theory, and inference.