freqCI: Confidence Intervals for Relative Frequencies

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/freqCI.R

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

1
2
3
4
5
6
7
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 table or as.table)

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.

Details

ref to the book

Value

freqCI() returns an object of class 'freqCI' as a list:

call

the function call issued

x

the original object

level

the confidence levels

freq

a numeric vector of frequencies

n

the number of observations

rel_freq

relative frequencies

cat_names

category names

CIs_low

lower confidence interval boundary/boundaries

CIs_high

upper 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.

Author(s)

Marco J. Maier

See Also

table, as.table, barplot

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# 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)

Example output

Loading required package: grid
mydata
  a   b   c 
100 200 300 
mydata
       a        b        c 
16.66667 33.33333 50.00000 
Warning: "x" was supplied as a "character" vector. A "factor"-type variable was assumed.
      0.5%     2.5% Estimate    97.5%    99.5%
a    12.75    13.68    16.67    19.65    20.59
b    28.38    29.56    33.33    37.11    38.29
c    44.74    46.00    50.00    54.00    55.26
     0.005    0.025 Estimate    0.975    0.995
a    0.127    0.137    0.167    0.196    0.206
b    0.284    0.296    0.333    0.371    0.383
c    0.447    0.460    0.500    0.540    0.553
      0.005     0.025  Estimate     0.975     0.995
a 0.1274767 0.1368468 0.1666667 0.1964866 0.2058567
b 0.2837615 0.2956138 0.3333333 0.3710529 0.3829052
c 0.4474211 0.4599924 0.5000000 0.5400076 0.5525789
[1] 0.7 1.9 3.1

REdaS documentation built on May 2, 2019, 2:48 p.m.

Related to freqCI in REdaS...