Chapter 4 Week 7

4.1 with and table

The data always have 0 and 1 indicating exposure/disease/confounder status, to get contigency tables for further calculation, we use with and table.

dat <- foreign::read.dta("./_data/diet.dta")
head(dat)
##   act diet mort
## 1   0    0    1
## 2   0    0    0
## 3   0    0    1
## 4   0    0    1
## 5   0    0    0
## 6   0    0    0

4.1.1 2 way table

  • table uses the cross-classifying factors to build a contingency table of the counts at each combination of factor levels.

    • The input data for table should be one or more objects which can be interpreted as factors.

    • To create a two by two table for exposure and disease: table(data$exposure, data$disease)

table(dat$diet, dat$mort)
##    
##       0   1
##   0 186 136
##   1 119  59
  • with(data, expr, ...): evaluate an R expression in an environment constructed from data

    • data: data to use for constructing an environment.
    • expr: expression to evaluate, in this case we use table()
with(dat, table(diet,mort))
##     mort
## diet   0   1
##    0 186 136
##    1 119  59
  • The same results as using table() function only

    • As the environment has been constructed, there’s no need to use dat$ to tell R how to find the factor.

4.1.2 3 way table

  • Similar method as 2 way table, but indicate the third factor using by =

    • To create a three way table for exposure and disease: with(data, table(exposure, disease, by = confounder))
table3 <- with(dat, table(diet, mort, by = act))
table3
## , , by = 0
## 
##     mort
## diet  0  1
##    0 75 75
##    1 22 17
## 
## , , by = 1
## 
##     mort
## diet  0  1
##    0 45 40
##    1 36 28
## 
## , , by = 2
## 
##     mort
## diet  0  1
##    0 34 14
##    1 37 10
## 
## , , by = 3
## 
##     mort
## diet  0  1
##    0 32  7
##    1 24  4

4.2 mantelhaen.test

Performs a Cochran-Mantel-Haenszel chi-squared test of the null that two nominal variables are conditionally independent in each stratum, assuming that there is no three-way interaction.

  • To test the null hypothesis that the exposure is independent of the disease when adjusting for confounding, we can use
mantelhaen.test(x, y = NULL, z = NULL,
                alternative = c("two.sided", "less", "greater"),
                correct = TRUE, exact = FALSE, conf.level = 0.95)
  • Input data:

    • A 3-dimensional contingency table in array form as x
    • Or three factor objects with at least 2 levels as x, y, and z.
  • correct = TRUE: Whether to apply continuity correction when computing the test statistic.

  • exact = FALSE: Whether the Mantel-Haenszel test or the exact conditional test (given the strata margins) should be computed.

# example
mantelhaen.test(table3)
## 
##  Mantel-Haenszel chi-squared test with continuity correction
## 
## data:  table3
## Mantel-Haenszel X-squared = 1.1249, df = 1, p-value = 0.2889
## alternative hypothesis: true common odds ratio is not equal to 1
## 95 percent confidence interval:
##  0.5228808 1.1789039
## sample estimates:
## common odds ratio 
##         0.7851281