# 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)
Run the code above in your browser using DataLab