Raw File
Tip revision: be291479202d9ca826914b9bf0fe0b8efa26e6c3 authored by Martin Lysy on 22 August 2022, 08:20:12 UTC
version 1.1.4
Tip revision: be29147
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/chi2.stat.R
\title{Chi-squared test statistic for contingency tables}
\item{tab}{A \code{K x C} matrix (contingency table) of counts. See details.}
The calculated value of the chi-squared statistic.
Calculates the chi-squared test statistic for a two-way contingency table.
Suppose that \code{tab} consists of counts from \eqn{K} populations (rows) in \eqn{C} categories.  The chi-squared test statistic is computed as
  \sum_{i=1}^K \sum_{j=1}^C (E_{ij} - O_{ij})^2/E_{ij},
  \sum_ij (E_ij - O_ij)^2/E_ij,
where \eqn{O_{ij}}{O_ij} is the observed number of counts in the \eqn{i}th row and \eqn{j}th column of \code{tab}, and \eqn{E_{ij}}{E_ij} is the expected number of counts under \eqn{H_0} that the populations have indentical proportions in each category:
  E_{ij} = \frac 1 N \sum_{i=1}^K O_{ij} \times \sum_{j=1}^C O_{ij}.
  E_ij = \sum_i O_ij * \sum_j O_ij / N,
where \eqn{N} is the total number of counts in \code{tab}.
# simple contingency table
ctab <- rbind(pop1 = c(5, 3, 0, 3),
                pop2 = c(4, 10, 2, 5))
colnames(ctab) <- LETTERS[1:4]
chi2.stat(ctab) # chi^2 test statistic
back to top