Skip to contents

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.

Usage

Llgsq(observed, expected = NULL, df = NULL, alpha = 0.05)

Arguments

observed

Contingency table (matrix) of non-negative counts.

expected

Optional fitted cell counts under the model.

df

Degrees of freedom; required when expected is supplied.

alpha

Significance level for the reject decision.

Value

list: statistic, g2, chisq, expected, df, pvalue, pvalue_chisq, reject, n.

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.

Examples

Llgsq(matrix(c(10, 20, 30, 40), 2, 2))$g2
#> [1] 0.8043486