modified.chisq.test: Zero-Tolerant Pearson's Chi-squared Test for Contingency...

View source: R/modified.chisq.test.R

modified.chisq.testR Documentation

Zero-Tolerant Pearson's Chi-squared Test for Contingency Tables

Description

Performs Pearson's chi-squared test \insertCitepearson1900Upsilon on contingency tables, slightly modified to handle rows or columns of all zeros.

Usage

modified.chisq.test(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 p-value must be returned on a contingency table with valid non-negative counts, where the build-in R implementation of chisq.test could return NA as p-value, regardless of a pattern being strong or weak. See Examples.

Unlike chisq.test, 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 chi-squared test statistic (calculated ignoring entries of 0-expected count).

parameter

the degrees of freedom.

p.value

the p-value by the test.

estimate

Cramér's V statistic.

observed

the observed counts.

expected

the expected counts under the null hypothesis.

Note

This function only takes contingency table as input. It does not support goodness-of-fit test on vectors. It does not offer an option to apply Yates's continuity correction on 2 \times 2 tables.

References

\insertRef

pearson1900Upsilon

Examples

library("Upsilon")

# A table with a dominant function and an empty column
x <- matrix(
  c(0, 3, 0,
    3, 0, 0), 
   nrow = 2, byrow = TRUE)
print(x)

# Standard chisq.test fails or returns NA warning
chisq.test(x)

# Modified chi-squared test is significant:
modified.chisq.test(x)

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