2

I am conducting the G test in both R and Python and I am getting different results, the results I am getting in Python being wrong. Somehow I am misapplying the formula.

The data are:

prfs
Sex F   M
Pref        
B   29  17
A   2   12

The R Code is :

library(RVAideMemoire)
G.test(prfs)
G-test

data:  prfs
G = 11.025, df = 1, p-value = 0.0008989

The Python code is :

stats.power_divergence(prfs, lambda_ = 'log-likelihood')
Power_divergenceResult(statistic=array([28.14366538,  0.86639163]), pvalue=array([1.12635722e-07, 3.51956200e-01]))

stats.power_divergence(prfs, lambda_ = 'log-likelihood', axis = None, ddof = 2)
Power_divergenceResult(statistic=29.07673602201342, pvalue=6.956736686069527e-08)
halfer
  • 19,824
  • 17
  • 99
  • 186
user8270077
  • 4,621
  • 17
  • 75
  • 140

1 Answers1

3

Its an old question but following answer may help:

obs = np.array([[29,17], [2,12]])
# G test with scipy: 
from scipy.stats import * 
g, p, dof, expctd = chi2_contingency(obs, lambda_="log-likelihood")
print("G={}; df={}; P={}".format(g, dof, p))

Output:

G=8.859368223179882; df=1; P=0.0029158847773319975

The values are similar to those obtained by R method.

Reference for above method is here.

rnso
  • 23,686
  • 25
  • 112
  • 234