Goodness of Fit and Contingency Tables

Introduction

  • On Monday, we discussed:

    • Test for \pi

    • Test for \pi_1-\pi_2

  • Now we will expand to:

    • Goodness-of-fit

    • Test for independence

Hypothesis Testing: Goodness-of-Fit

  • The goodness-of-fit test allows us to determine if a frequency distribution follows a specific distribution.

    • This could be a named distribution (e.g., normal)

    • It could also be a distribution without a name (e.g., the probabilities are specified)

  • Before we can perform the goodness-of-fit test, we must compute expected counts.E_i = n p_i

    • e.g., suppose that we expect 25% of Skittles to be red; if we have 100 Skittles, we then expect 25 of them to be red.

Hypothesis Testing: Goodness-of-Fit

  • Hypotheses

    • H_0: The random variable follows the specified distribution.
    • H_1: The random variable does not follow the specified distribution.
  • Test Statistic

\chi^2_0 = \sum_{i=1}^k \frac{(O_i-E_i)^2}{E_i}

  • where k = number of categories, O_i = observed count and E_i = expected count

Hypothesis Testing: Goodness-of-Fit (R)

  • We will use the goodness_of_fit() function from library(ssstats) to find the confidence interval.

  • Generic syntax (assuming uniform distribution):

dataset_name |> goodness_of_fit(outcome = categorical_variable,
                                alpha = specified_alpha)

Hypothesis Testing: Goodness-of-Fit

  • At the annual Ponyville Harvest Festival, Applejack runs a game called “Barrel Toss,” where players toss apples into one of five barrels lined up across the field. Applejack believes the barrels are evenly spaced and equally likely to receive apples if the game is fair.

  • However, after watching several rounds, she suspects something might be off… maybe the barrels aren’t truly equal. She records (apple_toss) the number of apples that landed in each barrel during the day (barrel) and wants to test whether they are uniformly distributed, as expected under fairness.

  • Let’s first look at the frequency table.

Hypothesis Testing: Goodness-of-Fit

  • Let’s first look at the frequency table.
apple_toss |> n_pct(barrel)
barrel n (pct)
Barrel 1 31 (15.5%)
Barrel 2 29 (14.5%)
Barrel 3 61 (30.5%)
Barrel 4 44 (22.0%)
Barrel 5 35 (17.5%)

Hypothesis Testing: Goodness-of-Fit

  • Running the code,
apple_toss |> goodness_of_fit(outcome = barrel)
Chi-square goodness-of-fit test

Hypotheses:
H₀: Observed frequencies match expected proportions
H₁: Observed frequencies do not match expected proportions
Test statistic: χ²(4) = 17.1
p-value: 0.002
Conclusion: Reject the null hypothesis (p = 0.002 < α = 0.050)

Hypothesis Testing: Goodness-of-Fit

  • Hypotheses
    • H_0: \ \pi_1 = \pi_2 = \pi_3 = \pi_4 = \pi_5 = 0.20.
    • H_1: At least one \pi_i is different.
  • Test Statistic and p-Value
    • \chi^2_0 = 17.1; p = 0.002
  • Rejection Region
    • Reject H_0 if p < \alpha; \alpha=0.05
  • Conclusion/Interpretation
    • Reject H_0. There is sufficient evidence to suggest that at least one proportion is different.

Hypothesis Testing: Goodness-of-Fit

  • Rarity tracks customer orders from her boutique (rarity_orders) across five product categories. She knows that sales typically follow this distribution:
Product Proportion
Dresses 0.40
Capes 0.25
Hats 0.15
Scarves 0.10
Hoof Accessories 0.10
  • This season, something feels off. Rarity reviews 200 orders from this season and wants to determine if the sales (item_ordered) are deviating from the historic pattern.

Hypothesis Testing: Goodness-of-Fit

  • Let’s first look at a frequency table.
rarity_orders |> n_pct(item_ordered)
item_ordered n (pct)
Cape 45 (22.5%)
Dress 77 (38.5%)
Hat 36 (18.0%)
Hoof Accessories 17 (8.5%)
Scarf 25 (12.5%)

Hypothesis Testing: Goodness-of-Fit

  • Rarity tracks customer orders from her boutique (rarity_orders) across five product categories. She knows that sales typically follow this distribution:
Product Proportion
Dresses 0.40
Capes 0.25
Hats 0.15
Scarves 0.10
Hoof Accessories 0.10
  • This season, something feels off. Rarity reviews 200 orders from this season and wants to determine if the sales (item_ordered) are deviating from the historic pattern.

  • Let’s now consider the hypothesis test. Except… this is not a uniform distribution.

Hypothesis Testing: Goodness-of-Fit (R)

  • We will use the goodness_of_fit() function from library(ssstats) to find the confidence interval.

  • Generic syntax (not assuming uniform distribution):

dataset_name |> goodness_of_fit(categorical = categorical_variable,
                                expected = c("Category 1" = proportion_1,
                                             "Category 2" = proportion_2,
                                             ... etc ...),
                                alpha = alpha_level)

Hypothesis Testing: Goodness-of-Fit

  • Rarity reviews 200 orders from this season and wants to determine if the sales are deviating from the historic pattern.
