The function hypothesis_predictor_core_c() performs statistical tests to evaluate the association between two categorical variables using a contingency table. It takes into account the viability of various statistical tests to decide which tests are appropriate given the characteristics of the data. This error handling ensures the inputs are correctly formatted and appropriate for the analysis to prevent runtime errors and logical inconsistencies.
Detailed Error Handling Breakdown
Type Validations
Contingency Table Type Check
Ensures that the contingency_table is a pandas DataFrame. This is essential because the function relies on DataFrame methods to compute statistical tests.
if not isinstance(contingency_table, pd.DataFrame):
raise TypeError("hypothesis_predictor_core_c(): The 'contingency_table' parameter must be a pandas DataFrame.")
Boolean Type Checks for Test Viability Flags
Checks that chi2_viability, barnard_viability, boschloo_viability, fisher_viability, and yates_correction_viability are all booleans. This is crucial because these flags determine which statistical tests are appropriate based on pre-evaluated criteria.
if not isinstance(chi2_viability, bool):
raise TypeError("hypothesis_predictor_core_c(): The 'chi2_viability' parameter must be a boolean.")
# Similar checks for barnard_viability, boschloo_viability, fisher_viability, and yates_correction_viability
String Type Check for alternative
Verifies that the alternative parameter is a string, which is necessary for specifying the hypothesis direction in exact tests.
if not isinstance(alternative, str):
raise TypeError("hypothesis_predictor_core_c(): The 'alternative' parameter must be a string.")
Value Validations
Contingency Table Emptiness Check
Checks that the contingency table is not empty to ensure there is data available for hypothesis testing.
if contingency_table.empty:
raise ValueError("hypothesis_predictor_core_c(): The input contingency table is empty.")
Validation of alternative Parameter
Ensures the alternative parameter matches one of the expected values ('two-sided', 'less', 'greater'). This is crucial for correct interpretation and execution of exact tests.
valid_alternatives = ['two-sided', 'less', 'greater']
if alternative not in valid_alternatives:
raise ValueError(f"hypothesis_predictor_core_c(): Invalid 'alternative' value. Expected one of {valid_alternatives}, got '{alternative}'.")
Implementation Summary
The function
hypothesis_predictor_core_c()
performs statistical tests to evaluate the association between two categorical variables using a contingency table. It takes into account the viability of various statistical tests to decide which tests are appropriate given the characteristics of the data. This error handling ensures the inputs are correctly formatted and appropriate for the analysis to prevent runtime errors and logical inconsistencies.Detailed Error Handling Breakdown
Type Validations
contingency_table
is a pandas DataFrame. This is essential because the function relies on DataFrame methods to compute statistical tests.chi2_viability
,barnard_viability
,boschloo_viability
,fisher_viability
, andyates_correction_viability
are all booleans. This is crucial because these flags determine which statistical tests are appropriate based on pre-evaluated criteria.alternative
alternative
parameter is a string, which is necessary for specifying the hypothesis direction in exact tests.Value Validations
alternative
Parameteralternative
parameter matches one of the expected values ('two-sided', 'less', 'greater'). This is crucial for correct interpretation and execution of exact tests.Link to Full Code