Performs the Cochran's Q test for unreplicated randomized block
design experiments with a binary response variable and paired data. This
test is analogue to the friedman.test()
with 0,1 coded
response. It's an extension of the McNemar Chi-squared test for comparing
more than two paired proportions.
cochran_qtest(data, formula)
a data frame containing the variables in the formula.
a formula of the form a ~ b | c
, where a
is the
outcome variable name; b is the within-subjects factor variables; and c
(factor) is the column name containing individuals/subjects identifier.
Should be unique per individual.
# Generate a demo data
mydata <- data.frame(
outcome = c(0,1,1,0,0,1,0,1,1,1,1,1,0,0,1,1,0,1,0,1,1,0,0,1,0,1,1,0,0,1),
treatment = gl(3,1,30,labels=LETTERS[1:3]),
participant = gl(10,3,labels=letters[1:10])
)
mydata$outcome <- factor(
mydata$outcome, levels = c(1, 0),
labels = c("success", "failure")
)
# Cross-tabulation
xtabs(~outcome + treatment, mydata)
#> treatment
#> outcome A B C
#> success 2 5 10
#> failure 8 5 0
# Compare the proportion of success between treatments
cochran_qtest(mydata, outcome ~ treatment|participant)
#> # A tibble: 1 × 6
#> .y. n statistic df p method
#> * <chr> <int> <dbl> <dbl> <dbl> <chr>
#> 1 outcome 10 10.9 2 0.00432 Cochran's Q test
# pairwise comparisons between groups
pairwise_mcnemar_test(mydata, outcome ~ treatment|participant)
#> # A tibble: 3 × 6
#> group1 group2 p p.adj p.adj.signif method
#> * <chr> <chr> <dbl> <dbl> <chr> <chr>
#> 1 A B 0.371 1 ns McNemar test
#> 2 A C 0.0133 0.0399 * McNemar test
#> 3 B C 0.0736 0.221 ns McNemar test