Source: Hedderich, Sachs and Reynarowych, Applied Statistics: Methods Using R,
section 8.5, printed page 851, equations (8.95) and (8.96): chi2 = sum (y_ij -
n p_ij)^2 / (n p_ij) and G2 = 2 sum y_ij log(y_ij / (n p_ij)), with sum
y_ij = n and p_ij the cell probabilities estimated under the model. The book calls
(8.95) the Pearson-residual statistic and (8.96) the likelihood-quotient statistic,
notes that G2 is usually preferred because its minimum comes from the
maximum-likelihood estimate, and states that both are asymptotically chi-squared.
Details
The default model is independence (8.97),
log n pi_ij = mu + lam_i^X + lam_j^Y, whose fitted counts are
the row-times-column product over the grand total and whose degrees
of freedom are (k1 - 1)(k2 - 1). Fitted counts for any other
model may be passed in expected with the matching df.
0 log 0 is taken as 0.
