<s>
In	O
statistics	O
,	O
G-tests	B-General_Concept
are	O
likelihood-ratio	B-General_Concept
or	O
maximum	O
likelihood	O
statistical	B-General_Concept
significance	I-General_Concept
tests	O
that	O
are	O
increasingly	O
being	O
used	O
in	O
situations	O
where	O
chi-squared	B-General_Concept
tests	I-General_Concept
were	O
previously	O
recommended	O
.	O
</s>
<s>
where	O
is	O
the	O
observed	O
count	O
in	O
a	O
cell	O
,	O
is	O
the	O
expected	O
count	O
under	O
the	O
null	B-General_Concept
hypothesis	I-General_Concept
,	O
denotes	O
the	O
natural	O
logarithm	O
,	O
and	O
the	O
sum	O
is	O
taken	O
over	O
all	O
non-empty	O
cells	O
.	O
</s>
<s>
G-tests	B-General_Concept
have	O
been	O
recommended	O
at	O
least	O
since	O
the	O
1981	O
edition	O
of	O
Biometry	O
,	O
a	O
statistics	O
textbook	O
by	O
Robert	O
R	B-Language
.	O
Sokal	O
and	O
F	O
.	O
James	O
Rohlf	O
.	O
</s>
<s>
We	O
can	O
derive	O
the	O
value	O
of	O
the	O
G-test	B-General_Concept
from	O
the	O
log-likelihood	B-General_Concept
ratio	I-General_Concept
test	I-General_Concept
where	O
the	O
underlying	O
model	O
is	O
a	O
multinomial	O
model	O
.	O
</s>
<s>
If	O
we	O
assume	O
that	O
the	O
underlying	O
model	O
is	O
multinomial	O
,	O
then	O
the	O
test	O
statistic	O
is	O
defined	O
bywhere	O
is	O
the	O
null	B-General_Concept
hypothesis	I-General_Concept
and	O
is	O
the	O
maximum	O
likelihood	O
estimate	O
(	O
MLE	O
)	O
of	O
the	O
parameters	O
given	O
the	O
data	O
.	O
</s>
<s>
Recall	O
that	O
for	O
the	O
multinomial	O
model	O
,	O
the	O
MLE	O
of	O
given	O
some	O
data	O
is	O
defined	O
byFurthermore	O
,	O
we	O
may	O
represent	O
each	O
null	B-General_Concept
hypothesis	I-General_Concept
parameter	O
asThus	O
,	O
by	O
substituting	O
the	O
representations	O
of	O
and	O
in	O
the	O
log-likelihood	B-General_Concept
ratio	I-General_Concept
,	O
the	O
equation	O
simplifies	O
toRelabel	O
the	O
variables	O
with	O
and	O
with	O
.	O
</s>
<s>
Given	O
the	O
null	B-General_Concept
hypothesis	I-General_Concept
that	O
the	O
observed	O
frequencies	O
result	O
from	O
random	O
sampling	O
from	O
a	O
distribution	O
with	O
the	O
given	O
expected	O
frequencies	O
,	O
the	O
distribution	O
of	O
G	O
is	O
approximately	O
a	O
chi-squared	O
distribution	O
,	O
with	O
the	O
same	O
number	O
of	O
degrees	O
of	O
freedom	O
as	O
in	O
the	O
corresponding	O
chi-squared	B-General_Concept
test	I-General_Concept
.	O
</s>
<s>
For	O
very	O
small	O
samples	O
the	O
multinomial	B-General_Concept
test	I-General_Concept
for	O
goodness	O
of	O
fit	O
,	O
and	O
Fisher	B-General_Concept
's	I-General_Concept
exact	I-General_Concept
test	I-General_Concept
for	O
contingency	B-Application
tables	I-Application
,	O
or	O
even	O
Bayesian	O
hypothesis	O
selection	O
are	O
preferable	O
to	O
the	O
G-test	B-General_Concept
.	O
</s>
<s>
McDonald	O
recommends	O
to	O
always	O
use	O
an	O
exact	O
test	O
(	O
exact	O
test	O
of	O
goodness-of-fit	O
,	O
Fisher	B-General_Concept
's	I-General_Concept
exact	I-General_Concept
test	I-General_Concept
)	O
if	O
the	O
total	O
sample	O
size	O
is	O
less	O
than	O
1	O
000	O
.	O
</s>
<s>
There	O
is	O
nothing	O
magical	O
about	O
a	O
sample	O
size	O
of	O
1	O
000	O
,	O
it	O
's	O
just	O
a	O
nice	O
round	O
number	O
that	O
is	O
well	O
within	O
the	O
range	O
where	O
an	O
exact	O
test	O
,	O
chi-square	B-General_Concept
test	I-General_Concept
,	O
and	O
G	O
–	O
test	O
will	O
give	O
almost	O
identical	O
values	O
.	O
</s>
<s>
Spreadsheets	O
,	O
web-page	O
calculators	O
,	O
and	O
SAS	B-Language
should	O
n't	O
have	O
any	O
problem	O
doing	O
an	O
exact	O
test	O
on	O
a	O
sample	O
size	O
of	O
1	O
000	O
.	O
</s>
<s>
The	O
commonly	O
used	O
chi-squared	B-General_Concept
tests	I-General_Concept
for	O
goodness	O
of	O
fit	O
to	O
a	O
distribution	O
and	O
for	O
independence	O
in	O
contingency	B-Application
tables	I-Application
are	O
in	O
fact	O
approximations	O
of	O
the	O
log-likelihood	B-General_Concept
ratio	I-General_Concept
on	O
which	O
the	O
G-tests	B-General_Concept
are	O
based	O
.	O
</s>
<s>
For	O
samples	O
of	O
a	O
reasonable	O
size	O
,	O
the	O
G-test	B-General_Concept
and	O
the	O
chi-squared	B-General_Concept
test	I-General_Concept
will	O
lead	O
to	O
the	O
same	O
conclusions	O
.	O
</s>
<s>
However	O
,	O
the	O
approximation	O
to	O
the	O
theoretical	O
chi-squared	O
distribution	O
for	O
the	O
G-test	B-General_Concept
is	O
better	O
than	O
for	O
the	O
Pearson	B-General_Concept
's	I-General_Concept
chi-squared	I-General_Concept
test	I-General_Concept
.	O
</s>
<s>
In	O
cases	O
where	O
for	O
some	O
cell	O
case	O
the	O
G-test	B-General_Concept
is	O
always	O
better	O
than	O
the	O
chi-squared	B-General_Concept
test	I-General_Concept
.	O
</s>
<s>
For	O
testing	O
goodness-of-fit	O
the	O
G-test	B-General_Concept
is	O
infinitely	O
more	O
efficient	O
than	O
the	O
chi	B-General_Concept
squared	I-General_Concept
test	I-General_Concept
in	O
the	O
sense	O
of	O
Bahadur	O
,	O
but	O
the	O
two	O
tests	O
are	O
equally	O
efficient	O
in	O
the	O
sense	O
of	O
Pitman	O
or	O
in	O
the	O
sense	O
of	O
Hodges	O
and	O
Lehmann	O
.	O
</s>
<s>
The	O
G-test	B-General_Concept
statistic	O
is	O
proportional	O
to	O
the	O
Kullback	O
–	O
Leibler	O
divergence	O
of	O
the	O
theoretical	O
distribution	O
from	O
the	O
empirical	O
distribution	O
:	O
</s>
<s>
For	O
analysis	O
of	O
contingency	B-Application
tables	I-Application
the	O
value	O
of	O
G	O
can	O
also	O
be	O
expressed	O
in	O
terms	O
of	O
mutual	O
information	O
.	O
</s>
<s>
is	O
the	O
mutual	O
information	O
between	O
the	O
row	O
vector	O
r	B-Language
and	O
the	O
column	O
vector	O
c	O
of	O
the	O
contingency	B-Application
table	I-Application
.	O
</s>
<s>
Similarly	O
,	O
the	O
result	O
of	O
Bayesian	O
inference	O
applied	O
to	O
a	O
choice	O
of	O
single	O
multinomial	O
distribution	O
for	O
all	O
rows	O
of	O
the	O
contingency	B-Application
table	I-Application
taken	O
together	O
versus	O
the	O
more	O
general	O
alternative	O
of	O
a	O
separate	O
multinomial	O
per	O
row	O
produces	O
results	O
very	O
similar	O
to	O
the	O
G	O
statistic	O
.	O
</s>
<s>
The	O
McDonald	O
–	O
Kreitman	O
test	O
in	O
statistical	O
genetics	O
is	O
an	O
application	O
of	O
the	O
G-test	B-General_Concept
.	O
</s>
<s>
In	O
R	B-Language
fast	O
implementations	O
can	O
be	O
found	O
in	O
the	O
and	O
packages	O
.	O
</s>
<s>
For	O
the	O
AMR	O
package	O
,	O
the	O
command	O
is	O
g.test	O
which	O
works	O
exactly	O
like	O
chisq.test	O
from	O
base	O
R	B-Language
.	O
R	B-Language
also	O
has	O
the	O
function	O
in	O
the	O
package	O
.	O
</s>
<s>
Note	O
:	O
Fisher	O
's	O
G-test	B-General_Concept
in	O
the	O
of	O
the	O
R	B-Language
programming	I-Language
language	I-Language
(	O
fisher.g.test	O
)	O
does	O
not	O
implement	O
the	O
G-test	B-General_Concept
as	O
described	O
in	O
this	O
article	O
,	O
but	O
rather	O
Fisher	B-General_Concept
's	I-General_Concept
exact	I-General_Concept
test	I-General_Concept
of	O
Gaussian	O
white-noise	O
in	O
a	O
time	O
series	O
.	O
</s>
<s>
Another	O
R	B-Language
implementation	O
to	O
compute	O
the	O
G	O
statistic	O
and	O
corresponding	O
p-values	O
is	O
provided	O
by	O
the	O
R	B-Language
package	O
.	O
</s>
<s>
In	O
SAS	B-Language
,	O
one	O
can	O
conduct	O
G-test	B-General_Concept
by	O
applying	O
the	O
/chisq	O
option	O
after	O
the	O
proc	O
freq	O
.	O
</s>
<s>
In	O
Stata	B-Algorithm
,	O
one	O
can	O
conduct	O
a	O
G-test	B-General_Concept
by	O
applying	O
the	O
lr	O
option	O
after	O
the	O
tabulate	O
command	O
.	O
</s>
<s>
In	O
Java	B-Language
,	O
use	O
org.apache.commons.math3.stat.inference.GTest	O
.	O
</s>
<s>
In	O
Python	B-Language
,	O
use	O
scipy.stats.power_divergence	O
with	O
lambda_	O
=	O
0	O
.	O
</s>
