ci.prop: (Bootstrap) Confidence Intervals for Proportions

View source: R/ci.prop.R

ci.propR Documentation

(Bootstrap) Confidence Intervals for Proportions

Description

This function computes and plots confidence intervals for proportions, optionally by a grouping and/or split variable. The function also supports three types of bootstrap confidence intervals (e.g., bias-corrected (BC) percentile bootstrap or bias-corrected and accelerated (BCa) bootstrap confidence intervals) and plots the bootstrap samples with histograms and density curves.

Usage

ci.prop(data, ..., method = c("wald", "wilson"),
        boot = c("none", "perc", "bc", "bca"), nrep = 1000, seed = NULL,
        alternative = c("two.sided", "less", "greater"), conf.level = 0.95,
        group = NULL, split = NULL, sort.var = FALSE, na.omit = FALSE,
        digits = 3, as.na = NULL, plot = c("none", "ci", "boot"),
        geom = c("all", "hist", "density", "point", "ci", "line"),
        filename = NULL, width = NA, height = NA, dpi = 600,
        write = NULL, append = TRUE, check = TRUE, output = TRUE)

Arguments

data

a numeric vector or data frame with numeric variables with 0 and 1 values.

...

an expression indicating the variable names in data, e.g., ci.prop(dat, x1, x2, x3). Note that the operators +, -, ~, :, ::, and ! can also be used to select variables, see 'Details' in the df.subset function.

method

a character string specifying the method for computing the confidence interval, must be one of "wald", or "wilson" (default).

boot

a character string specifying the type of bootstrap confidence intervals (CI), i.e., "none" (default) for not conducting bootstrapping, "perc", for the percentile bootstrap CI "bc" (default) for the bias-corrected (BC) percentile bootstrap CI (without acceleration), and "bca" for the bias-corrected and accelerated (BCa) bootstrap CI, see 'Details' in the ci.cor function.

nrep

a numeric value indicating the number of bootstrap replicates (default is 1000).

seed

a numeric value specifying seeds of the pseudo-random numbers used in the bootstrap algorithm when conducting bootstrapping.

alternative

a character string specifying the alternative hypothesis, must be one of "two.sided" (default), "greater" or "less".

conf.level

a numeric value between 0 and 1 indicating the confidence level of the interval.

group

either a character string indicating the variable name of the grouping variable in data, or a vector representing the grouping variable.

split

either a character string indicating the variable name of the split variable in data, or a vector representing the split variable.

sort.var

logical: if TRUE, output table is sorted by variables when specifying group.

na.omit

logical: if TRUE, incomplete cases are removed before conducting the analysis (i.e., listwise deletion) when specifying more than one outcome variable.

digits

an integer value indicating the number of decimal places to be used.

as.na

a numeric vector indicating user-defined missing values, i.e. these values are converted to NA before conducting the analysis. Note that as.na() function is only applied to data, but not to group or split.

plot

a character string indicating the type of the plot to display, i.e., "none" (default) for not displaying any plots, "ci" for displaying confidence intervals for the proportion, "boot" for displaying bootstrap samples with histograms and density curves when the argument "boot" is other than "none".

geom

a character string or character vector indicating which plot elements to draw, i.e., "all" for drawing all plot elements, "hist" for drawing histograms when plotting bootstrap samples (plot = "boot"), "density" for drawing density curves when plotting bootstrap samples (plot = "boot"), "point" for drawing vertical lines representing the point estimate of the proportion when plotting bootstrap samples (plot = "boot"), "ci" for drawing vertical lines representing the bootstrap confidence intervals of the proportion when plotting bootstrap samples (plot = "boot"), and "line" for drawing a horizontal line when specifying plot = "ci" or a vertical line when specifying plot = "boot".

filename

a character string indicating the filename argument including the file extension in the ggsave function. Note that one of ".eps", ".ps", ".tex", ".pdf" (default), ".jpeg", ".tiff", ".png", ".bmp", ".svg" or ".wmf" needs to be specified as file extension in the file argument. Note that plots can only be saved when plot = "ci" or plot = "boot".

width

a numeric value indicating the width argument (default is the size of the current graphics device) in the ggsave function.

height

a numeric value indicating the height argument (default is the size of the current graphics device) in the ggsave function.

dpi

a numeric value indicating the dpi argument (default is 600) in the ggsave function.

write

a character string naming a file for writing the output into either a text file with file extension ".txt" (e.g., "Output.txt") or Excel file with file extension ".xlsx" (e.g., "Output.xlsx"). If the file name does not contain any file extension, an Excel file will be written.

append

logical: if TRUE (default), output will be appended to an existing text file with extension .txt specified in write, if FALSE existing text file will be overwritten.

check

logical: if TRUE (default), argument specification is checked.

output

logical: if TRUE (default), output is shown on the console.

Details

The Wald confidence interval which is based on the normal approximation to the binomial distribution are computed by specifying method = "wald", while the Wilson (1927) confidence interval (aka Wilson score interval) is requested by specifying method = "wilson". By default, Wilson confidence interval is computed which have been shown to be reliable in small samples of n = 40 or less, and larger samples of n > 40 (Brown, Cai & DasGupta, 2001), while the Wald confidence intervals is inadequate in small samples and when p is near 0 or 1 (Agresti & Coull, 1998).

Value

Returns an object of class misty.object, which is a list with following entries:

