stats.cramers_v¶
cramers_v ¶
cramers_v(data: DataInput | DataSource, column: str, by: str, *, bias_corrected: bool = True, include_null: bool = False, max_categories: int = 50, max_cells: int = 1000) -> pl.DataFrame
Calculate Cramér's V for two nominal or categorical-coded variables.
Parameters¶
data
A polars DataFrame, LazyFrame, or parquet source.
column, by
Distinct columns representing categorical variables. Integer-coded
and other bounded numeric categories are accepted. Nulls are
excluded pairwise unless include_null=True. Floating NaN and
infinity are always excluded.
bias_corrected
Apply the finite-sample correction proposed by Bergsma (2013).
Defaults to True; False gives the conventional estimate,
without a Yates continuity correction.
include_null
Treat null as a distinct category in each column. A literal string
'(null)' is not conflated with an actual null.
max_categories
Maximum number of observed levels per column. This is a practical
guard, not a guarantee of fixed peak memory use.
max_cells
Maximum full contingency-table size (row levels × column levels).
Returns¶
pl.DataFrame
A one-row table with cramers_v (null when undefined), sample
size, contingency-table dimensions, chi-square statistic, and
expected-cell diagnostics. No p-values or source category values
are returned.
Notes¶
Cramér's V describes association strength, not direction or causation. Pairwise-complete records are used by default. For patient-level interpretation, consider whether repeated encounters are independent.
Example¶
import polarstory as ps
result = ps.stats.cramers_v(
df,
'ins_medicaid',
'enctype',
bias_corrected=True,
)
print(result)
report = ps.Report('Categorical associations')
report.add_table(result, title='Insurance and encounter type')
report.write_html('associations.html')
Interpretation¶
Cramér's V measures strength, not direction. The value lies between
0 and 1 when defined. By default, the estimator uses the finite-sample
bias correction proposed by Bergsma (2013). Use
bias_corrected=False for the conventional estimate.
Null observations are excluded pairwise by default. If
include_null=True, null is a distinct category and is not conflated
with a literal category named '(null)'. Floating NaN and infinity are
excluded in either case.
The returned one-row polars DataFrame includes the coefficient, the
complete-case sample size, contingency-table dimensions, chi-square
statistic, and diagnostics for expected cell counts below 5. Values are
undefined (null) if either variable has fewer than two observed
categories or if the corrected denominator is not positive.
This is a descriptive association measure, not a significance test. Repeated patient encounters can affect interpretation. A small expected count is a caution about sparse categories, not a reason to discard an observed association automatically.
Note: SciPy's association(..., correction=True) applies a Yates
continuity correction where applicable; it does not implement the
Bergsma finite-sample correction used here. For comparisons to SciPy,
use bias_corrected=False and correction=False in SciPy.