July 29, 2025
Tuesday
Before today, we have focused on continuous outcomes.
Now we will focus on categorical (or qualitative) outcomes.
We will estimate a proportion using \hat{p},
\hat{p} = \frac{x}{n}
\hat{p_1}- \hat{p_2} = \frac{x_1}{n_1} - \frac{x_2}{n_2}
(1-\alpha)100\% confidence interval for \pi:
\hat{p} \pm z_{\alpha/2} \sqrt{\frac{\hat{p}(1-\hat{p})}{n}}
We will use the one_prop_CI
function from library(ssstats)
to find the confidence interval.
Generic syntax:
n_pct()
function to create a frequency table.Pinkie Pie is curious if partygoers prefer chocolate cake over vanilla cake. At her latest party, she surveys 75 ponies (party_data). Each pony is asked which flavor they prefer (preference).
She ultimately wants to determine if more than 50% prefer chocolate cake.
Looking at the frequency table,
Pinkie Pie is curious if partygoers prefer chocolate cake over vanilla cake. At her latest party, she surveys 75 ponies (party_data). Each pony is asked which flavor they prefer (preference).
She ultimately wants to determine if more than 50% prefer chocolate cake.
Let’s now find the 95% confidence interval for \pi, the population proportion of ponies that prefere chocolate cake. How should we edit this code?
Pinkie Pie is curious if partygoers prefer chocolate cake over vanilla cake. At her latest party, she surveys 75 ponies (party_data). Each pony is asked which flavor they prefer (preference).
She ultimately wants to determine if more than 50% prefer chocolate cake.
Let’s now find the 95% confidence interval for \pi, the population proportion of ponies that prefere chocolate cake. How should we edit this code?
z_0 = \frac{\hat{p}-p_0}{\sqrt{\frac{p_0(1-p_0)}{n}}}
p = 2\times P\left[z \ge |z_0|\right]
p = P\left[z \le z_0\right]
p = P\left[z \ge z_0\right]
We will use the one_prop_HT
function from library(ssstats)
to perform the necessary calculations for the hypothesis test.
Generic syntax:
Pinkie Pie is curious if partygoers prefer chocolate cake over vanilla cake. At her latest party, she surveys 75 ponies (party_data). Each pony is asked which flavor they prefer (preference).
She ultimately wants to determine if more than 50% prefer chocolate cake.
Let’s now formally test her hypothesis at the \alpha = 0.05 level. How should we update this code?
Pinkie Pie is curious if partygoers prefer chocolate cake over vanilla cake. At her latest party, she surveys 75 ponies (party_data). Each pony is asked which flavor they prefer (preference).
She ultimately wants to determine if more than 50% prefer chocolate cake.
Let’s now formally test her hypothesis at the \alpha = 0.05 level. Our updated code,
party_data %>% one_prop_HT(binary = preference,
event = "Chocolate",
p = 0.5,
alternative = "greater",
alpha = 0.05)
One-sample z-test for the population proportion:
Null: H0: π = 0.5
Alternative: H1: π > 0.5
Test statistic: z = 3.58
p-value: p < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.05)
(1-\alpha)100\% confidence interval for \pi_1-\pi_2:
(\hat{p}_1 - \hat{p}_2) \pm z_{\alpha/2} \sqrt{\frac{\hat{p}_1 (1-\hat{p}_1)}{n_1} + \frac{\hat{p}_2(1-\hat{p}_2)}{n_2}}
We will use the two_prop_CI
function from library(ssstats)
to find the confidence interval.
Generic syntax:
n_pct()
function to create a frequency table.Fluttershy has a hunch: younger foals might be less likely to share their candy on Nightmare Night compared to older foals. To test this, she observes foals from each age group (age_group) and records whether each one shared their candy (shared_candy). The data is recorded in candy_data.
Let’s first examine a frequency table.
Fluttershy has a hunch: younger foals might be less likely to share their candy on Nightmare Night compared to older foals. To test this, she observes foals from each age group (age_group) and records whether each one shared their candy (shared_candy). The data is recorded in candy_data.
Let’s now find the 90% confidence interval for \pi_1 - \pi_2. How should we update this code?
Fluttershy has a hunch: younger foals might be less likely to share their candy on Nightmare Night compared to older foals. To test this, she observes foals from each age group (age_group) and records whether each one shared their candy (shared_candy). The data is recorded in candy_data.
Let’s now find the 90% confidence interval for \pi_1 - \pi_2. Our updated code,
candy_data %>% two_prop_CI(binary = shared_candy,
grouping = age_group,
event = "Yes",
confidence = 0.90)
z_0 = \frac{\left( \hat{p}_1 - \hat{p}_2 \right)- d_0}{\sqrt{\hat{p}\left(1-\hat{p}\right)\left( \frac{1}{n_1}+\frac{1}{n_2} \right)}}
\hat{p} = \frac{x_1+x_2}{n_1+n_2}
We will use the two_prop_HT
function from library(ssstats)
to perform the necessary calculations for the hypothesis test.
Generic syntax:
Fluttershy has a hunch: younger foals might be less likely to share their candy on Nightmare Night compared to older foals. To test this, she observes foals from each age group (age_group) and records whether each one shared their candy (shared_candy). The data is recorded in candy_data.
Let’s now test Fluttershy’s hunch at the \alpha = 0.10 level. How should we update this code?
Fluttershy has a hunch: younger foals might be less likely to share their candy on Nightmare Night compared to older foals. To test this, she observes foals from each age group (age_group) and records whether each one shared their candy (shared_candy). The data is recorded in candy_data.
Let’s now test Fluttershy’s hunch at the \alpha = 0.10 level. Our updated code,
candy_data %>% two_prop_HT(binary = shared_candy,
grouping = age_group,
event = "Yes",
p = 0,
alternative = "less",
alpha = 0.1)
Two-sample z-test for difference in proportions:
Group 1: Young, Group 2: Older
Observed difference: -0.4
Null: H₀: π₁ - π₂ = 0
Alternative: H₁: π₁ - π₂ < 0
Test statistic: z = -4.94
p-value: p < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.1)
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}
where k = number of categories, O_i = observed count and E_i = expected count
p-Value
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.
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.
How should we update the code?
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.
Our updated code,
Chi-square goodness-of-fit test:
Null: H₀: Observed frequencies match expected proportions
Alternative: H₁: Observed frequencies do not match expected proportions
Test statistic: χ²(4) = 17.1
p-value: p = 0.002
Conclusion: Reject the null hypothesis (p = 0.0018 < α = 0.05)
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 first look at a frequency table.
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 tracks customer orders from her boutique (rarity_orders) across five product categories (item_ordered).
Rarity reviews 200 orders from this season and wants to determine if the sales are deviating from the historic pattern.
How should we update the following code?
Rarity tracks customer orders from her boutique (rarity_orders) across five product categories (item_ordered).
Rarity reviews 200 orders from this season and wants to determine if the sales are deviating from the historic pattern.
Our updated code,
rarity_orders %>% goodness_of_fit(categorical = 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:
Null: H₀: Observed frequencies match expected proportions
Alternative: H₁: Observed frequencies do not match expected proportions
Test statistic: χ²(4) = 3.51
p-value: p = 0.476
Conclusion: Fail to reject the null hypothesis (p = 0.476 ≥ α = 0.05)
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.
Let’s first construct a contingency table,
# A tibble: 3 × 5
role Fiction History Science Spells
<chr> <chr> <chr> <chr> <chr>
1 Community 34 (72.3%) 11 (23.9%) 9 (30.0%) 6 (10.5%)
2 Student 9 (19.1%) 10 (21.7%) 13 (43.3%) 28 (49.1%)
3 Teacher 4 (8.5%) 25 (54.3%) 8 (26.7%) 23 (40.4%)
# A tibble: 3 × 4
material Community Student Teacher
<chr> <chr> <chr> <chr>
1 Fiction 34 (56.7%) 9 (15.0%) 4 (6.7%)
2 History 11 (18.3%) 10 (16.7%) 25 (41.7%)
3 Science 9 (15.0%) 13 (21.7%) 8 (13.3%)
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. How should we edit the following code?
Chi-square test for independence:
Null: H₀: role and material are independent
Alternative: H₁: role and material depend on one another
Test statistic: χ²(6) = 57.55
p-value: p < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.05)
Chi-square test for independence:
Null: H₀: material and role are independent
Alternative: H₁: material and role depend on one another
Test statistic: χ²(6) = 57.55
p-value: p < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.05)
STA4173 - Biostatistics - Summer 2025