Learn R Programming

MADPop (version 1.1.7)

chi2.stat: Chi-squared test statistic for contingency tables

Description

Calculates the chi-squared test statistic for a two-way contingency table.

Usage

chi2.stat(tab)

Value

The calculated value of the chi-squared statistic.

Arguments

tab

A K x C matrix (contingency table) of counts. See details.

Details

Suppose that tab consists of counts from \(K\) populations (rows) in \(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}, $$ where \(O_{ij}\) is the observed number of counts in the \(i\)th row and \(j\)th column of tab, and \(E_{ij}\) is the expected number of counts under \(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}. $$ where \(N\) is the total number of counts in tab.

Examples

Run this code
# simple contingency table
ctab <- rbind(pop1 = c(5, 3, 0, 3),
                pop2 = c(4, 10, 2, 5))
colnames(ctab) <- LETTERS[1:4]
ctab
chi2.stat(ctab) # chi^2 test statistic

Run the code above in your browser using DataLab