# example data for a matched case-control design
# subject case control
#
# 1 1 1
# 2 0 1
# 3 1 0
# 4 0 1
# 5 1 1
# ... ... ...
# 100 0 0
# example data for a before-after design
# subject before after
#
# 1 1 1
# 2 0 1
# 3 1 0
# 4 0 1
# 5 1 1
# ... ... ...
# 100 0 0
# convert to a 2 x 2 frequency table
freqs <- matrix(c(30, 10, 20, 40), nrow = 2, ncol = 2)
colnames(freqs) <- c("control_1", "control_0")
rownames(freqs) <- c("case_1", "case_0")
freqs
# convert to a 2 x 2 proportion table
props <- freqs / sum(freqs)
props
# discordant pairs (0 and 1, or 1 and 0) in 'props' matrix
# are the sample estimates of prob01 and prob10
# post-hoc exact power
power.exact.mcnemar(prob10 = 0.20, prob01 = 0.10,
n.paired = 100, alpha = 0.05,
alternative = "two.sided", method = "exact")
# required sample size for exact test
# assuming prob01 and prob10 are population parameters
power.exact.mcnemar(prob10 = 0.20, prob01 = 0.10,
power = 0.80, alpha = 0.05,
alternative = "two.sided", method = "exact")
# we may not have 2 x 2 joint probs
# convert marginal probs to joint probs
joint.probs.2x2(prob1 = 0.55, # mean of case group (or after)
prob2 = 0.45, # mean of matched control group (or before)
# correlation between matched case-control or before-after
rho = 0.4141414
)
Run the code above in your browser using DataLab