Math 132A

Inference For Two Way Tables

Two Categorical Variables

with more than 2 levels.

Treatment 1 Treatment 2 Control Sum
diseased 20 17 23 60
healthy 30 25 15 70
Sum 50 42 38 130

Is there an association between the two variables?

\(H_0:\) There is no association

A Problem

What should we use as test statistic?

How do we reduce the whole table to a single number?

Column Rates

Treatment 1 Treatment 2 Control Sum
diseased 20 17 23 60
healthy 30 25 15 70
Sum 50 42 38 130

Overall column rates:

  • diseased: \(\frac{60}{130} = 0.4615385\)
  • healthy: \(\frac{70}{130} = 0.5384615\)

Expected Counts

How would the table look like if there really was no association?

Treatment 1 Treatment 2 Control Sum
diseased

\(\frac{60}{130}\cdot 50\)

23.0769231

\(\frac{60}{130}\cdot 42\)

19.3846154

\(\frac{60}{130}\cdot 38\)

17.5384615

60
healthy

\(\frac{70}{130}\cdot 50\)

26.9230769

\(\frac{70}{130}\cdot 42\)

22.6153846

\(\frac{70}{130}\cdot 38\)

20.4615385

70
Sum 50 42 38 130
  • This is a table of so called expected counts.
  • This is what the table would look like if it showed no association between the two variables.

Comparison

  • Observed counts:

    Treatment 1 Treatment 2 Control
    diseased 20 17 23
    healthy 30 25 15
  • Expected counts:

    Treatment 1 Treatment 2 Control
    diseased 23.07692 19.38462 17.53846
    healthy 26.92308 22.61538 20.46154

Discrepancies

Observed – Expected:

Treatment 1 Treatment 2 Control
diseased \(20 - 23.0769231\) \(17 - 19.3846154\) \(23 - 17.5384615\)
healthy \(30 - 26.9230769\) \(25 - 22.6153846\) \(15 - 20.4615385\)

Treatment 1 Treatment 2 Control
diseased \(-3.0769231\) \(-2.3846154\) \(5.4615385\)
healthy \(3.0769231\) \(2.3846154\) \(-5.4615385\)

Z-scores

  • z-score is \[z = \frac{\text{observed} - \text{expected}}{\text{standard error}}\]
  • What is the standard error?
  • Square root of the expected count.
  • \[z = \frac{\text{observed} - \text{expected}}{\sqrt{\text{expected}}}\]
  • Some are positive, some negative.
  • Square them to make them positive.
  • \[z^2 = \frac{(\text{observed} - \text{expected})^2}{\text{expected}}\]

Squares of z-scores

Treatment 1 Treatment 2 Control
diseased \(\frac{(20 - 23.0769231)^2}{23.0769231}\) \(\frac{(17 - 19.3846154)^2}{19.3846154}\) \(\frac{(23 - 17.5384615)^2}{17.5384615}\)
healthy \(\frac{(30 - 26.9230769)^2}{26.9230769}\) \(\frac{(25 - 22.6153846)^2}{22.6153846}\) \(\frac{(15 - 20.4615385)^2}{20.4615385}\)

Treatment 1 Treatment 2 Control
diseased \(\frac{(-3.0769231)^2}{23.0769231}\) \(\frac{(-2.3846154)^2}{19.3846154}\) \(\frac{(5.4615385)^2}{17.5384615}\)
healthy \(\frac{(3.0769231)^2}{26.9230769}\) \(\frac{(2.3846154)^2}{22.6153846}\) \(\frac{(-5.4615385)^2}{20.4615385}\)

Treatment 1 Treatment 2 Control
diseased 0.4102564 0.2933455 1.700742
healthy 0.3516484 0.2514390 1.457779

\(\chi^2\)-score

Add all the squares of z-scores:

Treatment 1 Treatment 2 Control
diseased 0.4102564 0.2933455 1.700742
healthy 0.3516484 0.2514390 1.457779

\[\chi^2 = 0.4102564 + 0.2933455 + 1.7007422 + 0.3516484 + 0.251439 + 1.4577791 = 4.4652106\]


Using R:

chisq(cont_table)
X.squared 
 4.465211 

Test Statistic

Test statistic is the \(\chi^2\) score:

  1. Start with the contingency table (observed counts).
  2. Calculate the margins of the contingency table.
  3. Use the right margin (row sums) to calculate the overall row rates.
  4. Multiply each column sum (sample size) by the row rates to get the expected counts.
  5. Calculate the discrepancies by subtracting the expected counts from the observed counts (observed - expected).
  6. Square all the discrepancies.
  7. Divide them by the expected counts to get the \(z^2\)s.
  8. Add all the \(z^2\)s together.

The higher the \(\chi^2\)-score is, the more evidence against the null hypothesis we have.

Hypothesis Test

  • \(H_0: \chi^2 = 0\)
  • \(H_A: \chi^2 > 0\) (Always right tailed test)

What is the null distribution of the sample \(\chi^2\)-scores?


Central Limit Theorem for two-way tables:

If all the expected counts are at least 5, then the null distribution of \(\chi^2\)-scores is the \(\chi^2\)-distribution with \[(\text{number of rows} - 1)(\text{number of columns} - 1)\] degrees of freedom.

\(\chi^2\) Distribution

  • Very skewed to the right.
  • Parameter: degrees of freedom.
  • We use the following formula: \[df = (\text{number of rows} - 1)(\text{number of columns} - 1)\]
  • P-value is the area to the right of the observed \(\chi^2\).

\(\chi^2\) Distribution Plot

gf_dist("chisq", df = 2) |>
    gf_vline(xintercept = observed_chisquare, color = "red")
1 - pchisq(observed_chisquare, df = 2)
[1] 0.1072486

\(\chi^2\) table

Observed \(\chi^2\)-score: 4.4652106

p-value is between 0.1 and 0.2.