TB sections 5.3
2024-11-13
Lesson | Section | Population parameter | Symbol (pop) | Point estimate | Symbol (sample) | SE |
---|---|---|---|---|---|---|
11 | 5.1 | Pop mean | \(\mu\) | Sample mean | \(\overline{x}\) | \(\dfrac{s}{\sqrt{n}}\) |
12 | 5.2 | Pop mean of paired diff | \(\mu_d\) or \(\delta\) | Sample mean of paired diff | \(\overline{x}_{d}\) | \(\dfrac{s_d}{\sqrt{n}}\) |
13 | 5.3 | Diff in pop means | \(\mu_1-\mu_2\) | Diff in sample means | \(\overline{x}_1 - \overline{x}_2\) | ???? |
15 | 8.1 | Pop proportion | \(p\) | Sample prop | \(\widehat{p}\) | |
15 | 8.2 | Diff in pop prop’s | \(p_1-p_2\) | Diff in sample prop’s | \(\widehat{p}_1-\widehat{p}_2\) |
Examples
Population parameter
Sample statistic (point estimate)
Study Design:1
CaffeineTaps.csv
data
that is in your R project folder (your working directory)Group | variable | n | mean | sd |
---|---|---|---|---|
Caffeine | Taps | 35 | 248.114 | 2.621 |
NoCaffeine | Taps | 35 | 244.514 | 2.318 |
Then calculate the difference between the means:
Single-sample mean:
Paired mean difference:
Diff in means of 2 ind samples:
If \(\overline{X}_1\) and \(\overline{X}_2\) are independent normal RVs, then \(\overline{X}_1 - \overline{X}_2\) is also normal
What is the mean of \(\overline{X}_1 - \overline{X}_2\)? \[E[\overline{X}_1 - \overline{X}_2] = E[\overline{X}_1] - E[\overline{X}_2] = \mu_1-\mu_2\]
What is the standard deviation of \(\overline{X}_1 - \overline{X}_2\)?
\[\begin{align} Var(\overline{X}_1 - \overline{X}_2) &= Var(\overline{X}_1) + Var(\overline{X}_2) = \frac{\sigma_1^2}{n_1}+\frac{\sigma_2^2}{n_2} \\ SD(\overline{X}_1 - \overline{X}_2) &= \sqrt{\frac{\sigma_1^2}{n_1}+\frac{\sigma_2^2}{n_2}} \end{align}\]
Gp 1 | Gp 2 | |
---|---|---|
sample size | \(n_1\) | \(n_2\) |
pop mean | \(\mu_1\) | \(\mu_2\) |
pop sd | \(\sigma_1\) | \(\sigma_2\) |
\[\overline{X}_1 - \overline{X}_2 \sim \]
Single-sample mean:
Paired mean difference:
Diff in means of 2 ind samples:
Calculate CI for the mean difference \(\mu_1 - \mu_2\):
\[\overline{x}_1 - \overline{x}_2 \pm\ t^*\times \sqrt{\frac{s_{1}^2}{n_{1}}+\frac{s_{2}^2}{n_2}}\]
Run a hypothesis test:
Hypotheses
\[\begin{align} H_0:& \mu_1 = \mu_2\\ H_A:& \mu_1 \neq \mu_2\\ (or&~ <, >) \end{align}\]
Test statistic
\[ t_{\overline{x}_1 - \overline{x}_2} = \frac{\overline{x}_1 - \overline{x}_2 - 0}{\sqrt{\frac{s_{1}^2}{n_{1}}+\frac{s_{2}^2}{n_2}}} \]
Confidence interval for \(\mu_1 - \mu_2\)
\[\overline{x}_1 - \overline{x}_2 \pm\ t^*\times \text{SE}\]
CaffTaps %>% group_by(Group) %>% get_summary_stats(type = "mean_sd") %>%
gt() %>% tab_options(table.font.size = 40)
Group | variable | n | mean | sd |
---|---|---|---|---|
Caffeine | Taps | 35 | 248.114 | 2.621 |
NoCaffeine | Taps | 35 | 244.514 | 2.318 |
95% CI for \(\mu_{caff} - \mu_{ctrl}\):
\[ \begin{aligned} \overline{x}_{\text{caff}} - \overline{x}_{\text{ctrl}} & \pm t^* \cdot \sqrt{\frac{s_{\text{caff}}^2}{n_{\text{caff}}}+\frac{s_{\text{ctrl}}^2}{n_{\text{ctrl}}}} \\ 248.114 - 244.514 & \pm 2.032 \cdot \sqrt{\frac{2.621^2}{35}+\frac{2.318^2}{35}} \\ 3.6 & \pm 2.032 \cdot \sqrt{0.196 + 0.154} \\ (2.398, & 4.802) \end{aligned} \]
Used \(t^*\) = qt(0.975, df=34)
= 2.032
Conclusion:
We are 95% confident that the difference in (population) mean finger taps/min between the caffeine and control groups is between 2.398 mg/dL and 4.802 mg/dL.
Welch Two Sample t-test
data: Taps by Group
t = 6.0867, df = 67.002, p-value = 6.266e-08
alternative hypothesis: true difference in means between group Caffeine and group NoCaffeine is not equal to 0
95 percent confidence interval:
2.41945 4.78055
sample estimates:
mean in group Caffeine mean in group NoCaffeine
248.1143 244.5143
estimate | estimate1 | estimate2 | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
---|---|---|---|---|---|---|---|---|---|
3.6 | 248.1143 | 244.5143 | 6.086677 | 6.265631e-08 | 67.00222 | 2.41945 | 4.78055 | Welch Two Sample t-test | two.sided |
Conclusion:
We are 95% confident that the difference in (population) mean finger taps/min between the caffeine and control groups is between 2.398 mg/dL and 4.802 mg/dL.
Check the assumptions
Set the level of significance \(\alpha\)
Specify the null ( \(H_0\) ) and alternative ( \(H_A\) ) hypotheses
Calculate the test statistic.
Calculate the p-value based on the observed test statistic and its sampling distribution
Write a conclusion to the hypothesis test
The assumptions to run a hypothesis test on a sample are:
In our example, we would check the assumptions with a statement:
Before doing a hypothesis test, we set a cut-off for how small the \(p\)-value should be in order to reject \(H_0\).
Typically choose \(\alpha = 0.05\)
Notation for hypotheses (for two ind samples)
Hypotheses test for example
\(H_A: \mu_1 \neq \mu_2\)
\(H_A: \mu_1 < \mu_2\)
\(H_A: \mu_1 > \mu_2\)
\(H_A: \mu_1 \neq \mu_2\)
\(H_A: \mu_1 > \mu_2\)
\(H_A: \mu_1 < \mu_2\)
\(H_A: \mu_1 - \mu_2 \neq 0\)
\(H_A: \mu_1 - \mu_2 > 0\)
\(H_A: \mu_1 - \mu_2 < 0\)
Null and alternative hypotheses in words
\(H_0\): The population difference in mean finger taps/min between the caffeine and control groups is 0
\(H_A\): The population difference in mean finger taps/min between the caffeine and control groups is greater than 0
Null and alternative hypotheses in symbols
\[\begin{align} H_0:& \mu_{caff} - \mu_{ctrl} = 0\\ H_A:& \mu_{caff} - \mu_{ctrl} > 0 \\ \end{align}\]
Recall, for a two sample independent means test, we have the following test statistic:
\[ t_{\overline{x}_1 - \overline{x}_2} = \frac{\overline{x}_1 - \overline{x}_2 - 0}{\sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}} \]
From our example: Recall that \(\overline{x}_1 = 248.114\), \(s_1=2.621\), \(n_1 = 35\), \(\overline{x}_2 = 244.514\), \(s_2=2.318\), and \(n_2 = 35\):
The test statistic is:
\[ \text{test statistic} = t_{\overline{x}_1 - \overline{x}_2} = \frac{\overline{x}_1 - \overline{x}_2 - 0}{\sqrt{\frac{s_1^2}{n_1} + \frac{s_2^2}{n_2}}} = \frac{248.114 - 244.514 - 0}{\sqrt{\frac{2.621^2}{35}+\frac{2.318^2}{35}}} = 6.0869 \]
The p-value is the probability of obtaining a test statistic just as extreme or more extreme than the observed test statistic assuming the null hypothesis \(H_0\) is true.
t.test()
Welch Two Sample t-test
data: Taps by Group
t = 6.0867, df = 67.002, p-value = 3.133e-08
alternative hypothesis: true difference in means between group Caffeine and group NoCaffeine is greater than 0
95 percent confidence interval:
2.613502 Inf
sample estimates:
mean in group Caffeine mean in group NoCaffeine
248.1143 244.5143
If \(\text{p-value} < \alpha\), reject the null hypothesis
If \(\text{p-value} \geq \alpha\), fail to reject the null hypothesis
\[\begin{align} H_0:& \mu_{caff} - \mu_{ctrl} = 0\\ H_A:& \mu_{caff} - \mu_{ctrl} > 0\\ \end{align}\]
Conclusion statement:
CaffTaps
data are in a long format, meaning that
Welch Two Sample t-test
data: Taps by Group
t = 6.0867, df = 67.002, p-value = 3.133e-08
alternative hypothesis: true difference in means between group Caffeine and group NoCaffeine is greater than 0
95 percent confidence interval:
2.613502 Inf
sample estimates:
mean in group Caffeine mean in group NoCaffeine
248.1143 244.5143
tidy
the t.test
output# use tidy command from broom package for briefer output that's a tibble
tidy(Taps_2ttest) %>% gt() %>% tab_options(table.font.size = 40)
estimate | estimate1 | estimate2 | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
---|---|---|---|---|---|---|---|---|---|
3.6 | 248.1143 | 244.5143 | 6.086677 | 3.132816e-08 | 67.00222 | 2.613502 | Inf | Welch Two Sample t-test | greater |
# make CaffTaps data wide: pivot_wider needs an ID column so that it
# knows how to "match" values from the Caffeine and NoCaffeine groups
CaffTaps_wide <- CaffTaps %>%
mutate(id = c(rep(1:10, 2), rep(11:35, 2))) %>% # "fake" IDs for pivot_wider step
pivot_wider(names_from = "Group",
values_from = "Taps")
glimpse(CaffTaps_wide)
Rows: 35
Columns: 3
$ id <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, …
$ Caffeine <int> 246, 248, 250, 252, 248, 250, 246, 248, 245, 250, 251, 251,…
$ NoCaffeine <int> 242, 245, 244, 248, 247, 248, 242, 244, 246, 242, 244, 245,…
t.test(x = CaffTaps_wide$Caffeine, y = CaffTaps_wide$NoCaffeine, alternative = "greater") %>%
tidy() %>% gt() %>% tab_options(table.font.size = 40)
estimate | estimate1 | estimate2 | statistic | p.value | parameter | conf.low | conf.high | method | alternative |
---|---|---|---|---|---|---|---|---|---|
3.6 | 248.1143 | 244.5143 | 6.086677 | 3.132816e-08 | 67.00222 | 2.613502 | Inf | Welch Two Sample t-test | greater |
From many slides ago:
The actual degrees of freedom are calculated using Satterthwaite’s method:
\[\nu = \frac{[ (s_1^2/n_1) + (s_2^2/n_2) ]^2} {(s_1^2/n_1)^2/(n_1 - 1) + (s_2^2/n_2)^2/(n_2-1) } = \frac{ [ SE_1^2 + SE_2^2 ]^2}{ SE_1^4/df_1 + SE_2^4/df_2 }\]
Verify the p-value in the R output using \(\nu\) = 17.89012:
Lesson 13 Slides