call

function call

type

type of analysis

data

list with the input specified in ..., data, group, and split

args

specification of function arguments

boot

data frame with bootstrap replicates of the aproportion when bootstrapping was requested

plot

ggplot2 object for plotting the results and the data frame used for plotting

result

result table

Note

Bootstrap confidence intervals are computed using the R package boot by Angelo Canty and Brain Ripley (2024).

Author(s)

Takuya Yanagida

References

Agresti, A. & Coull, B.A. (1998). Approximate is better than "exact" for interval estimation of binomial proportions. American Statistician, 52, 119-126.

Brown, L. D., Cai, T. T., & DasGupta, A., (2001). Interval estimation for a binomial proportion. Statistical Science, 16, 101-133.

Canty, A., & Ripley, B. (2024). boot: Bootstrap R (S-Plus) Functions. R package version 1.3-31.

Rasch, D., Kubinger, K. D., & Yanagida, T. (2011). Statistics in psychology - Using R and SPSS. John Wiley & Sons.

Wilson, E. B. (1927). Probable inference, the law of succession, and statistical inference. Journal of the American Statistical Association, 22, 209-212.

See Also

ci.prop, ci.prop.diff, ci.median, ci.prop.diff, ci.cor, ci.var, ci.sd, descript

Examples

#————————————————————————————————————————————————————————————————————————————
# Confidence Interval (CI) for Proportions

# Example 1a: Two-Sided 95% CI
ci.prop(mtcars, vs, am)

# Example 1b: Alternative specification without using the '...' argument
ci.prop(mtcars[, c("vs", "am")])

# Example 1c: One-Sided 95% CI using Wald method
ci.prop(mtcars, vs, am, method = "wald", alternative = "less")

#————————————————————————————————————————————————————————————————————————————
# Grouping Variable

# Example 2a: Grouping variable
ci.prop(mtcars, vs, group = "am")

# Example 2b: Alternative specification without using the '...' argument
ci.prop(mtcars$vs, group = mtcars$am)

# Example 2c: Sort result table by variables
ci.prop(mtcars, vs, am, group = "gear", sort.var = TRUE)

#————————————————————————————————————————————————————————————————————————————
# Split Variable

# Example 3a: Split variable
ci.prop(mtcars, vs, split = "am")

# Example 3b: Alternative specification without using the '...' argument
ci.prop(mtcars$vs, split = mtcars$am)

#————————————————————————————————————————————————————————————————————————————
# Grouping and Split Variable

# Example 4a: Grouping and split variable
ci.prop(mtcars, vs, group = "am", split = "cyl")

# Example 4b: Alternative specification without using the '...' argument
ci.prop(mtcars$vs, group = mtcars$am, split = mtcars$cyl)

# Example 4c: Sort result tables by variables
ci.prop(mtcars, vs, am, group = "gear", split = "cyl", sort.var = TRUE)

## Not run: 
#————————————————————————————————————————————————————————————————————————————
# Bootstrap Confidence Interval (CI)

# Example 5a: Bias-corrected (BC) percentile bootstrap CI
ci.prop(mtcars, vs, am, boot = "bc")

# Example 5b: Bias-corrected and accelerated (BCa) bootstrap CI,
# 5000 bootstrap replications, set seed of the pseudo-random number generator
ci.prop(mtcars, vs, am, boot = "bca", nrep = 5000, seed = 42)

#————————————————————————————————————————————————————————————————————————————
# Plot Confidence Intervals

# Example 6a: Two-Sided 95
ci.prop(mtcars, vs, am, plot = "ci")

# Example 6b: Grouping variable
ci.prop(mtcars, vs, am, group = "am", plot = "ci")

# Example 6c: Split variable
ci.prop(mtcars, vs, am, split = "am", plot = "ci")

# Example 6d: Plot results using the plot() function, use additional arguments
# see Details in the help page of the function plot.misty.object
object <- ci.prop(mtcars, vs, am, plot = "ci")
plot(object, ybreaks = seq(0, 1, by = 0.1), title = "Confidence Intervals")

#————————————————————————————————————————————————————————————————————————————
# Plot Bootstrap Samples

# Example 7a: Two-Sided 95
ci.prop(mtcars, vs, am, boot = "bc", plot = "boot")

# Example 7b: Grouping variable
ci.prop(mtcars, vs, am, group = "am", boot = "bc", plot = "boot")

# Example 7c: Split variable
ci.prop(mtcars, vs, am, split = "am", boot = "bc", plot = "boot")

# Example 7d: Plot results using the plot() function, use additional arguments
# see Details in the help page of the function plot.misty.object
object <- ci.prop(mtcars, vs, am, boot = "bc", plot = "boot")
plot(object, fill = "gray30", title = "Bootstrap Samples")

#————————————————————————————————————————————————————————————————————————————
# Write Results and Save Plot

# Example 8a: Write results into a text file
ci.prop(mtcars, vs, am, write = "CI_Prop.txt")

# Example 8b: Write results into an Excel file
ci.prop(mtcars, vs, am, write = "CI_Prop.xlsx")

# Example 8c: Save plot as PNG file
ci.prop(mtcars, vs, am, plot = "ci", filename = "CI_Prop.png",
        width = 9, height = 6)

## End(Not run)

misty documentation built on Nov. 24, 2026, 5:06 p.m.

Related to ci.prop in misty...