Skip to contents

Calculates a z-squared goodness-of-fit statistic comparing observed and expected frequencies.

Usage

calculate_z_squared_score(df)

Arguments

df

A data frame containing:

count_x

Observed frequencies.

count_y

Expected frequencies.

observed_total

Optional observed population total.

expected_total

Optional expected population total.

If totals are not supplied they are calculated from count_x and count_y.

Value

A list containing:

z_square

Overall z-squared statistic.

p_value

Chi-squared p-value.

degrees_of_freedom

Number of degrees of freedom.

critical_value

Chi-squared critical value at the 0.05 significance level.

details

Data frame containing cell-level z scores and probabilities.

Details

The implementation follows the approach described by Voas and Williamson and incorporates the continuity correction proposed by Fleiss (1981).

The resulting z-squared statistic can be interpreted in a similar manner to a chi-squared test statistic and may be used to assess the fit of a synthetic population to a reference distribution.

For each contingency-table cell:

  1. Observed and expected proportions are calculated.

  2. A continuity correction is applied when the expected frequency is non-zero.

  3. Cell-level z scores are calculated.

  4. Squared z scores are summed to create an overall z-squared statistic.

When the expected count equals zero, a small surrogate expected proportion is used in the denominator to avoid division-by-zero problems.

This function is primarily intended for evaluating the fit of synthetic populations generated by replica.

References

Fleiss JL (1981). Statistical Methods for Rates and Proportions. Wiley.

Voas D, Williamson P (2001). "The Diversity of Diversity: A Critique of Geodemographic Classification".

Examples

df <- data.frame(
  count_x = c(
    40,
    35,
    25
  ),
  count_y = c(
    45,
    30,
    25
  )
)

result <- calculate_z_squared_score(
  df
)

result$z_square
#> [1] 1.795801

result$p_value
#> [1] 0.6158491