prop.ci: Proportion confidence interval

View source: R/prop.ci.R

prop.ciR Documentation

Proportion confidence interval

Description

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

Usage

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

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.

Value

a vector with confidence interval

See Also

prop.test

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
  }
}

larmarange/JLutils documentation built on March 24, 2023, 6:39 a.m.