Learn R Programming

REdaS (version 0.9.1)

Confidence-Intervals-for-Frequencies: Confidence Intervals for Relative Frequencies

Description

This function computes (one or more) confidence intervals (CIs) for a vector of observations or a table object and returns an object of class 'freqCI' to draw a bar plot of the results.

Usage

freqCI(x, level = 0.95)

## S3 method for class 'freqCI': print(x, percent = TRUE, digits, ...)

## S3 method for class 'freqCI': barplot(height, percent = TRUE, ...)

Arguments

x
must either be a numeric or factor object of individual observations (character vectors are also accepted, but a warning is issued) or an object of class 'table' of frequencies (produced using
level
a numeric vector of confidence levels in $(0,\,1)$.
percent
if TRUE, all values are printed as percentages, else relative frequencies are printed.
digits
the number of digits to print (default to 2 if values are represented as percents or 4 if relative frequencies are used.
height
to plot the proportions and confidence intervals, an object of class 'freqCI' must be used with the generic barplot function.
...
further arguments.

Value

  • freqCI() returns an object of class 'freqCI' as a list:
  • callthe function call issued
  • xthe original object
  • levelthe confidence levels
  • freqa numeric vector of frequencies
  • nthe number of observations
  • rel_freqrelative frequencies
  • cat_namescategory names
  • CIs_lowlower confidence interval boundary/boundaries
  • CIs_highupper confidence interval boundary/boundaries
  • print.freqCI() invisibly returns a matrix with the confidence intervals and estimates.

    barplot.freqCI() invisibly returns a vector with the $x$-coordinates of the plotted bars.

encoding

UTF-8

Details

ref to the book

See Also

table, as.table, barplot

Examples

Run this code
# generate some simple data using rep() and inspect them using table()
mydata <- rep(letters[1:3], c(100,200,300))
table(mydata)
100 * prop.table(table(mydata))

# compute 95% and 99% confidence intervals and print them with standard settings
res <- freqCI(mydata, level = c(.95, .99))
res

# print the result as relative frequencies rounded to 3 digits, save the result
# and print the invisibly returned matrix
resmat <- print(res, percent = FALSE, digits = 3)
resmat

# plot the results and save the x-coordinates
x_coo <- barplot(res)
x_coo

# use the x-coordinates to plot the frequencies per category
text(x_coo, 0, labels = paste0("n = ", res$freq), pos = 3)

Run the code above in your browser using DataLab