# Cochran Q Test for k related samples in R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**ALSTAT R Blog**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

To run the Cochran Q Test in R, we need to download the package of it first, since it is not built-in in R. The name of the package is RVAideMemoire authored by Maxime Hervé. Here’s how to do it.

Codes:

Here we are installing the package named RVAideMemoire.

Press ‘Enter/Return’ key then there will be a pop-up window asking you to choose your CRAN Mirror.

Select your country or any country you want, then hit ‘Ok’, and it will be downloaded and installed.

After installation, you can now run the package, and to do that you need to type the following code,

Note: Every time you use the Cochran Q Test function make sure you call the package first.

This time lets try and ask help for the function of it.

Example: Four methods (A, B, C, and D) of treating raw fabric to make it water repellent were tested for effectiveness on six types of fabric. A satisfactory result got a 1.

Method A | Method B | Method C | Method D |

1 | 1 | 0 | 0 |

1 | 1 | 0 | 1 |

1 | 0 | 0 | 0 |

1 | 1 | 1 | 0 |

1 | 1 | 0 | 1 |

1 | 1 | 0 | 1 |

*Solution:*

1. H0: The four treatments are equally effective.

H1: The four treatments are not equally effective.

2. Level of significance: alpha = 0.05

4. Rejection Region: Reject the null hypothesis if X^2 >= 7.185. Otherwise, do not reject it.

5. Computation:

Method A | Method B | Method C | Method D | Li | (Li)^2 |

1 | 1 | 0 | 0 | 2 | 4 |

1 | 1 | 0 | 1 | 3 | 9 |

1 | 0 | 0 | 0 | 1 | 1 |

1 | 1 | 1 | 0 | 3 | 9 |

1 | 1 | 0 | 1 | 3 | 9 |

1 | 1 | 0 | 1 | 3 | 9 |

G1 | G2 | G3 | G4 | Li | (Li)^2 |

6 | 5 | 1 | 3 | 15 | 41 |

6. Decision: Since 9.31579 is greater than 7.185, then we reject the null hypothesis

7. Conclusion: Therefore, we have sufficient statistical evidence to conclude that the effectiveness of at least two treatments differ.

**Computations in R**

Now lets try and solve the above example using Cochran Q Test in R. First thing we need to do is to input the data.

Now, its better to run the length function, so that we would know if we miss something.

This time lets make the four columns and the 6 rows, i.e., the four methods and the 6 different types of fabric, respectively.

Now, lets combine the rows and columns to make a table.

The output of this should look like this.

So, as you can see everything is the same with the given table in the example. Now, lets try and confirm the above computations.

Clearly in the result, that our test statistics Q is equal to 9.3158 and the p-value is 0.025374. And since the p-value is less than the alpha, then we reject the null hypothesis. That’s why, we have a pairwise comparisons by Wilcoxon sign test.

“Special Thanks to: Tal Galili and Maxime Hervé”

Example used: http://frank.mtsu.edu/~dwalsh/6604/6604COCH.pdf

To

**leave a comment**for the author, please follow the link and comment on their blog:**ALSTAT R Blog**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.