GaryBAYLOR / R-code

A collection of algorithms written by myself for solving statistical problems
0 stars 0 forks source link

Cochran Mantel Haenszel Test in R #9

Open GaryBAYLOR opened 9 years ago

GaryBAYLOR commented 9 years ago

The import thing is to input the data in the right form. The form should be an array with 2 X 2 X q dimension, q is the number of strata. The following code is an example in Cochran–Mantel–Haenszel test for repeated tests of independence.

data <- array(c(708, 50, 169, 13,
        136, 24, 73, 14,
        106, 32, 17, 4,
        109, 22, 16, 26,
        801, 102, 180, 25,
        159, 27, 18, 13,
        151, 51, 28, 15,
        950, 173, 218, 33),
        dim = c(2, 2, 8),
        dimnames = 
        list(hand = c("right", "left"),
              hair = c("cw", "ccw"),
              strata = c("white_children", "British_adult", "Penn_white", "Welsh_men", 
                         "German_soldiers", "German_children", "New_York", "American_men"))
)
mantelhaen.test(data)

The result is as follows.

Mantel-Haenszel chi-squared test with continuity correction

data:  data
Mantel-Haenszel X-squared = 6.0702, df = 1, p-value = 0.01375
alternative hypothesis: true common odds ratio is not equal to 1
95 percent confidence interval:
 1.061250 1.608976
sample estimates:
common odds ratio 
         1.306723 

The p-value is 0.01375, showing that the two variables(hand and hair) are not independent. The Chi-square statistic is 6.07.