ci.mean: (Bootstrap) Confidence Intervals for Arithmetic Means and...

View source: R/ci.mean.R

ci.meanR Documentation

(Bootstrap) Confidence Intervals for Arithmetic Means and Medians

Description

The function ci.mean computes and plots confidence intervals for arithmetic means with known or unknown population standard deviation or population variance and the function ci.median computes confidence intervals for medians, optionally by a grouping and/or split variable. These functions also supports six 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.mean(data, ..., sigma = NULL, sigma2 = NULL, adjust = FALSE,
        boot = c("none", "norm", "basic", "stud", "perc", "bc", "bca"),
        nrep = 1000, seed = NULL, sample = TRUE,
        alternative = c("two.sided", "less", "greater"),
        conf.level = 0.95, group = NULL, split = NULL, sort.var = FALSE,
        na.omit = FALSE, digits = 2, as.na = NULL,
        plot = c("none", "ci", "boot"), hist = TRUE, density = TRUE,
        point = TRUE, ci = TRUE, line = TRUE, filename = NULL,
        width = NA, height = NA, dpi = 600, write = NULL, append = TRUE,
        check = TRUE, output = TRUE)

ci.median(data, ..., boot = c("none", "norm", "basic", "stud", "perc", "bc", "bca"),
          nrep = 1000, seed = NULL, sample = TRUE,
          alternative = c("two.sided", "less", "greater"),
          conf.level = 0.95, group = NULL, split = NULL, sort.var = FALSE,
          na.omit = FALSE, digits = 2, as.na = NULL,
          plot = c("none", "ci", "boot"), hist = TRUE, density = TRUE,
          point = TRUE, ci = TRUE, line = TRUE, 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, i.e., factors and character variables are excluded from data before conducting the analysis.

...

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

sigma

a numeric vector indicating the population standard deviation when computing confidence intervals for the arithmetic mean with known standard deviation Note that either argument sigma or argument sigma2 is specified and it is only possible to specify one value for the argument sigma even though multiple variables are specified in data.

sigma2

a numeric vector indicating the population variance when computing confidence intervals for the arithmetic mean with known variance. Note that either argument sigma or argument sigma2 is specified and it is only possible to specify one value for the argument sigma2 even though multiple variables are specified in data.

adjust

logical: if TRUE, difference-adjustment for the confidence intervals for the arithmetic mean (Baguley, 2012) is applied.

boot

a character string specifying the type of bootstrap confidence intervals (CI), i.e., "none" (default) for not conducting bootstrapping, "norm" for the bias-corrected normal approximation bootstrap CI, "basic" for the basic bootstrap CI, "stud" for the studentized bootstrap CI, "perc", for the percentile bootstrap CI "bc" 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.

sample

logical: if TRUE (default), the univariate sample skewness and kurtosis is computed, while the population skewness and kurtosis is computed when sample = FALSE.

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. The grouping variable is excluded from the data frame specified in data. Notethat a grouping variable can only be used when computing confidence intervals with unknown population standard deviation and population variance.

split

either a character string indicating the variable name of the split variable in data, or a vector representing the split variable. The split variable is excluded from the data frame specified in data.Note that a grouping variable can only be used when computing confidence intervals with unknown population standard deviation and population variance.

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 arithmetic mean or median, "boot" for displaying bootstrap samples with histograms and density curves when the argument "boot" is other than "none".

hist

logical: if TRUE (default), histograms are drawn when plotting bootstrap samples (plot = "boot").

density

logical: if TRUE (default), density curves are drawn when plotting bootstrap samples (plot = "boot").

point

logical: if TRUE (default), vertical lines representing the point estimate of the arithmetic mean or median are drawn when plotting bootstrap samples (plot = "boot").

ci

logical: if TRUE (default), vertical lines representing the bootstrap confidence intervals of the arithmetic mean or median are drawn when plotting bootstrap samples (plot = "boot").

line

logical: if TRUE, a horizontal line is drawn when plot = "ci" or a vertical line is drawn when 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.

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 arithmetic mean of median 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 takuya.yanagida@univie.ac.at

References

Baguley, T. S. (2012). Serious stats: A guide to advanced statistics for the behavioral sciences. Palgrave Macmillan.

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.

See Also

test.z, test.t, ci.mean.diff, ci.cor, ci.prop, ci.var, ci.sd, descript

Examples

#————————————————————————————————————————————————————————————————————————————
# Confidence Interval (CI) for the Arithmetic Mean

# Example 1a: Two-Sided 95% CI
ci.mean(mtcars)

# Example 1b: Two-Sided 95% Difference-Adjusted CI
ci.mean(mtcars, adjust = TRUE)

# Example 1c: Two-Sided 95% CI with known population standard deviation
ci.mean(mtcars, mpg, sigma = 6)

# Alternative specification without using the '...' argument
ci.mean(mtcars$mpg, sigma = 6)

#————————————————————————————————————————————————————————————————————————————
# Confidence Interval (CI) for the Median

# Example 2a: Two-Sided 95% CI
ci.median(mtcars)

# Example 2b: One-Sided 99% CI
ci.median(mtcars, alternative = "less", conf.level = 0.99)

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

# Example 3a: Bias-corrected (BC) percentile bootstrap CI
ci.mean(mtcars, boot = "bc")

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

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

# Example 4a: Grouping variable
ci.mean(mtcars, mpg, cyl, disp, group = "vs")

# Alternative specification without using the '...' argument
ci.mean(mtcars[, c("mpg", "cyl", "disp")], group = mtcars$vs)

# Example 4b: Split variable
ci.mean(mtcars, mpg, cyl, disp, split = "am")

# Alternative specification
ci.mean(mtcars[, c("mpg", "cyl", "disp")], split = mtcars$am)

# Example 4c: Grouping and split variable
ci.mean(mtcars, mpg, cyl, disp, group = "vs", split = "am")

# Alternative specification
ci.mean(mtcars[, c("mpg", "cyl", "disp")], group = mtcars$vs, split = mtcars$am)

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

# Example 5a: Two-Sided 95
ci.mean(mtcars, disp, hp, plot = "ci")

# Example 5b: Grouping variable
ci.mean(mtcars, disp, hp, group = "vs", plot = "ci")

# Example 5c: Split variable
ci.mean(mtcars, disp, hp, split = "am", plot = "ci")

# Example 5d: Plot results using the plot() function, use additional arguments
# see Details in the help page of the function plot.misty.object
object <- ci.mean(mtcars, disp, hp, plot = "ci")
plot(object, ybreaks = seq(0, 300, by = 50), title = "Confidence Intervals")

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

# Example 6a: Two-Sided 95
ci.mean(mtcars, disp, hp, boot = "bc", plot = "boot")

# Example 6b: Grouping variable
ci.mean(mtcars, disp, hp, group = "vs", boot = "bc", plot = "boot")

# Example 6c: Split variable
ci.mean(mtcars, disp, hp, split = "am", boot = "bc", plot = "boot")

# 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.mean(mtcars, disp, hp, boot = "bc", plot = "boot")
plot(object, fill = "gray30", title = "Bootstrap Samples")

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

# Example 7a: Write results into a text file
ci.mean(mtcars, write = "CI_Mean_Text.txt")

# Example 7b: Write results into an Excel file
ci.mean(mtcars, write = "CI_Mean_Excel.xlsx")

# Example 7ce: Save plot as PNG file
ci.mean(mtcars, disp, hp, plot = "ci", filename = "CI_Mean.png",
        width = 9, height = 6)

## End(Not run)

misty documentation built on Aug. 2, 2026, 9:06 a.m.

Related to ci.mean in misty...