Inference For Categorical Variables II
Both with 2 levels (binary)
At least one of them has more than two levels
Null hypothesis:
\[H_0: \text{there is no association between variables}\]
\[H_0: p_1 - p_2 = 0\]
Two options to get the null distribution:
Simulation
Central Limit Theorem
Confidence interval for difference of two proportions.
Two options to get the bootstrap distribution:
Resampling
Central Limit Theorem
Suppose we are selecting samples of size \(n_1\) from a population in which the proportion of success is \(p_1\), and of size \(n_2\) from a population in which the proportion of success is \(p_2\).
Suppose that \(n_1p_1 \ge 10\) and \(n_2p_2 \ge 10\).
Then the sampling distribution of \(\widehat{p}_1 - \widehat{p}_2\) is
\[\operatorname{N}\left(p_1 - p_2, \sqrt{\frac{p_1(1-p_1)}{n_1} + \frac{p_2(1-p_2)}{n_2}}\right).\]
In the CLT, \(p_1 = \widehat{p}_1\) and \(p_2 = \widehat{p}_2\).
Point estimate: \(\widehat{p}_1 - \widehat{p}_2\).
Standard error: \[\sqrt{\frac{\widehat{p}_1(1 - \widehat{p}_1)}{n_1} + \frac{\widehat{p}_2(1 - \widehat{p}_2)}{n_2}}.\]
Assumption: \(n_1\widehat{p}_1 \ge 10\), \(n_1\widehat{q}_1 \ge 10\), \(n_2\widehat{p}_2 \ge 10\), and \(n_2\widehat{q}_2 \ge 10\).
The number of successes and failures in each sample is at least 10.
\(H_0: p_1 = p_2\)
In the CLT, \(p_1 - p_2 = 0\).
Use the pooled sample proportion \[\overline{p} = \frac{x_1 + x_2}{n_1 + n_2} = \frac{\widehat{p}_1 n_1 + \widehat{p}_2 n_2}{n_1 + n_2}\] for both \(p_1\) and \(p_2\).
Standard error: \[\sqrt{\frac{\overline{p}(1 - \overline{p})}{n_1} + \frac{\overline{p}(1 - \overline{p})}{n_2}}.\]
Assumption: \(n_1\overline{p} \ge 10\), \(n_1\overline{q} \ge 10\), \(n_2\overline{p} \ge 10\), and \(n_2\overline{q} \ge 10\).
Researchers (Saraux at al., 2011) wanted to know whether metal bands used for tagging penguins are harmful. They selected a random sample of 100 penguins, tagged them with RFID chips, and tagged 50 of them with metal bands. After about 4 years, they checked how many penguins in each group survived.
tally(survived ~ group, data = penguin_data) group
survived band control
TRUE 16 31
FALSE 34 19
The same with proportions:
tally(survived ~ group, data = penguin_data, format = "proportion") group
survived band control
TRUE 0.32 0.62
FALSE 0.68 0.38
Is there a difference in the survival rates between the penguins tagged with the metal band and penguins without the metal band?
How large is the effect?
\(H_0: p_1 - p_2 = 0\)
\(H_A: p_1 - p_2 \neq 0\)
Sample statistic: the difference of the sample proportions \(\widehat{p}_1 - \widehat{p_2}\).
We need to find its null distribution.
The observed value:
diffprop(survived ~ group, data = penguin_data)diffprop
0.3
Sampling from the null population:
diffprop(survived ~ shuffle(group), data = penguin_data)diffprop
-0.06
Repeat:
do(10000) * diffprop(survived ~ shuffle(group), data = penguin_data) -> null_dist
glimpse(null_dist)Rows: 10,000
Columns: 1
$ diffprop <dbl> -0.10, 0.02, 0.02, -0.14, -0.10, 0.14, 0.26, 0.02, -0.02, -0.…
gf_histogram(~diffprop, data = null_dist, bondary = 0) |>
gf_vline(xintercept = 0.3, color = "red")What proportion of the null samples had the difference of proportion at least as extreme as the observed value?
What is “more extreme”?
\(H_A: p_1 - p_2 {\color{red}{{}\neq{}}} 0\), two-tailed test!
2 * prop(~(diffprop >= 0.3), data = null_dist)prop_TRUE
0.0036
\[SE = \sqrt{\frac{p_1q_1}{n_1} + \frac{p_2q_2}{n_2}}\]
where \(p_1 = p_2 = \overline{p}\) (the pooled proportion)
group
survived band control Total
TRUE 16 31 47
FALSE 34 19 53
Total 50 50 100
\[\overline{p} = \frac{x_1 + x_2}{n_1 + n_2} = \frac{16 + 31}{50 + 50} = \frac{47}{100} = 0.47\]
\[SE = \sqrt{\frac{0.47\cdot 0.53}{50} + \frac{0.47\cdot 0.53}{50}} = 0.0998198\]
group
survived band control Total
TRUE 16 31 47
FALSE 34 19 53
Total 50 50 100
Point estimate: \(\widehat{p}_1 - \widehat{p}_2 = -0.3\).
Test statistic: \[z = \frac{(\widehat{p}_1 - \widehat{p}_2) - (p_1 - p_2)}{SE} = \frac{-0.3 - 0}{0.0998198} = -3.0054146\]
P-value: twice the area to the left of \(z\):
2 * pnorm(z)[1] 0.00265219
gf_dhistogram(~diffprop, data = null_dist, bondary = 0) |>
gf_dist("norm", mean = 0, sd = SE)95% confidence interval for \(p_1 - p_2\).
Point estimate: \(\widehat{p}_1 - \widehat{p}_2 = -0.3\).
Standard error: \[SE = \sqrt{\frac{\widehat{p}_1\widehat{q}_1}{n_1} + \frac{\widehat{p}_2\widehat{q}_2}{n_2}} = \sqrt{\frac{0.32\cdot 0.68}{50} + \frac{0.62\cdot 0.38}{50}} = 0.095205\]
Margin of error: \(m = z^*\cdot SE = 1.96 \cdot 0.095205 = 0.1866019\)
Now you can write down the interval.
do(10000) * diffprop(survived ~ group, data = resample(penguin_data)) -> boots
glimpse(boots)Rows: 10,000
Columns: 1
$ diffprop <dbl> 0.2192877, 0.2914614, 0.1328289, 0.1698718, 0.3589436, 0.4006…
Interval:
qdata(~diffprop, c(.025,.975), data = boots) 2.5% 97.5%
0.1070382 0.4823718