On Monday, we discussed:
Test for \pi
Test for \pi_1-\pi_2
Now we will expand to:
Goodness-of-fit
Test for independence
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
Hypotheses
Test Statistic
\chi^2_0 = \sum_{i=1}^k \frac{(O_i-E_i)^2}{E_i}
We will use the goodness_of_fit() function from library(ssstats) to find the confidence interval.
Generic syntax (assuming uniform distribution):
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.
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)
| Product | Proportion |
|---|---|
| Dresses | 0.40 |
| Capes | 0.25 |
| Hats | 0.15 |
| Scarves | 0.10 |
| Hoof Accessories | 0.10 |
| 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.
We will use the goodness_of_fit() function from library(ssstats) to find the confidence interval.
Generic syntax (not assuming uniform distribution):
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)
n_pct(row_var, col_var).E_{ij} = \frac{R_i C_j}{n}
\chi_0^2 = \sum_{i=1}^k \frac{(O_i-E_i)^2}{E_i}
We will use the goodness_of_fit() function from library(ssstats) to find the confidence interval.
Generic syntax:
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.
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.
This concludes our categorical analysis journey.
We often use these methods to analyze survey data.
Next week:
Monday: lab
Wednesday: test
Next module: regression for continuous outcomes.