Skip to content
Prev 350734 / 398502 Next

Question about cochran test in R

On 2015-05-07 09:15, Jim Lemon wrote:

            
Cochran's Q test is a marginal homogeneity test, and such tests can be 
performed by the 'mh_test' function in the 'coin' package.  The 
following replicates the result in the blog post

 > library("coin")
 >
 > dta <- data.frame(
+     method    = factor(rep(LETTERS[1:4], 6)),
+     repellent = factor(c(1, 1, 0, 0,
+                          1, 1, 0, 1,
+                          1, 0, 0, 0,
+                          1, 1, 1, 0,
+                          1, 1, 0, 1,
+                          1, 1, 0, 1)),
+     fabric    = gl(6, 4, labels = as.roman(1:6))
+ )
 >
 > mh_test(repellent ~ method | fabric, data = dta)

	Asymptotic Marginal-Homogeneity Test

data:  repellent by
	 method (A, B, C, D)
	 stratified by fabric
chi-squared = 9.3158, df = 3, p-value = 0.02537


and uses the asymptotic approximation to compute the p-value.  The 
'coin' package also allows you to approximate the exact null 
distribution using Monte Carlo methods:

 > set.seed(123)
 > mh_test(repellent ~ method | fabric, data = dta,
+         distribution = approximate(B = 10000L))

	Approximative Marginal-Homogeneity Test

data:  repellent by
	 method (A, B, C, D)
	 stratified by fabric
chi-squared = 9.3158, p-value = 0.0202


For future reference, 'mh_test' is fairly general and handles both 
matched pairs or matched sets.  So, the well-known tests due McNemar, 
Cochran, Stuart(-Maxwell) and Madansky are just special cases.

For more general symmetry test problems, the 'coin' package offers the 
'symmetry_test' function and this can be used to perform, e.g., 
multivariate marginal homogeneity tests like the multivariate McNemar 
test (Klingenberg and Agresti, 2006) or the multivariate Friedman test 
(Gerig, 1969).


Henric