# Using ‘confintr’

## Overview

{confintr} is dedicated to confidence intervals (CI). The following parameters are covered:

- mean (Student, Wald, bootstrap),
- proportion (Wilson, Clopper-Pearson, Agresti-Coutts, bootstrap),
- median and other quantiles (distribution-free binomial and bootstrap),
- variance and standard deviation (chi-squared, bootstrap),
- IQR and MAD (bootstrap only),
- skewness and kurtosis (bootstrap only),
- R-squared and the non-centrality parameter of the F distribution (parametric),
- Cramér’s V and the non-centrality parameter of the chi-squared distribution (parametric and bootstrap),
- the odds ratio of a 2x2 table (exact),
- Pearson-, Spearman-, Kendall correlation coefficients (normal for Pearson, bootstrap for any),
- Mean, quantile and median differences of two samples (for quantile/median only bootstrap).

Many of the classic CIs on this list are discussed in Smithson (2003).

In line with the {boot} backend, the following bootstrap CIs are (usually) available:

1. Normal (“norm”) bootstrap CI: This is the Wald/Student CI using bootstrap standard error and bootstrap bias correction. Simple, but only first-order accurate, and not transformation respecting.

2. Percentile (“perc”) bootstrap CI: Uses quantiles of the bootstrap distribution as confidence limits. Simple, but only first-order accurate. Transformation respecting.

3. Basic (“basic”) or reverse bootstrap CI: Flipped version of the percentile approach, dealing with bias but at the price of having very unnaturally tailed sampling distributions. Only first-order accurate.

4. Bias-corrected and accelerated (“bca”) CI: Refined version of the percentile bootstrap. Second-order accurate and transformation respecting. Needs more replications than observations. The default (except for the mean and the mean difference, see below).

5. Student-t (“stud”) bootstrap CI: Refined version of the normal bootstrap that replaces the Student quantile by a custom quantile obtained from bootstrapping the variance of the statistic. second-order accurate but not transformation respecting. Requires a formula for the variance of the estimator, which {confintr} provides for the mean, the mean difference, the variance (and standard deviation) as well as for the proportion. Used as the default for the mean and the mean difference.

For details on bootstrap CIs, we refer to Efron and Tibshirani (1993).

## Installation

    # From CRAN
    install.packages("confintr")

    # Development version
    devtools::install_github("mayer79/confintr")

## Usage

    library(confintr)

    set.seed(1)

    # Mean
    ci_mean(1:100)
    #>
    #>  Two-sided 95% t confidence interval for the population mean
    #>
    #> Sample estimate: 50.5
    #> Confidence interval:
    #>     2.5%    97.5%
    #> 44.74349 56.25651

    ci_mean(1:100, type = "bootstrap")
    #>
    #>  Two-sided 95% bootstrap confidence interval for the population mean
    #>  based on 9999 bootstrap replications and the student method
    #>
    #> Sample estimate: 50.5
    #> Confidence interval:
    #>     2.5%    97.5%
    #> 44.72913 56.34685

    # 95% value at risk
    ci_quantile(rexp(1000), q = 0.95)
    #>
    #>  Two-sided 95% binomial confidence interval for the population 95%
    #>  quantile
    #>
    #> Sample estimate: 3.054989
    #> Confidence interval:
    #>     2.5%    97.5%
    #> 2.745526 3.499928

    # IQR
    ci_IQR(rexp(100))
    #>
    #>  Two-sided 95% bootstrap confidence interval for the population IQR
    #>  based on 9999 bootstrap replications and the bca method
    #>
    #> Sample estimate: 1.042259
    #> Confidence interval:
    #>      2.5%     97.5%
    #> 0.8753326 1.3895169

    # Correlation
    ci_cor(iris[1:2], method = "spearman", type = "bootstrap")
    #>
    #>  Two-sided 95% bootstrap confidence interval for the true Spearman
    #>  correlation coefficient based on 9999 bootstrap replications and the
    #>  bca method
    #>
    #> Sample estimate: -0.1667777
    #> Confidence interval:
    #>         2.5%        97.5%
    #> -0.305510208 -0.005814712

    # Proportions
    ci_proportion(10, n = 100, type = "Wilson")
    #>
    #>  Two-sided 95% Wilson confidence interval for the true proportion
    #>
    #> Sample estimate: 0.1
    #> Confidence interval:
    #>       2.5%      97.5%
    #> 0.05522914 0.17436566

    ci_proportion(10, n = 100, type = "Clopper-Pearson")
    #>
    #>  Two-sided 95% Clopper-Pearson confidence interval for the true
    #>  proportion
    #>
    #> Sample estimate: 0.1
    #> Confidence interval:
    #>       2.5%      97.5%
    #> 0.04900469 0.17622260

    # R-squared
    fit <- lm(Sepal.Length ~ ., data = iris)
    ci_rsquared(fit, probs = c(0.05, 1))
    #>
    #>  One-sided 95% F confidence interval for the population R-squared
    #>
    #> Sample estimate: 0.8673123
    #> Confidence interval:
    #>        5%      100%
    #> 0.8312405 1.0000000

    # Kurtosis
    ci_kurtosis(1:100)
    #>
    #>  Two-sided 95% bootstrap confidence interval for the population kurtosis
    #>  based on 9999 bootstrap replications and the bca method
    #>
    #> Sample estimate: 1.79976
    #> Confidence interval:
    #>     2.5%    97.5%
    #> 1.585133 2.050132

    # Mean difference
    ci_mean_diff(10:30, 1:15)
    #>
    #>  Two-sided 95% t confidence interval for the population value of
    #>  mean(x)-mean(y)
    #>
    #> Sample estimate: 12
    #> Confidence interval:
    #>      2.5%     97.5%
    #>  8.383547 15.616453

    ci_mean_diff(10:30, 1:15, type = "bootstrap")
    #>
    #>  Two-sided 95% bootstrap confidence interval for the population value of
    #>  mean(x)-mean(y) based on 9999 bootstrap replications and the student
    #>  method
    #>
    #> Sample estimate: 12
    #> Confidence interval:
    #>     2.5%    97.5%
    #>  8.32796 15.66603

    # Median difference
    ci_median_diff(10:30, 1:15)
    #>
    #>  Two-sided 95% bootstrap confidence interval for the population value of
    #>  median(x)-median(y) based on 9999 bootstrap replications and the bca
    #>  method
    #>
    #> Sample estimate: 12
    #> Confidence interval:
    #>  2.5% 97.5%
    #>     5    17

## References

Efron, Bradley, and Robert J. Tibshirani. 1993. An Introduction to the Bootstrap. Monographs on Statistics and Applied Probability 57. Boca Raton, Florida, USA: Chapman & Hall/CRC.
Smithson, Michael. 2003. Confidence Intervals. Quantitative Applications in the Social Sciences. SAGE Publications, New York.