[banner]

An R Companion for the Handbook of Biological Statistics

Salvatore S. Mangiafico

G–test of Goodness-of-Fit

The G-test goodness-of-fit test can be performed with the G.test function in the package RVAideMemoire.  As another alternative, you can use R to calculate the statistic and p-value manually.

 

Examples in Summary and Analysis of Extension Program Evaluation

SAEEPER: Goodness-of-Fit Tests for Nominal Variables

 

Packages used in this chapter

The following commands will install these packages if they are not already installed:


if(!require(DescTools)){install.packages("DescTools")}
if(!require(RVAideMemoire)){install.packages("RVAideMemoire")}

 

When to use it

Null hypothesis

How the test works

Post-hoc test

Assumptions

See the Handbook for information on these topics.

 

Examples: extrinsic hypothesis

G-test goodness-of-fit test with DescTools and RVAideMemoire


### --------------------------------------------------------------
### Crossbill example, G-test goodness-of-fit, p. 55
### --------------------------------------------------------------

observed = c(1752, 1895)    # observed frequencies
expected = c(0.5, 0.5)      # expected proportions

library(DescTools)

GTest(x=observed,
      p=expected,
      correct="none")            # "none" "williams" "yates"

 

Log likelihood ratio (G-test) goodness of fit test

 

G = 5.6085, X-squared df = 1, p-value = 0.01787

 


library(RVAideMemoire)

G.test(x=observed,
       p=expected)

 

G-test for given probabilities

G = 5.6085, df = 1, p-value = 0.01787

 

#     #     #

 

 

G-test goodness-of-fit test by manual calculation

 

### --------------------------------------------------------------
### Crossbill example, G-test goodness-of-fit, p. 55
###   Manual calculation
### --------------------------------------------------------------

observed      = c(1752, 1895)     # observed frequencies
expected.prop = c(0.5, 0.5)       # expected proportions

degrees = 1                       # degrees of freedom

expected.count = sum(observed)*expected.prop

G = 2 * sum(observed * log(observed / expected.count))

G                          

[1] 5.608512

 

pchisq(G,
       df=degrees,
       lower.tail=FALSE)

 

 

[1] 0.01787343

#     #     #

 

 

Examples of G-test goodness-of-fit test with DescTools and RVAideMemoire

 

### --------------------------------------------------------------
### Rice example, G-test goodness-of-fit, p. 55
### --------------------------------------------------------------

observed = c(772, 1611, 737)
expected = c(0.25, 0.50, 0.25)

library(DescTools)

GTest(x=observed,
      p=expected,
      correct="none")            # "none" "williams" "yates"

 

Log likelihood ratio (G-test) goodness of fit test

 

G = 4.1471, X-squared df = 2, p-value = 0.1257



library(RVAideMemoire)

G.test(x=observed,
       p=expected)

 

G-test for given probabilities

G = 4.1471, df = 2, p-value = 0.1257

 

#     #     #

 

 

### --------------------------------------------------------------
### Foraging example, G-test goodness-of-fit, pp. 55
56
### --------------------------------------------------------------

observed = c(70, 79, 3, 4)
expected = c(0.54, 0.40, 0.05, 0.01)

library(DescTools)   

GTest(x=observed,
      p=expected,
      correct="none")            # "none" "williams" "yates"
     

 

Log likelihood ratio (G-test) goodness of fit test

 

G = 13.145, X-squared df = 3, p-value = 0.004334



library(RVAideMemoire)

G.test(x=observed,
       p=expected)

 

G-test for given probabilities

G = 13.1448, df = 3, p-value = 0.004334

 

#     #     #

 

 

Example: intrinsic hypothesis

 

### --------------------------------------------------------------
### Intrinsic example, G-test goodness-of-fit, amphipod, p. 56
### --------------------------------------------------------------

observed       = c(1203,  2919,  1678)
expected.prop  = c(.21073, 0.49665, 0.29262)
 
     ### Note: These are recalculated for more precision
     ###       In this case, low precision probabilities
     ###         change the results

expected.count = sum(observed)*expected.prop
  
G = 2 * sum(observed * log(observed / expected.count))

G                         

[1] 1.032653

 

 

pchisq(G,
       df=1,
       lower.tail=FALSE)  

 

[1] 0.3095363

 

#     #     #

 

 

Graphing the results

Graphing would be the same as in the “Chi-square Test of Goodness-of-Fit” section.

 

Similar tests

Chi-square vs. G–test

See the Handbook for information on these topics.  The exact test of goodness-of-fit and the chi-square test of goodness-of-fit tests are described elsewhere in this book.

 

How to do the test

These examples are shown above.

 

Power analysis

Power analysis would be the same as in the “Chi-square Test of Goodness-of-Fit” section.