Math 132A

Inference For Categorical Variables II

Two Categorical Variables

  • Both with 2 levels (binary)

    • An “explanatory” variable: splits the population into two groups.
    • A “response” variable: splits each group into “success” and “failure”.
    • We can ask whether the two groups have the same proportion of successes.
    • In other words, we want to know whether there is an association between the variables.
  • At least one of them has more than two levels

    • Again, we want to know whether there is an association between the variables.

Difference of Proportions

  • The “explanatory” variable splits the population into group 1 and group 2.
  • One of the levels of the “response” variable is considered a “success”.
  • \(p_1\): the proportion of “successes” in the group 1.
  • \(p_2\): the proportion of “successes” in the group 2.
  • \(p_1 - p_2\): the difference between the population proportions.
  • The “explanatory” variable also splits the sample into sample 1 and sample 2.
  • \(\widehat{p}_1\): the proportion of “successes” in the sample 1.
  • \(\widehat{p}_2\): the proportion of “successes” in the sample 2.
  • \(\widehat{p}_1 - \widehat{p}_2\): the difference between the sample proportions.

Hypothesis Test

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

Interval Estimate

Confidence interval for difference of two proportions.

Two options to get the bootstrap distribution:

  • Resampling

  • Central Limit Theorem

CLT for Difference of Two Proportions

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).\]

Bootstrap Distribution

  • 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.

Null Distribution

\(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\).

Are metal bands used for tagging harmful to penguins?

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.

Data

Summary

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

Questions

  • 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?

Is There a Difference?

\(H_0: p_1 - p_2 = 0\)

  • \(p_1\): probability that a penguin tagged by a metal band survives
  • \(p_2\): probability that a penguin not tagged by a metal band survives

\(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.

Using Simulation

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.…

Visualizing the Null Distribution

gf_histogram(~diffprop, data = null_dist, bondary = 0) |>
    gf_vline(xintercept = 0.3, color = "red")

P-value

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 

Using the CLT

\[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
  • \(x_1 = 16\)
  • \(n_1 = 50\)
  • \(x_2 = 31\)
  • \(n_2 = 50\)

\[\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\]

Using the CLT cont.

        group
survived band control Total
   TRUE    16      31    47
   FALSE   34      19    53
   Total   50      50   100
  • \(\widehat{p}_1 = \frac{16}{50} = 0.32\)
  • \(\widehat{p}_2 = \frac{31}{50} = 0.62\)

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

Plots

gf_dhistogram(~diffprop, data = null_dist, bondary = 0) |>
    gf_dist("norm", mean = 0, sd = SE)

How Large is the Effect?

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.

Bootstrapping

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