↑ arvau

Wikipedia / G-test

In statistics, G-tests are likelihood-ratio or maximum likelihood statistical significance tests that are increasingly being used in situations where chi-squared tests were previously recommended.

Formulation

The general formula for test statistics of the G-test is

is the expected count under the null hypothesis denotes the natural logarithm is asymptotically chi-squared distributed as the total number of observations tends to infinity (convergence in distribution).

Furthermore, the total observed count must be equal to the total expected count:

is the total number of observations.

Both, the G and the chi-square test statistics are special cases of a general family of power divergence statistics by Cressie and Read set

Then,

Derivation

We can derive the value of the G-test from the log-likelihood ratio test where the underlying model is a multinomial model.

be the total number of observations. If we assume that the underlying model is multinomial, then the test statistic is defined by

is the maximum likelihood estimate given some data is given by

as

in the log-likelihood ratio, the equation simplifies to

(used to make the G-test formula asymptotically equivalent to the Pearson's chi-squared test statistics) to achieve the form

, and terms with zero observations can simply be dropped. However the expected) to apply the method.

Distribution and use

Given the null hypothesis that the observed frequencies result from random sampling from a distribution with the given expected frequencies, the distribution is approximately a chi-squared distribution, with the same number of degrees of freedom as in the corresponding chi-squared test.

For very small samples the multinomial test for goodness of fit, and Fisher's exact test for contingency tables, or even Bayesian hypothesis selection are preferable to the G-test. McDonald recommends to always use an exact test (exact test of goodness-of-fit, Fisher's exact test) if the total sample size is less than 1 000 .

  1. There is nothing magical about a sample size of 1 000, it's just a nice round number that is well within the range where an exact test, chi-square test, and G–test will give almost identical values. Spreadsheets, web-page calculators, and SAS shouldn't have any problem doing an exact test on a sample size of 1 000 .

        1. — John H. McDonald (2014)

G-tests have been recommended at least since the 1981 edition of Biometry, a statistics textbook by Robert R. Sokal and F. James Rohlf.

Relation to other metrics

Relation to the chi-squared test

The commonly used chi-squared tests for goodness of fit to a distribution and for independence in contingency tables are in fact approximations of the log-likelihood ratio on which the G-tests are based.

The general formula for Pearson's chi-squared test statistic is

The approximation of the G-test statistics by chi-squared test statistics is obtained by a second order Taylor expansion of the natural logarithm around 1 (see the derivation. When this difference is large, however, the approximation by the chi-squared test statistics begins to break down. Here, the effects of outliers in data will be more pronounced, and this explains the why chi-squared tests fail in situations with little data.

For samples of a reasonable size, the G-test and the chi-squared test will lead to the same conclusions. However, the approximation to the theoretical chi-squared distribution for the G-test is better than for the Pearson's chi-squared test. for some cell case the G-test is always better than the chi-squared test.

For testing goodness-of-fit the G-test is infinitely more efficient than the chi-squared test in the sense of Bahadur, but the two tests are equally efficient in the sense of Pitman or in the sense of Hodges and Lehmann.

Derivation (chi-squared)

Consider

using big Θ notation using big O notation, which should be true under the null hypothesis because of the central limit theorem and

follow, which will be used later.

Upon substitution we find,

Using the Taylor expansion yields

and distributing terms we find,

, we can write the result,

Relation to Kullback–Leibler divergence

The G-test statistic is proportional to the Kullback–Leibler divergence of the observed data:

, respectively.

Relation to mutual information

For analysis of contingency tables the value of the G-test statistics can also be expressed in terms of mutual information.

. Set

assuming independence is given by

Finally, the G-test statistics in this case is given by

be random variables with joint distribution of the contingency table, i.e.,

Then the G-test statistics can be expressed in several alternative forms:

where the entropies are given

and the joint entropy is given by

and the mutual information is

It can also be shown that the inverse document frequency weighting commonly used for text retrieval is an approximation of G applicable when the row sum for the query is much smaller than the row sum for the remainder of the corpus. Similarly, the result of Bayesian inference applied to a choice of single multinomial distribution for all rows of the contingency table taken together versus the more general alternative of a separate multinomial per row produces results very similar to the G-test statistic.

Application

Statistical software

  • In R fast implementations can be found in the AMR and Rfast packages. For the AMR package, the command is g.test which works exactly like chisq.test from base R. R also has the likelihood.test function in the Deducer package. Note: Fisher's G-test in the GeneCycle Package of the R programming language (fisher.g.test) does not implement the G-test as described in this article, but rather Fisher's exact test of Gaussian white-noise in a time series.

  • Another R implementation to compute the G-test statistic and corresponding p-values is provided by the R package entropy. The commands are Gstat for the standard G statistic and the associated p-value and Gstatindep for the G statistic applied to comparing joint and product distributions to test independence.

  • In SAS, one can conduct G-test by applying the /chisq option after the proc freq.

  • In Stata, one can conduct a G-test by applying the lr option after the tabulate command.

  • In Java, use org.apache.commons.math3.stat.inference.GTest.

  • In Python, use scipy.stats.power_divergence with lambda_=0.

References