Provide a convenient wrapper for prop.test to compute confidence intervals for proportions

prop.ci(x, n = NULL, bounds = 1:2, ...)

prop.ci.lower(x, n = NULL, ...)

prop.ci.upper(x, n = NULL, ...)

Arguments

x

a factor vector, a logical vector, a one-dimensional table or a vector of count of successes (in that case, n should be provided)

n

a vector of count of trials (x should be a vector of successes in that case)

bounds

1:2 for lower and upper bounds, 1 for lower bounds only, 2 for upper bound only.

...

additional parameters used by prop.test

Value

a vector with confidence interval

Details

prop.ci.lower is a wrapper for prop.ci with bounds = 1 and prop.ci.upper for prop.ci with bounds = 2.

When x and n are provided, they should be of same length. Alternativaly, n could be a single integer that will be used for each element of x.

When x and n are provided, if x contains several elements, bounds should be 1 or 2 but can't be 1:2. In that scenario, the returned vector will contain the corresponding lower or the upper confidence interval bound for each element of x.

The confidence level could be specify with conf.level (0.95 by default).

By default, NA value are removed.

See also

Examples

if (require(questionr)) {
  data(hdv2003)
  d <- hdv2003

  freq(d$sport)
  prop.ci(d$sport)
  prop.ci.lower(d$sport)
  prop.ci.upper(d$sport)
  prop.ci(d$sport, conf.level = 0.9)
  prop.ci(table(d$sport))
  prop.ci(d$sport == "Non")
  prop.ci(d$sport == "Oui")

  prop.ci.lower(c(1277, 723), n = 2000)
  prop.ci.upper(c(1277, 723), n = 2000)

  if (require(data.table)) {
    d <- as.data.table(d)
    res <- d[, .(freq = .N), by = .(sexe, sport)]
    res[, n := sum(freq), by = sexe]
    res[, prop := freq / n]
    res[, prop.l := prop.ci.lower(freq, n)]
    res[, prop.h := prop.ci.upper(freq, n)]
    res
  }
}
#> Loading required package: questionr
#> Loading required package: data.table
#>     sexe sport freq    n      prop    prop.l    prop.h
#> 1: Femme   Non  747 1101 0.6784741 0.6498433 0.7058435
#> 2: Femme   Oui  354 1101 0.3215259 0.2941565 0.3501567
#> 3: Homme   Oui  369  899 0.4104561 0.3782002 0.4434870
#> 4: Homme   Non  530  899 0.5895439 0.5565130 0.6217998