I need to perform a test of independence on the following crosstab ct
using python:
Since there are some values less than 5, I cannot perform the chi-square test of independence. Instead, I need to perform Fisher's exact test.
Since Fisher's exact test implementation on Scipy supports only a 2x2 table, I implemented the following solution:
from scipy.stats import fisher_exact# Combine rows and columns to create a 2x2 tabletable_2x2 = np.array([[ct[1][4] + ct[2][4] + ct[1][3] + ct[2][3], ct[3][4] + ct[4][4] + ct[3][3] + ct[3][3]], [ct[1][2] + ct[2][2] + ct[1][1] + ct[2][1], ct[3][2] + ct[4][2] + ct[3][1] + ct[4][1]]])# Perform Fisher's exact test on the 2x2 tableodds_ratio, p_value = fisher_exact(table_2x2)# Display the resultsprint(f'Odds Ratio: {odds_ratio}')print(f'P-value: {p_value}')
Is this a valid solution? if not is there any other suggestion to implement this in Python?