rarity_orders |> goodness_of_fit(outcome = item_ordered,
                                 expected = c("Dress" = 0.40,
                                              "Cape" = 0.25,
                                              "Hat" = 0.15,
                                              "Scarf" = 0.10,
                                              "Hoof Accessories" = 0.10))
Chi-square goodness-of-fit test

Hypotheses:
H₀: Observed frequencies match expected proportions
H₁: Observed frequencies do not match expected proportions
Test statistic: χ²(4) = 3.51
p-value: 0.476
Conclusion: Fail to reject the null hypothesis (p = 0.476 ≥ α = 0.050)

Hypothesis Testing: Goodness-of-Fit

  • Hypotheses
    • H_0: This year’s sales follow the historical pattern.
    • H_1: This year’s sales do not follow the historical pattern.
  • Test Statistic and p-Value
    • \chi^2_0 = 3.51; p = 0.476
  • Rejection Region
    • Reject H_0 if p < \alpha; \alpha=0.05
  • Conclusion/Interpretation
    • Fail to reject H_0. There is not sufficient evidence to suggest that this year’s sales do not follow the historical pattern.

Hypothesis Testing: Test for Independence

  • Let us now discuss testing two categorical variables to determine if a relationship exists.
    • This involves using a contingency table.
    • We can generate them using n_pct(row_var, col_var).
  • Like in the goodness-of-fit test, we will first compute expected values,

E_{ij} = \frac{R_i C_j}{n}

  • where
    • R_i is the total for row i,
    • C_j is the total for column j, and
    • n is the total sample size

Hypothesis Testing: Test for Independence

  • Hypotheses
    • H_0: There is not a relationship between [var 1] and [var 2].
    • H_1: There is a relationship between [var 1] and [var 2].
  • Test Statistic

\chi_0^2 = \sum_{i=1}^k \frac{(O_i-E_i)^2}{E_i}

Hypothesis Testing: Test for Independence (R)

  • We will use the goodness_of_fit() function from library(ssstats) to find the confidence interval.

  • Generic syntax:

dataset_name |> independence(col1 = first_variable,
                             col2 = second_variable,
                             alpha = specified_alpha)

Hypothesis Testing: Test for Independence

  • Twilight Sparkle notices that her public library in Ponyville is visited by a variety of ponies: students, teachers, and general community members. She’s curious whether the type of reading material ponies check out depends on their role in the community.

  • She records each visitor’s role (role; student, teacher, and community member) and the type of material they check out (material; spells, history, fiction, and science).

  • Twilight wants to test whether the type of material checked out is independent of visitor role, or if certain roles are more likely to favor particular genres.

Hypothesis Testing: Test for Independence

  • Looking at the contingency tables,
twilight_library |> n_pct(role, material)
role Fiction History Science Spells
Community 34 (72.3%) 11 (23.9%) 9 (30.0%) 6 (10.5%)
Student 9 (19.1%) 10 (21.7%) 13 (43.3%) 28 (49.1%)
Teacher 4 (8.5%) 25 (54.3%) 8 (26.7%) 23 (40.4%)

Hypothesis Testing: Test for Independence

  • Looking at the contingency tables,
twilight_library |> n_pct(material, role)
material Community Student Teacher
Fiction 34 (56.7%) 9 (15.0%) 4 (6.7%)
History 11 (18.3%) 10 (16.7%) 25 (41.7%)
Science 9 (15.0%) 13 (21.7%) 8 (13.3%)
Spells 6 (10.0%) 28 (46.7%) 23 (38.3%)

Hypothesis Testing: Test for Independence

  • Twilight Sparkle notices that her public library in Ponyville is visited by a variety of ponies: students, teachers, and general community members. She’s curious whether the type of reading material ponies check out depends on their role in the community.

  • She records each visitor’s role (role; student, teacher, and community member) and the type of material they check out (material; spells, history, fiction, and science).

  • Twilight wants to test whether the type of material checked out is independent of visitor role, or if certain roles are more likely to favor particular genres. Assume \alpha = 0.05.

Hypothesis Testing: Test for Independence

  • Running the code,
twilight_library |> independence(col1 = role,
                                 col2 = material)
Chi-square test of independence

Hypotheses:
H₀: role and material are independent
H₁: role and material are not independent
Test statistic: χ²(6) = 57.55
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.050)

Hypothesis Testing: Test for Independence

  • Running the code & switching the variable order,
twilight_library |> independence(col1 = material,
                                 col2 = role)
Chi-square test of independence

Hypotheses:
H₀: material and role are independent
H₁: material and role are not independent
Test statistic: χ²(6) = 57.55
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.050)

Hypothesis Testing: Test for Independence

  • Hypotheses
    • H_0: Material and role are independent of one another.
    • H_1: Material and role depend on one another.
  • Test Statistic and p-Value
    • \chi^2_0 = 57.55; p < 0.001
  • Rejection Region
    • Reject H_0 if p < \alpha; \alpha=0.05
  • Conclusion/Interpretation
    • Reject H_0. There is sufficient evidence to suggest that the what type of material is checked out depends on the role of the visitor.

Wrap Up

  • This concludes our categorical analysis journey.

    • There are more advanced methods (e.g., logistic regression analysis) available.
  • We often use these methods to analyze survey data.

  • Next week:

    • Monday: lab

    • Wednesday: test

  • Next module: regression for continuous outcomes.