| Title: | Calculate Confidence Intervals |
|---|---|
| Description: | This calculates a variety of different CIs for proportions and difference of proportions that are commonly used in the pharmaceutical industry including Wald, Wilson, Clopper-Pearson, Agresti-Coull and Jeffreys for proportions. And Miettinen-Nurminen (1985) <doi:10.1002/sim.4780040211>, Wald, Haldane, and Mee <https://www.lexjansen.com/wuss/2016/127_Final_Paper_PDF.pdf> for difference in proportions. |
| Authors: | Christina Fillmore [aut, cre] (ORCID: <https://orcid.org/0000-0003-0595-2302>), GlaxoSmithKline Research & Development Limited [cph, fnd], Mike Sprys [aut], Dan Lythgoe [aut] (ORCID: <https://orcid.org/0000-0002-4682-772X>) |
| Maintainer: | Christina Fillmore <[email protected]> |
| License: | Apache License (>= 2) |
| Version: | 0.2.1 |
| Built: | 2026-05-08 15:21:29 UTC |
| Source: | https://github.com/gsk-biostatistics/cicalc |
Calculates the Agresti-Coull interval (created by Alan Agresti and Brent Coull) by
(for 95% CI) adding two successes and two failures to the data and then using the Wald formula to construct a CI.
ci_prop_agresti_coull(x, conf.level = 0.95, data = NULL)ci_prop_agresti_coull(x, conf.level = 0.95, data = NULL)
x |
( |
conf.level |
( |
data |
( |
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Type of method used |
Calculates the Clopper-Pearson interval by calling stats::binom.test().
Also referred to as the exact method.
ci_prop_clopper_pearson(x, conf.level = 0.95, data = NULL)ci_prop_clopper_pearson(x, conf.level = 0.95, data = NULL)
x |
( |
conf.level |
( |
data |
( |
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Type of method used |
Anderson-Hauck Confidence Interval for Difference in Proportions
ci_prop_diff_ha(x, by, conf.level = 0.95, data = NULL)ci_prop_diff_ha(x, by, conf.level = 0.95, data = NULL)
x |
( |
by |
( |
conf.level |
( |
data |
( |
The confidence interval is given by:
.
An object containing the following components:
n |
The number of responses for each group |
N |
The total number in each group |
estimate |
The point estimate of the difference in proportions |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Anderson-Hauck Confidence Interval |
Hauck WW, Anderson S. (1986) A comparison of large-sample confidence interval methods for the difference of two binomial probabilities The American Statistician 40(4). p.318-322. Constructing Confidence Intervals for the Differences of Binomial Proportions in SAS
responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_ha(x = responses, by = arm)responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_ha(x = responses, by = arm)
Haldane Confidence Interval for Difference in Proportions
ci_prop_diff_haldane(x, by, conf.level = 0.95, data = NULL)ci_prop_diff_haldane(x, by, conf.level = 0.95, data = NULL)
x |
( |
by |
( |
conf.level |
( |
data |
( |
The confidence interval is calculated by where:
where
An object containing the following components:
n |
The number of responses for each group |
N |
The total number in each group |
estimate |
The point estimate of the difference in proportions (theta*) |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Haldane Confidence Interval |
Constructing Confidence Intervals for the Differences of Binomial Proportions in SAS
responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_haldane(x = responses, by = arm)responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_haldane(x = responses, by = arm)
Jeffreys-Perks Confidence Interval for Difference in Proportions
ci_prop_diff_jp(x, by, conf.level = 0.95, data = NULL)ci_prop_diff_jp(x, by, conf.level = 0.95, data = NULL)
x |
( |
by |
( |
conf.level |
( |
data |
( |
The confidence interval is calculated by where:
where
An object containing the following components:
n |
The number of responses for each group |
N |
The total number in each group |
estimate |
The point estimate of the difference in proportions (theta*) |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Jeffreys-Perks Confidence Interval |
Constructing Confidence Intervals for the Differences of Binomial Proportions in SAS
responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_jp(x = responses, by = arm)responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_jp(x = responses, by = arm)
Mee Confidence Interval for Difference in Proportions
ci_prop_diff_mee(x, by, conf.level = 0.95, delta = NULL, data = NULL)ci_prop_diff_mee(x, by, conf.level = 0.95, delta = NULL, data = NULL)
x |
( |
by |
( |
conf.level |
( |
delta |
( |
data |
( |
The confidence interval is calculated by where:
where
An object containing the following components:
n |
The number of responses for each group |
N |
The total number in each group |
estimate |
The point estimate of the difference in proportions (p1-p2) |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Mee Confidence Interval |
Constructing Confidence Intervals for the Differences of Binomial Proportions in SAS
responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_mee(x = responses, by = arm)responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_mee(x = responses, by = arm)
Calculates the confidence interval for the Mantel-Haenszel estimate of the common risk difference across multiple 2x2 tables (strata), using the Sato or Independent Binomial variance estimator.
ci_prop_diff_mh_strata( x, by, strata, conf.level = 0.95, sato_var = TRUE, data = NULL )ci_prop_diff_mh_strata( x, by, strata, conf.level = 0.95, sato_var = TRUE, data = NULL )
x |
( |
by |
( |
strata |
( |
conf.level |
( |
sato_var |
( |
data |
( |
The Mantel-Haenszel common risk difference is computed as:
where ,
,
,
and are the number of events in each group, and ,
and are the group sizes in stratum .
The Sato variance is:
where
and .
The Cochran Independent Binomial variance is:
where
and .
The confidence interval is then .
An object containing the following components:
estimate |
The Mantel-Haenszel estimated common risk difference |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
variance |
Variance estimate |
statistic |
Z-Statistic under the null hypothesis, assuming a common risk difference of 0 |
p.value |
p-value under the null hypothesis, assuming a common risk difference of 0 |
method |
Description of the method used ("Mantel-Haenszel Confidence Interval, Sato Variance") or ("Mantel-Haenszel Confidence Interval, Independent Binomial") |
Agresti, A. (2013). Categorical Data Analysis. 3rd Edition. John Wiley & Sons, Hoboken, NJ p. 231 Cochran, W.G. (1954). The Combination of estimates from different experiments. Biometrics, 10(1), p.101-129
# Generate binary samples with strata responses <- expand(c(9, 3, 7, 2), c(10, 10, 10, 10)) arm <- rep(c("treat", "control"), 20) strata <- rep(c("stratum1", "stratum2"), times = c(20, 20)) # Calculate common risk difference ci_prop_diff_mh_strata(x = responses, by = arm, strata = strata) # Calculate risk difference with independent binomial variance ci_prop_diff_mh_strata(x = responses, by = arm, strata = strata, sato_var = FALSE)# Generate binary samples with strata responses <- expand(c(9, 3, 7, 2), c(10, 10, 10, 10)) arm <- rep(c("treat", "control"), 20) strata <- rep(c("stratum1", "stratum2"), times = c(20, 20)) # Calculate common risk difference ci_prop_diff_mh_strata(x = responses, by = arm, strata = strata) # Calculate risk difference with independent binomial variance ci_prop_diff_mh_strata(x = responses, by = arm, strata = strata, sato_var = FALSE)
Calculates the Miettinen-Nurminen (MN) confidence interval for the difference between two proportions. This method can be more accurate than traditional methods, especially with small sample sizes or proportions close to 0 or 1.
ci_prop_diff_mn(x, by, conf.level = 0.95, delta = NULL, data = NULL)ci_prop_diff_mn(x, by, conf.level = 0.95, delta = NULL, data = NULL)
x |
( |
by |
( |
conf.level |
( |
delta |
( |
data |
( |
The function implements the Miettinen-Nurminen method to compute confidence intervals for the difference between two proportions. This approach:
Calculates the Miettinen-Nurminen score test statistic for different possible values of the proportion difference (delta)
Identifies the delta values where the test statistic equals the critical value corresponding to the desired confidence level
Returns these boundary values as the confidence interval limits
The method uses a score test with a small-sample correction factor, making it more accurate than normal approximation methods, especially for small samples or extreme proportions. The equation for the test statistics is as follows:
where represent the observed number of successes
divided by the number of participant in that group. The is a
function of the delta values and is create with the following equation"
represent the MLE of the proportions.
and
where:
For more information about these equations see Miettinen (1985)
An object containing the following components:
estimate |
The point estimate of the difference in proportions (p_x - p_y) |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
delta |
delta value(s) used |
statistic |
Z-Statistic under the null hypothesis based on the given 'delta' |
p.value |
p-value under the null hypothesis based on the given 'delta' |
method |
Description of the method used ("Miettinen-Nurminen Confidence Interval") |
If delta is not provided statistic and p.value will be NULL
Miettinen, O. S., & Nurminen, M. (1985). Comparative analysis of two rates. Statistics in Medicine, 4(2), 213-226.
# Generate binary samples responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_mn(x = responses, by = arm) # Calculate 99% confidence interval ci_prop_diff_mn(x = responses, by = arm, conf.level = 0.99) # Calculate the p-value under the null hypothesis delta = -0.1 ci_prop_diff_mn(x = responses, by = arm, delta = -0.1) # Calculate from a data.frame data <- data.frame(responses, arm) ci_prop_diff_mn(x = responses, by = arm, data = data)# Generate binary samples responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_mn(x = responses, by = arm) # Calculate 99% confidence interval ci_prop_diff_mn(x = responses, by = arm, conf.level = 0.99) # Calculate the p-value under the null hypothesis delta = -0.1 ci_prop_diff_mn(x = responses, by = arm, delta = -0.1) # Calculate from a data.frame data <- data.frame(responses, arm) ci_prop_diff_mn(x = responses, by = arm, data = data)
Calculates Stratified Miettinen-Nurminen (MN) confidence intervals and corresponding point estimates for the difference between two proportions
ci_prop_diff_mn_strata( x, by, strata, method = c("score", "summary score"), conf.level = 0.95, delta = NULL, data = NULL )ci_prop_diff_mn_strata( x, by, strata, method = c("score", "summary score"), conf.level = 0.95, delta = NULL, data = NULL )
x |
( |
by |
( |
strata |
( |
method |
( |
conf.level |
( |
delta |
( |
data |
( |
The function implements the stratified Miettinen-Nurminen method to compute confidence intervals for the difference between two proportions across multiple strata.
The "score" method is a weighted MN score first described in the original 1985 paper. The formula is:
Calculates weights for each stratum as
Computes the overall weighted difference
Uses the stratified test statistic:
Finds the range of all values of for which the stratified test statistic ()
falls in the acceptance region
The is the Miettinen-Nurminen variance estimate.
See the details of ci_prop_diff_mn() for how is calculated.
The "summary score" method follows the meta-analyses proposed in Agresti 2013 and is consistent with the "Summary Score Confidence Limits" method used in SAS. The formula is:
The point estimate of the stratified risk difference is a weighted average of the midpoints of the within-stratum MN confidence intervals:
Define as the width of the CI for the th stratum divided by and then stratum weights are given by
The variance of is computed as
Confidence limits for the stratified risk difference estimate are
An object containing the following components:
estimate |
The point estimate of the difference in proportions (p_x - p_y) |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
delta |
delta value(s) used |
statistic |
Z-Statistic under the null hypothesis based on the given 'delta' |
p.value |
p-value under the null hypothesis based on the given 'delta' |
method |
Description of the method used ("Stratified {method} Miettinen-Nurminen Confidence Interval") |
If delta is not provided statistic and p.value will be NULL
Miettinen, O. S., & Nurminen, M. (1985). Comparative analysis of two rates. Statistics in Medicine, 4(2), 213-226.
Common Risk Difference :: Base SAS(R) 9.4 Procedures Guide: Statistical Procedures, Third Edition
Agresti, A. (2013). Categorical Data Analysis. 3rd Edition. John Wiley & Sons, Hoboken, NJ
# Generate binary samples with strata responses <- expand(c(9, 3, 7, 2), c(10, 10, 10, 10)) arm <- rep(c("treat", "control"), 20) strata <- rep(c("stratum1", "stratum2"), times = c(20, 20)) # Calculate stratified confidence interval for difference in proportions ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata) # Using the summary score method ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata, method = "summary score") # Calculate 99% confidence interval ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata, conf.level = 0.99) # Calculate p-value under null hypothesis delta = 0.2 ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata, delta = 0.2)# Generate binary samples with strata responses <- expand(c(9, 3, 7, 2), c(10, 10, 10, 10)) arm <- rep(c("treat", "control"), 20) strata <- rep(c("stratum1", "stratum2"), times = c(20, 20)) # Calculate stratified confidence interval for difference in proportions ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata) # Using the summary score method ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata, method = "summary score") # Calculate 99% confidence interval ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata, conf.level = 0.99) # Calculate p-value under null hypothesis delta = 0.2 ci_prop_diff_mn_strata(x = responses, by = arm, strata = strata, delta = 0.2)
Newcombe Confidence Interval for Difference in Proportions
ci_prop_diff_nc(x, by, conf.level = 0.95, correct = FALSE, data = NULL)ci_prop_diff_nc(x, by, conf.level = 0.95, correct = FALSE, data = NULL)
x |
( |
by |
( |
conf.level |
( |
correct |
( |
data |
( |
The Wilson (Score) confidence limits without continuity correction for each
individual binomial proportion, , for , are given by:
Denote the lower and upper Wilson (Score) confidence limits for as
and , respectively.
Then, the Newcombe (Score) confidence limits for the difference in proportions
() are given by:
The confidence intervals with continuity correction for each individual binomial proportion are obtained using the Wilson (Score) confidence limits with continuity correction.
For each binomial proportion , where ,
the confidence intervals are given by:
An object containing the following components:
n |
The number of responses for each group |
N |
The total number in each group |
estimate |
The point estimate of the difference in proportions |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Anderson-Hauck Confidence Interval |
Newcombe, R. G. (1998). Interval estimation for the difference between independent proportions: Comparison of eleven methods. Statistics in Medicine, 17(8), 873–890. Constructing Confidence Intervals for the Differences of Binomial Proportions in SAS
responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_nc(x = responses, by = arm)responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_nc(x = responses, by = arm)
Calculates the stratified Newcombe confidence interval for unequal proportions as described in Yan X, Su XG. Stratified Wilson and Newcombe confidence intervals or multiple binomial proportions. Weights are estimated using CMH or Wilson methods.
ci_prop_diff_nc_strata( x, by, strata, conf.level = 0.95, correct = FALSE, weights_method = c("wilson", "cmh"), data = NULL )ci_prop_diff_nc_strata( x, by, strata, conf.level = 0.95, correct = FALSE, weights_method = c("wilson", "cmh"), data = NULL )
x |
( |
by |
( |
strata |
( |
conf.level |
( |
correct |
(scalar [ci_prop_diff_nc())]: R:ci_prop_diff_nc()) |
weights_method |
( |
data |
( |
Where:
: Mantel-Haenszel common risk difference
: standard normal critical value
: stratum weights
, : Wilson-type CI limits for stratum h
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
weights |
Weights of each strata calculated as per the specified "weights_method" argument. |
method |
Type of method used |
set.seed(1) rsp <- sample(c(TRUE, FALSE), 100, TRUE) grp <- sample(c("Placebo", "Treatment"), 100, TRUE) strata_data <- data.frame( "f1" = sample(c("a", "b"), 100, TRUE), "f2" = sample(c("x", "y", "z"), 100, TRUE), stringsAsFactors = TRUE ) strata <- interaction(strata_data) ci_prop_diff_nc_strata( x = rsp, by = grp, strata = strata, weights_method = "cmh", conf.level = 0.95 )set.seed(1) rsp <- sample(c(TRUE, FALSE), 100, TRUE) grp <- sample(c("Placebo", "Treatment"), 100, TRUE) strata_data <- data.frame( "f1" = sample(c("a", "b"), 100, TRUE), "f2" = sample(c("x", "y", "z"), 100, TRUE), stringsAsFactors = TRUE ) strata <- interaction(strata_data) ci_prop_diff_nc_strata( x = rsp, by = grp, strata = strata, weights_method = "cmh", conf.level = 0.95 )
Calculates the Wald interval by following the usual textbook definition for a difference in proportions confidence interval using the normal approximation.
ci_prop_diff_wald(x, by, conf.level = 0.95, correct = FALSE, data = NULL)ci_prop_diff_wald(x, by, conf.level = 0.95, correct = FALSE, data = NULL)
x |
( |
by |
( |
conf.level |
( |
correct |
( |
data |
( |
An object containing the following components:
n |
Number of responses in each by group |
N |
Total number in each by group |
estimate |
The point estimate of the difference in proportions (p_1 - p_2) |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Type of method used |
responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_wald(x = responses, by = arm)responses <- expand(c(9, 3), c(10, 10)) arm <- rep(c("treat", "control"), times = c(10, 10)) # Calculate 95% confidence interval for difference in proportions ci_prop_diff_wald(x = responses, by = arm)
Calculates the Jeffreys interval, an equal-tailed interval based on the non-informative Jeffreys prior for a binomial proportion.
ci_prop_jeffreys(x, conf.level = 0.95, data = NULL)ci_prop_jeffreys(x, conf.level = 0.95, data = NULL)
x |
( |
conf.level |
( |
data |
( |
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Type of method used |
Calculates the exact mid-p CI for binomial proportions by inverting two one-sided binomial tests that include the mid-p tail.
This is calculated by finding the and that satisfies the following equations:
ci_prop_mid_p(x, conf.level = 0.95, data = NULL)ci_prop_mid_p(x, conf.level = 0.95, data = NULL)
x |
( |
conf.level |
( |
data |
( |
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Type of method used |
Calculates the Wald interval by following the usual textbook definition for a single proportion confidence interval using the normal approximation.
ci_prop_wald(x, conf.level = 0.95, correct = FALSE, data = NULL)ci_prop_wald(x, conf.level = 0.95, correct = FALSE, data = NULL)
x |
( |
conf.level |
( |
correct |
( |
data |
( |
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Type of method used |
# example code x <- c( TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE ) ci_prop_wald(x, conf.level = 0.9)# example code x <- c( TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, FALSE ) ci_prop_wald(x, conf.level = 0.9)
Calculates the Wilson interval by calling stats::prop.test().
Also referred to as Wilson score interval.
ci_prop_wilson(x, conf.level = 0.95, correct = FALSE, data = NULL)ci_prop_wilson(x, conf.level = 0.95, correct = FALSE, data = NULL)
x |
( |
conf.level |
( |
correct |
( |
data |
( |
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
method |
Type of method used |
Calculates the stratified Wilson confidence interval for unequal proportions as described in Xin YA, Su XG. Stratified Wilson and Newcombe confidence intervals for multiple binomial proportions. Statistics in Biopharmaceutical Research. 2010;2(3).
ci_prop_wilson_strata( x, strata, weights = NULL, conf.level = 0.95, max.iterations = 10L, correct = FALSE, data = NULL )ci_prop_wilson_strata( x, strata, weights = NULL, conf.level = 0.95, max.iterations = 10L, correct = FALSE, data = NULL )
x |
( |
strata |
( |
weights |
( |
conf.level |
( |
max.iterations |
(positive |
correct |
(scalar |
data |
( |
An object containing the following components:
n |
Number of responses |
N |
Total number |
estimate |
The point estimate of the proportion |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
weights |
Weights of each strata, will be the same as the input unless unspecified, then it will be the dynamically calculated weights. |
method |
Type of method used |
# Stratified Wilson confidence interval with unequal probabilities set.seed(1) rsp <- sample(c(TRUE, FALSE), 100, TRUE) strata_data <- data.frame( "f1" = sample(c("a", "b"), 100, TRUE), "f2" = sample(c("x", "y", "z"), 100, TRUE), stringsAsFactors = TRUE ) strata <- interaction(strata_data) n_strata <- ncol(table(rsp, strata)) # Number of strata ci_prop_wilson_strata( x = rsp, strata = strata, conf.level = 0.90 ) # Not automatic setting of weights ci_prop_wilson_strata( x = rsp, strata = strata, weights = rep(1 / n_strata, n_strata), conf.level = 0.90 )# Stratified Wilson confidence interval with unequal probabilities set.seed(1) rsp <- sample(c(TRUE, FALSE), 100, TRUE) strata_data <- data.frame( "f1" = sample(c("a", "b"), 100, TRUE), "f2" = sample(c("x", "y", "z"), 100, TRUE), stringsAsFactors = TRUE ) strata <- interaction(strata_data) n_strata <- ncol(table(rsp, strata)) # Number of strata ci_prop_wilson_strata( x = rsp, strata = strata, conf.level = 0.90 ) # Not automatic setting of weights ci_prop_wilson_strata( x = rsp, strata = strata, weights = rep(1 / n_strata, n_strata), conf.level = 0.90 )
Calculates the confidence interval for the Mantel-Haenszel estimate of the common relative risk across multiple 2x2 tables (strata)
ci_rel_risk_cmh_strata(x, by, strata, conf.level = 0.95, data = NULL)ci_rel_risk_cmh_strata(x, by, strata, conf.level = 0.95, data = NULL)
x |
( |
by |
( |
strata |
( |
conf.level |
( |
data |
( |
The Mantel-Haenszel relative risk difference is computed as:
The variance is:
The confidence interval is then .
An object containing the following components:
estimate |
The Mantel-Haenszel estimated common risk difference |
conf.low |
Lower bound of the confidence interval |
conf.high |
Upper bound of the confidence interval |
conf.level |
The confidence level used |
variance |
Mantel-Haenszel variance estimate |
method |
Description of the method used ("Mantel-Haenszel Common Relative Risk Confidence Interval") |
Agresti, A. (2013). Categorical Data Analysis. 3rd Edition. John Wiley & Sons, Hoboken, NJ
# Generate binary samples with strata responses <- expand(c(9, 3, 7, 2), c(10, 10, 10, 10)) arm <- rep(c("treat", "control"), 20) strata <- rep(c("stratum1", "stratum2"), times = c(20, 20)) # Calculate common risk difference ci_rel_risk_cmh_strata(x = responses, by = arm, strata = strata)# Generate binary samples with strata responses <- expand(c(9, 3, 7, 2), c(10, 10, 10, 10)) arm <- rep(c("treat", "control"), 20) strata <- rep(c("stratum1", "stratum2"), times = c(20, 20)) # Calculate common risk difference ci_rel_risk_cmh_strata(x = responses, by = arm, strata = strata)
Converts count data (number of successes and total sample size) into a binary vector of TRUE/FALSE values. This is useful for converting summary statistics back into raw data format for analysis functions that require individual-level data.
expand(x, n)expand(x, n)
x |
Integer (or vector of integers) representing the number of successes. |
n |
Integer (or vector of integers) representing the total number of participants. |
For each pair of values in x and n, the function creates a vector with x TRUE values
followed by n-x FALSE values. If multiple pairs are provided, the resulting vectors are
concatenated in order.
A logical vector where TRUE represents a success and FALSE represents a failure. The length of the vector equals the sum of all sample sizes.
# Convert 4 successes out of 13 participants to binary data expand(4, 13) # Convert multiple groups of data # Group 1: 9 successes out of 10 # Group 2: 3 successes out of 10 expand(c(9, 3), c(10, 10))# Convert 4 successes out of 13 participants to binary data expand(4, 13) # Convert multiple groups of data # Group 1: 9 successes out of 10 # Group 2: 3 successes out of 10 expand(c(9, 3), c(10, 10))