In the last module, we reviewed statistical inference for one sample.
In this module, we will focus on tests for two samples.
In this lecture: two-sample independent means (\mu_1-\mu_2)
R Setup| resident_name | department | district | academy_status | academy_exam_score | sleep_hours | hustle_earnings | district_temp_f |
|---|---|---|---|---|---|---|---|
| Sable Jones | Desk | Rainforest District | Fail | 65 | 5.5 | 14.72 | 72.2 |
| Kevin Frostwhisker | Patrol | Downtown | Fail | 69 | 7.4 | 45.66 | 66.2 |
| Jasper Duskrunner | Patrol | Sahara Square | Pass | 79 | 8.7 | 30.91 | 89.8 |
(\bar{y}_1 - \bar{y}_2) \pm t_{\alpha/2} \sqrt{\frac{s_1^2 }{n_1} + \frac{s_2^2}{n_2}}
Hypotheses
Two-Tailed
H_0: \mu_1-\mu_2 = \mu_0
H_1: \mu_1 - \mu_2 \ne \mu_0
Left-Tailed
H_0: \mu_1-\mu_2 \ge \mu_0
H_1: \mu_1-\mu_2 < \mu_0
Right-Tailed
H_0: \mu_1-\mu_2 \le \mu_0
H_1: \mu_1-\mu_2 > \mu_0
\mu_i is the population mean for group i,
\mu_0 is the hypothesized difference between the groups
Test Statistic:
t_0 = \frac{(\bar{x}_1-\bar{x}_2)-\mu_0}{{\sqrt{\frac{s_1^2}{n_1} + \frac{s^2_2}{n_2}}}} \sim t_{\text{df}}
R)We will use the independent_means function from library(ssstats) to perform statistical inference.
Generic syntax:
dataset_name |> independent_means(
outcome = outcome_variable,
grouping = grouping_variable,
alternative = "two" | "less" | "greater",
mu = hypothesized_difference,
alpha = alpha_level
) independent_means().
outcome: the continuous variable you want to compare between the two groups.
grouping: the categorical variable that splits your data into the two groups being compared.
alternative: the direction of the test; default is two.
mu: the hypothesized difference between the two groups under H_0; default is 0.
alpha: the significance level; default is 0.05.
Okonkwo pings you on your first morning back from vacation: “Patrol runs early shifts while Desk runs later ones and I keep hearing officers gripe about who’s more rested. Tell me if there’s actually a difference in how much sleep our two groups are getting.”
We will use the zootopia dataset to answer the following question:
First, we need to describe the data.
Then, we will perform a hypothesis test to answer the question.
Independent means (Welch's t-test)
x̅[Desk] = 6.2667
x̅[Patrol] = 7.1444
Point estimate: x̅[Desk] − x̅[Patrol] = -0.8777
95% CI for μ[Desk] − μ[Patrol]: (-1.9269, 0.1713)
Hypotheses:
H₀: μ[Desk] − μ[Patrol] = 0
H₁: μ[Desk] − μ[Patrol] ≠ 0
Test statistic: t(21.52) = -1.737
p-value: 0.097
Conclusion: Fail to reject the null hypothesis (p = 0.097 ≥ α = 0.050)
Then, we will perform a hypothesis test to answer the question.
How do we translate this into hypotheses?
For the independent_means() function, we will set
mu = 0alternative = "two".zootopia |> independent_means(outcome = sleep_hours,
grouping = department,
mu = 0,
alternative = "two")Independent means (Welch's t-test)
x̅[Desk] = 6.2667
x̅[Patrol] = 7.1444
Point estimate: x̅[Desk] − x̅[Patrol] = -0.8777
95% CI for μ[Desk] − μ[Patrol]: (-1.9269, 0.1713)
Hypotheses:
H₀: μ[Desk] − μ[Patrol] = 0
H₁: μ[Desk] − μ[Patrol] ≠ 0
Test statistic: t(21.52) = -1.737
p-value: 0.097
Conclusion: Fail to reject the null hypothesis (p = 0.097 ≥ α = 0.050)
Hypotheses
Test Statistic and p-Value
Rejection Region
Conclusion and Interpretation
Okonkwo’s back with another one: “Word around the precinct is that Patrol officers have more access to the public, so it’s naturally easier to run their side hustles. Let’s see what the data says – do Patrol officers earn more than Desk officers?”
We will use the zootopia dataset to answer the following question:
First, we need to describe the data.
Then, we will perform a hypothesis test to answer the question.
Independent means (Welch's t-test)
x̅[Desk] = 31.6483
x̅[Patrol] = 40.0883
Point estimate: x̅[Desk] − x̅[Patrol] = -8.44
95% CI for μ[Desk] − μ[Patrol]: (-27.8211, 10.9411)
Hypotheses:
H₀: μ[Desk] − μ[Patrol] = 0
H₁: μ[Desk] − μ[Patrol] ≠ 0
Test statistic: t(17.45) = -0.917
p-value: 0.372
Conclusion: Fail to reject the null hypothesis (p = 0.372 ≥ α = 0.050)
Then, we will perform a hypothesis test to answer the question.
How do we translate this into hypotheses?
For the independent_means() function, we will set
mu = 0alternative = "greater".zootopia |> independent_means(outcome = hustle_earnings,
grouping = department,
mu = 0,
alternative = "less")Independent means (Welch's t-test)
x̅[Desk] = 31.6483
x̅[Patrol] = 40.0883
Point estimate: x̅[Desk] − x̅[Patrol] = -8.44
95% CI for μ[Desk] − μ[Patrol]: (-27.8211, 10.9411)
Hypotheses:
H₀: μ[Desk] − μ[Patrol] ≥ 0
H₁: μ[Desk] − μ[Patrol] < 0
Test statistic: t(17.45) = -0.917
p-value: 0.186
Conclusion: Fail to reject the null hypothesis (p = 0.186 ≥ α = 0.050)
Hypotheses
Test Statistic and p-Value
Rejection Region
Conclusion and Interpretation
y_{11}, y_{12}, \ldots, y_{1n_1} \sim N(\mu_1, \sigma_1^2) y_{21}, y_{22}, \ldots, y_{2n_2} \sim N(\mu_2, \sigma_2^2)
We will use a quantile-quantile plot (Q-Q plot) to check the assumption of normality.
R)independent_qq() function from library(ssstats) to assess normality.y_{11}, y_{12}, \ldots, y_{1n_1} \sim N(\mu_1, \sigma_1^2) y_{21}, y_{22}, \ldots, y_{2n_2} \sim N(\mu_2, \sigma_2^2)
Note that we are not assuming \sigma_1^2 = \sigma_2^2.
independent_means() function assumes \sigma_1^2 \ne \sigma_2^2.The confidence interval is built on every possible pairwise difference between the two groups.
We examine the n_1 \times n_2 differences between the observations.
The median of that pool of differences is the Hodges–Lehmann estimate.
The CI is then found by
ranking that entire pool of n_1 \times n_2 differences from smallest to largest,
then we pick two values from that ranked list as the lower and upper bounds.
Hypotheses
Two-Tailed
H_0: M_1 - M_2 = M_0
H_1: M_1 - M_2 \ne M_0
Left-Tailed
H_0: M_1 - M_2 \ge M_0
H_1: M_1 - M_2 < M_0
Right-Tailed
H_0: M_1 - M_2 \le M_0
H_1: M_1 - M_2 > M_0
M_i is the population median for group i, and
M_0 is the hypothesized difference between the groups
Test Statistic
T_0 = \sum R_{\text{1}} - \frac{n_1(n_1+1)}{2}
R)We will use the independent_medians function from library(ssstats) to perform statistical inference.
Generic syntax:
dataset_name |> independent_medians(
outcome = outcome_variable,
grouping = grouping_variable,
alternative = "two" | "less" | "greater",
m = hypothesized_difference,
alpha = alpha_level
) independent_medians().
outcome: the continuous variable you want to compare between the two groups.
grouping: the categorical variable that splits your data into the two groups being compared.
alternative: the direction of the test; default is two.
m: the hypothesized difference between the two groups under H_0; default is 0.
alpha: the significance level; default is 0.05.
Then, we will perform a hypothesis test to answer the question.
How do we translate this into hypotheses?
For the independent_medians() function, we will set
m = 0alternative = "two".Independent medians
M[Desk] = 6.45
M[Patrol] = 7.35
Hodges-Lehmann estimate of median difference: -0.7
95% CI for M[Desk] − M[Patrol]: (-1.7, 0.1)
Hypotheses:
H₀: M[Desk] − M[Patrol] = 0
H₁: M[Desk] − M[Patrol] ≠ 0
Test statistic: W = 68
p-value: 0.092
Conclusion: Fail to reject the null hypothesis (p = 0.092 ≥ α = 0.050)
Hypotheses
Test Statistic and p-Value
Rejection Region
Conclusion and Interpretation
Then, we will perform a hypothesis test to answer the question.
How do we translate this into hypotheses?
For the independent_means() function, we will set
mu = 0alternative = "greater".zootopia |> independent_medians(outcome = hustle_earnings,
grouping = department, alternative = "less")Independent medians
M[Desk] = 22.265
M[Patrol] = 34.17
Hodges-Lehmann estimate of median difference: -11.695
95% CI for M[Desk] − M[Patrol]: (-23.54, 0.7)
Hypotheses:
H₀: M[Desk] − M[Patrol] ≥ 0
H₁: M[Desk] − M[Patrol] < 0
Test statistic: W = 63
p-value: 0.029
Conclusion: Reject the null hypothesis (p = 0.029 < α = 0.050)
Hypotheses
Test Statistic and p-Value
Rejection Region
Conclusion and Interpretation
Using a t-test to examine the difference between two independent means (\mu_1-\mu_2) requires normality.
independent_qq()).The analysis you choose should match the needed method:
If we meet normality (independent_means()):
If we do not meet normality (independent_medians()):