explore_correlation_table

source

explore_correlation_table(
    df,
    digits=2,
    bold=0.05,
    *,
    caption=None,
    title=None,
    subtitle=None,
)

Correlation table with Pearson above and Spearman below the diagonal.

Parameters

Name Type Description Default
df pd.DataFrame Data frame with at least two numeric/logical variables and five observations. required
digits int Number of decimals to display (0 < digits < 5). 2
bold float Correlations with a p-value below bold are shown in bold. Set to 0 to disable. Must satisfy 0 <= bold < 1. 0.05
caption str | None Optional table title. None

Returns

Name Type Description
CorrelationTableResult df_corr/df_prob/df_n (numeric matrices keyed by the original variable names) and gt (a Great Tables object using letter labels).

Examples

Basic — Pearson (above) and Spearman (below) correlations for a few variables (slice the columns first, then label so the data dictionary supplies row names):

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(
    load_kuznets()[["gini_regional", "gdp_pc", "log_gdp_pc"]],
    load_kuznets_data_def(),
)
ex.explore_correlation_table(df).gt
A B C
A: Regional inequality (Gini) 0.20 -0.09
B: GDP per capita (USD) -0.19 0.82
C: Log GDP per capita -0.19 1.00
This table reports Pearson correlations above and Spearman correlations below the diagonal. Number of observations: 880. Correlations with significance levels below 5% appear in bold.

Advanced — more decimals, a stricter bold threshold, a caption, and the raw coefficient/p-value matrices from .df_corr / .df_prob:

import expdpy as ex
from expdpy.data import load_kuznets, load_kuznets_data_def

df = ex.set_labels(
    load_kuznets()[["gini_regional", "gdp_pc", "log_gdp_pc", "trade_share"]],
    load_kuznets_data_def(),
)
result = ex.explore_correlation_table(
    df,
    digits=3,
    bold=0.01,
    caption="Correlations (kuznets)",
)
result.gt
result.df_corr
result.df_prob
gini_regional gdp_pc log_gdp_pc trade_share
gini_regional 1.000000e+00 1.440404e-09 9.294102e-03 0.000004
gdp_pc 1.276770e-08 1.000000e+00 2.447400e-219 0.014868
log_gdp_pc 1.276770e-08 0.000000e+00 1.000000e+00 0.412617
trade_share 9.938982e-06 8.381995e-02 8.381995e-02 1.000000