getConfidenceInterval: Get a confidence interval for a summary statistic of a...

View source: R/statistics.R

getConfidenceIntervalR Documentation

Get a confidence interval for a summary statistic of a numeric vector.

Description

Calculate or bootstrap a confidence interval.

Usage

getConfidenceInterval(
  data,
  variance = stats::var(data, na.rm = TRUE),
  conf.level = 0.95,
  method = "t-distr",
  resamples = 1000,
  FUN = mean
)

Arguments

data

A numeric vector.

variance

Set the variance (e.g. when the population variance is known). Only used for method='t-distr'.

conf.level

Set the confidence level (.50 < conf.level < 1.00, default: 0.95).

method

One of 't-distr' or 'bootstrap' to either use the sample t-distribution to _calculate_ the confidence interval or to bootstrap it by resampling from the sample in 'data'. Using the sample t-distribution can be much faster and allows overriding the sample variance, and according to the central limit theorem, the distribution of sample means is normal, so that is fine. Bootstrapping is slower but works on any data distribution and allows other descriptors like the median, or variance by setting FUN.

resamples

The number of samples to draw with replacement from the data to bootstrap values of the descriptor.

FUN

The function to use as descriptor for every sample when bootstrapping.

Details

Tell a story here.

Value

A vector with the upper and lower bound of the confidence interval.

Examples

# for normally distributed data, using a t-interval is fine:
normal <- rnorm(1000)
getConfidenceInterval(normal)
getConfidenceInterval(normal, variance=1)

# but perhaps we want to bootstrap it for other distributions:
exponential <- rexp(1000)
getConfidenceInterval(exponential)
getConfidenceInterval(exponential,method='bootstrap')


thartbm/handlocs documentation built on Feb. 18, 2025, 10:53 p.m.