mcnemar_test: McNemar's Chi-squared Test for Count Data

View source: R/mcnemar_test.R

mcnemar_testR Documentation

McNemar's Chi-squared Test for Count Data

Description

Performs McNemar chi-squared test to compare paired proportions.

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

Usage

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

# Compare the proportion of smokers
mcnemar_test(xtab)

# 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)

# Compare the proportion of success between treatments
cochran_qtest(mydata, outcome ~ treatment|participant)

# pairwise comparisons between groups
pairwise_mcnemar_test(mydata, outcome ~ treatment|participant)


kassambara/rstatix documentation built on Feb. 6, 2023, 3:36 a.m.