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}}
one_proportion() function from library(ssstats) to perform statistical inference.where
outcome is the variable name of the outcome variable
event is the name or number of the event of interest
p is the hypothesized value of the population proportion (default = 0.05)
alternative is the alternative hypothesis (default = “two”)
alpha is the significance level (default = 0.05)
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 prefer chocolate cake.
z_0 = \frac{\hat{p}-p_0}{\sqrt{\frac{p_0(1-p_0)}{n}}}
where
We will use the one_proportion() function from library(ssstats) to perform the necessary calculations for the hypothesis test.
Generic syntax:
Pinkie Pie is curious if party goers 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.
Pinkie Pie is curious if party goers 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_proportion(outcome = preference,
event = "Chocolate",
p = 0.5,
alternative = "greater",
alpha = 0.05)One-sample proportion
p̂ = 0.7067 (53/75)
95% CI for π: (0.6036, 0.8097)
Hypotheses:
H₀: π ≤ 0.5
H₁: π > 0.5
Test statistic: z = 3.58
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.050)
(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}}
where
We will use the two_proportions() function from library(ssstats) to find the confidence interval.
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 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,
candy_data |> independent_proportions(outcome = shared_candy,
grouping = age_group,
event = "Yes",
alpha = 0.10)Independent proportions
p̂[Older] = 0.8167
p̂[Young] = 0.4167
Point estimate: p̂[Older] − p̂[Young] = 0.4
90% CI for π[Older] − π[Young]: (0.2669, 0.5331)
Hypotheses:
H₀: π[Older] − π[Young] = 0
H₁: π[Older] − π[Young] ≠ 0
Test statistic: z = 4.51
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.100)
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 independent_proportions() 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 |> independent_proportions(outcome = shared_candy,
grouping = age_group,
event = "Yes",
p = 0,
alternative = "greater",
alpha = 0.1)Independent proportions
p̂[Older] = 0.8167
p̂[Young] = 0.4167
Point estimate: p̂[Older] − p̂[Young] = 0.4
90% CI for π[Older] − π[Young]: (0.2669, 0.5331)
Hypotheses:
H₀: π[Older] − π[Young] ≤ 0
H₁: π[Older] − π[Young] > 0
Test statistic: z = 4.51
p-value: < 0.001
Conclusion: Reject the null hypothesis (p = < 0.001 < α = 0.100)
Today we have reviewed the most common analyses used in categorical data.
Not covered this semester: logistic regression