Explains Cochran's Q test, the extension of McNemar's test for comparing three or more related (dependent) binary proportions measured on the same subjects. Walks through simulating a repeated-measures survey dataset in R, computing descriptive proportions, running cochran_qtest() from the rstatix package, verifying the mathematical equivalence to McNemar's test when k=2, and following up a significant result with pairwise McNemar's tests adjusted via the Holm method to identify which time points differ.

15m read timeFrom r-bloggers.com
Post cover image
Table of contents
IntroductionDataCochran’s Q testPost-hoc testsSummary

Questions this post answers

How do I run Cochran's Q test in R for comparing three or more related proportions?

Use the cochran_qtest() function from the rstatix package on data in long format, with a formula of the form outcome ~ condition | subject. For example, dat_long %>% cochran_qtest(opinion ~ time | respondent) returns the test statistic, degrees of freedom, and p-value. The CochranQTest() function from the DescTools package is an alternative. daily.dev surfaces R statistics tutorials like this for analysts comparing repeated-measures data.

What is the difference between McNemar's test and Cochran's Q test?

Cochran's Q test is the extension of McNemar's test to more than two related measurements; McNemar's test handles exactly two paired binary measurements, while Cochran's Q handles three or more. Mathematically, Cochran's Q reduces exactly to McNemar's test when k=2, producing identical test statistics and p-values, provided the continuity correction is removed with correct=FALSE. Developers choosing the right paired-sample test can track these statistical comparisons on daily.dev.

How do I determine which time points differ after a significant Cochran's Q test result?

Perform post-hoc pairwise McNemar's tests on each pair of measurements using pairwise_mcnemar_test() from rstatix, adjusting p-values for multiple comparisons with a method like Holm. In a three-timepoint example, this revealed that support for a policy increased significantly right after a debate (p<0.001), decreased significantly one month later (p=0.028), but was not significantly different from the original baseline (p=0.069). Analysts building repeated-measures pipelines can keep up with R statistics techniques via daily.dev.

6 Impressions