contingency Module#
2x2 contingency table calculations for epidemiology.
This module provides the Table2x2 class for performing epidemiological
calculations on 2x2 contingency tables, including risk ratios, odds ratios,
risk differences, and various confidence intervals.
Classes#
- class episia.stats.contingency.Table2x2(a, b, c, d)[source]#
Bases:
object2x2 contingency table for epidemiological calculations.
Represents a standard 2x2 table layout: +—————-+———–+———–+ | | Exposed | Unexposed | +================+===========+===========+ | Cases | a | b | +—————-+———–+———–+ | Non-cases | c | d | +—————-+———–+———–+
- __init__(a, b, c, d)[source]#
Initialize a 2x2 contingency table.
- Parameters:
- Raises:
ValueError – If any cell value is negative
- a#
- attributable_fraction_exposed()[source]#
Calculate attributable fraction among the exposed.
AF_exposed = (RR - 1) / RR Represents proportion of cases among exposed attributable to exposure.
- Returns:
-∞ to 1)
- Return type:
Attributable fraction (range
- attributable_fraction_population()[source]#
Attributable fraction in the population (PAF).
PAF = Pe * (RR - 1) / (Pe * (RR - 1) + 1)
where Pe = proportion of cases that are exposed = a / (a + b).
- Returns:
-∞ to 1)
- Return type:
Population attributable fraction (range
- b#
- c#
- d#
- odds_ratio(method=ConfidenceMethod.WALD, confidence=0.95)[source]#
Calculate odds ratio with confidence interval.
Odds Ratio = (a/c) / (b/d) = (a*d) / (b*c)
- Parameters:
method (ConfidenceMethod) – Method for confidence interval calculation
confidence (float) – Confidence level (default: 0.95 for 95% CI)
- Returns:
OddsRatioResult object containing estimate and confidence interval
- Return type:
Example
>>> table = Table2x2(10, 20, 30, 40) >>> result = table.odds_ratio() >>> print(result.estimate) 0.6667
- risk_difference(confidence=0.95)[source]#
Calculate risk difference (attributable risk) with confidence interval.
Risk Difference = Risk_exposed - Risk_unexposed
- risk_ratio(method=ConfidenceMethod.WALD, confidence=0.95)[source]#
Calculate risk ratio (relative risk) with confidence interval.
Risk Ratio = (a/(a+c)) / (b/(b+d))
- Parameters:
method (ConfidenceMethod) – Method for confidence interval calculation
confidence (float) – Confidence level (default: 0.95 for 95% CI)
- Returns:
RiskRatioResult object containing estimate and confidence interval
- Return type:
Example
>>> table = Table2x2(10, 20, 30, 40) >>> result = table.risk_ratio() >>> print(result.estimate) 0.6667
- class episia.stats.contingency.RiskRatioResult(estimate, ci_lower, ci_upper, method, table, null_value=1.0)[source]#
Bases:
objectResult object for Risk Ratio calculations.
- Parameters:
Functions#
- episia.stats.contingency.risk_ratio(a, b, c, d, **kwargs)[source]#
Convenience function to calculate risk ratio from raw counts.
- Parameters:
- Returns:
RiskRatioResult object
- Return type:
Example
>>> result = risk_ratio(10, 20, 30, 40) >>> print(result.estimate)
- episia.stats.contingency.odds_ratio(a, b, c, d, **kwargs)[source]#
Convenience function to calculate odds ratio from raw counts.
- Parameters:
- Returns:
OddsRatioResult object
- Return type:
Examples#
Creating a 2x2 table:
from episia.stats.contingency import Table2x2, ConfidenceMethod
# Table: Exposed vs Unexposed, Cases vs Non-cases
table = Table2x2(a=40, b=10, c=20, d=30)
print(f"Risk in exposed: {table.risk_exposed:.3f}")
print(f"Risk in unexposed: {table.risk_unexposed:.3f}")
Risk ratio calculation:
# Risk ratio with Wald confidence interval
rr = table.risk_ratio(method=ConfidenceMethod.WALD, confidence=0.95)
print(rr) # Risk Ratio: 2.667 (1.514-4.696)
print(f"Significant: {rr.significant}")
Odds ratio calculation:
# Odds ratio with exact confidence interval
or_result = table.odds_ratio(method=ConfidenceMethod.EXACT)
print(or_result) # Odds Ratio: 6.000 (2.241-16.788)
Comprehensive summary:
summary = table.summary()
print(f"Chi-square: {summary['chi_square']['chi2']:.3f}")
print(f"Fisher exact p-value: {summary['fisher_exact']['p_value']:.4f}")
From DataFrame:
import pandas as pd
from episia.stats.contingency import from_dataframe
df = pd.DataFrame({
'exposed': [1, 1, 0, 0, 1, 0],
'case': [1, 0, 1, 0, 1, 0]
})
table = from_dataframe(df, exposed_col='exposed', outcome_col='case')