modified.gtest: Zero-Tolerant G-Test for Contingency Tables

View source: R/modified.gtest.R

modified.gtestR Documentation

Zero-Tolerant G-Test for Contingency Tables

Description

Performs G-test \insertCiteWOOLF:1957aaUpsilon on contingency tables, slightly modified to handle rows or columns of all zeros.

Usage

modified.gtest(x, log.p = FALSE)

Arguments

x

a matrix or data frame of floating or integer numbers to specify a contingency table. Entries must be non-negative.

log.p

a logical. If TRUE, the p-value is calculated in closed form to natural logarithm of p-value to improve numerical precision when p-value approaches zero. Defaults to FALSE.

Details

This test is useful if a p-value must be returned on a contingency table with valid non-negative counts, where other implementations of G-test could return NA as the p-value, regardless of a pattern being strong or weak.

This function handles tables with empty rows or columns (where expected values are 0) by calculating the test statistic over non-zero entries only. This prevents the result from becoming NA, while giving meaningful p-values.

Value

A list with class "htest" containing:

statistic

the G statistic (log-likelihood ratio).

parameter

the degrees of freedom.

p.value

the p-value of the test.

estimate

the value of mutual information.

method

a character string indicating the method used.

data.name

a character string, name of the input data.

observed

the observed counts.

expected

the expected counts under the null hypothesis.

References

\insertRef

WOOLF:1957aaUpsilon

Examples

library("Upsilon")

# Create a sparse table with empty rows/cols
x <- matrix(
  c(0, 3, 0, 
    3, 0, 0), 
  nrow = 2, byrow = TRUE
)
print(x)
# Perform the modified G-test
modified.gtest(x)

Upsilon documentation built on March 7, 2026, 5:07 p.m.