Performs McNemar chi-squared test to compare paired proportions.

Wrappers around the R base function mcnemar.test(), but provide pairwise comparisons between multiple groups

mcnemar_test(x, y = NULL, correct = TRUE)

pairwise_mcnemar_test(
  data,
  formula,
  type = c("mcnemar", "exact"),
  correct = TRUE,
  p.adjust.method = "bonferroni"
)

Arguments

x

either a two-dimensional contingency table in matrix form, or a factor object.

y

a factor object; ignored if x is a matrix.

correct

a logical indicating whether to apply continuity correction when computing the test statistic.

data

a data frame containing the variables in the formula.

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.

type

type of statistical tests used for pairwise comparisons. Allowed values are one of c("mcnemar", "exact").

p.adjust.method

method to adjust p values for multiple comparisons. Used when pairwise comparisons are performed. Allowed values include "holm", "hochberg", "hommel", "bonferroni", "BH", "BY", "fdr", "none". If you don't want to adjust the p value (not recommended), use p.adjust.method = "none".

Value

return a data frame with the following columns:

  • n: the number of participants.

  • statistic: the value of McNemar's statistic.

  • df the degrees of freedom of the approximate chi-squared distribution of the test statistic.

  • p: p-value.

  • p.adj: the adjusted p-value.

  • method: the used statistical test.

  • p.signif: the significance level of p-values.

The returned object has an attribute called args, which is a list holding the test arguments.

Functions

  • mcnemar_test(): performs McNemar's chi-squared test for comparing two paired proportions

  • pairwise_mcnemar_test(): performs pairwise McNemar's chi-squared test between multiple groups. Could be used for post-hoc tests following a significant Cochran's Q test.

Examples


# Comparing two paired proportions
#%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Data: frequencies of smokers before and after interventions
xtab <- as.table(
  rbind(c(25, 6), c(21,10))
)
dimnames(xtab) <- list(
  before = c("non.smoker", "smoker"),
  after = c("non.smoker", "smoker")
)
xtab
#>             after
#> before       non.smoker smoker
#>   non.smoker         25      6
#>   smoker             21     10

# Compare the proportion of smokers
mcnemar_test(xtab)
#> # A tibble: 1 × 6
#>       n statistic    df       p p.signif method      
#> * <dbl>     <dbl> <dbl>   <dbl> <chr>    <chr>       
#> 1    62      7.26     1 0.00705 **       McNemar test

# Comparing multiple related proportions
# %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# 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