TransWikia.com

chi squared test in python libraries

Cross Validated Asked by eurohacker on December 20, 2021

As I understand it, statsmodels.stats.contingency_tables.Table.test_nominal_association() and scipy.stats.chi2_contingency() refer to the same test, however the results are different:

import numpy as np

### Contingency table
tab = np.array([[6,20],[13,5]])

### SciPy
from scipy.stats import chi2_contingency

chi_val,p_val,dof,exp_val=chi2_contingency(tab)
print("Chi square testnChi squared = %fnp           = %fnDOF         = %d"%(chi_val,p_val,dof))

Chi square test
Chi squared = 8.563275
p           = 0.003430
DOF         = 1
### Statsmodels

import statsmodels.api as sm

table = sm.stats.Table(tab)
res =  table.test_nominal_association()
print (res)

df          1
pvalue      0.0012129356662224922
statistic   10.470535312640576


Is there a fundamental difference between the two stats?

One Answer

scipy uses the continuity correction, statsmodels does not. If you pass correction=False to the scipy test, then the results will be identical.

Answered by Kerby Shedden on December 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP