---
title: "Audit sampling: Get started"
author: Koen Derks
date: "`r Sys.Date()`"
output: 
  rmarkdown::html_vignette:
    toc: true
    toc_depth: 3
vignette: >
  %\VignetteIndexEntry{Audit sampling: Get started}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteDepends{jfa}
  %\VignetteKeywords{audit, classical, sampling, workflow}
  %\VignettePackage{jfa}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
library(jfa)
```

## Introduction

Welcome to the 'Audit sampling' vignette of the **jfa** package. Here you can
find a detailed explanation of the functions in the package that facilitate the
statistical audit sampling workflow. Specifically, these functions implement
standard audit sampling techniques to calculate sample sizes, select items from
a population, and evaluate misstatement in a data sample. The **jfa** package
enables users to create a prior probability distribution to perform Bayesian
audit sampling using these functions. For more detailed explanations of each
function, read the other vignettes on the
[package website](https://koenderks.github.io/jfa/).

## Cheat sheet

The cheat sheet below will help you get started with **jfa**'s' intended audit
sampling workflow. You can download a `pdf` version of the cheat sheet
[here](https://github.com/koenderks/jfa/raw/development/man/figures/cheatsheet/cheatsheet.pdf).

<p align='center'><img src='cheatsheet.png' alt='cheatsheet' width='1000'></p>

## Functions and intended usage

<p align='center'><img src='jfaBanner.png' alt='banner'/></p>

Below you can find an explanation of the available functions in **jfa** sorted
by their occurrence in the standard audit sampling workflow.

- [`auditPrior()`](#create-a-prior-distribution-with-auditprior)
- [`planning()`](#plan-a-sample-with-planning)
- [`selection()`](#select-sample-items-with-selection)
- [`evaluation()`](#evaluate-a-sample-with-evaluation)
- [`report()`](#create-a-report-with-report)

### Create a prior distribution with `auditPrior()`

[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)

The `auditPrior()` function is used to specify a prior distribution for Bayesian
audit sampling. The interface allows a complete customization of the prior
distribution as well as a formal translation of pre-existing audit information
into a prior distribution. The function returns an object of class `jfaPrior`
which can be used with associated `summary()` and `plot()` methods. Objects with
class `jfaPrior` can also be used as input for the `prior` argument in other
functions. Moreover, `jfaPrior` objects have a corresponding `predict()`
function to produce the predictions of the prior distribution on the data level.

*Full function with default arguments:*

```r
auditPrior(method = c(
             "default", "strict", "param", "impartial", "hyp",
             "arm", "bram", "sample", "factor", "nonparam"
           ),
           likelihood = c(
             "poisson", "binomial", "hypergeometric",
             "normal", "uniform", "cauchy", "t", "chisq",
             "exponential"
           ),
           N.units = NULL,
           alpha = NULL,
           beta = NULL,
           materiality = NULL,
           expected = 0,
           ir = NULL,
           cr = NULL,
           ub = NULL,
           p.hmin = NULL,
           x = NULL,
           n = NULL,
           factor = NULL,
           samples = NULL,
           conf.level = 0.95)
```

*Supported options for the `method` argument:*

- `default`: Indifferent / noninformative prior distribution.
- `param`: Manually set the prior parameters.
- `strict`: Improper prior distribution (matches classical results).
- `impartial`: Impartial prior distribution (Derks et al., 2022).
- `hyp`: Manually provide the prior probability for tolerable misstatement (Derks et al., 2021).
- `arm`: Manually provide the inherent risk and internal control risk (Derks et al., 2021).
- `bram`: Manually provide the upper bound of the prior distribution (Touw & Hoogduin, 2011).
- `sample`: Manually provide an equivalent prior sample (Derks et al., 2021).
- `factor`: Manually provide and weigh an equivalent prior sample (Derks et al., 2021).
- `nonparam`: Nonparametric prior based on samples.

*Supported options for the `likelihood` argument:*

- `poisson`: Poisson likelihood and conjugate gamma prior distribution (Stewart, 2013).
- `binomial`: Binomial likelihood and conjugate beta prior distribution (Steele, 1992).
- `hypergeometric`: Hypergeometric likelihood and conjugate beta-binomial prior distribution (Dyer & Pierce, 1991).
- `normal`: Normal prior distribution.
- `uniform`: Uniform prior distribution.
- `cauchy`: Cauchy prior distribution.
- `t`: Student-t prior distribution.
- `chisq`: Chi-squared prior distribution.
- `exponential`: Exponential prior distribution.

*Example usage:*

```{r}
# Default beta(1, 1) prior distribution
x <- auditPrior(method = "default", likelihood = "binomial")

# Custom gamma(1, 10) prior distribution
x <- auditPrior(method = "param", likelihood = "poisson", alpha = 1, beta = 10)

# Beta prior distribution incorporating inherent risk (70%) and control risk (50%)
x <- auditPrior(method = "arm", likelihood = "binomial", materiality = 0.05, ir = 0.7, cr = 0.5)

summary(x) # Prints information about the prior distribution
predict(x, n = 20, cumulative = TRUE) # Predictions for a sample of n = 20
```

### Plan a sample with `planning()`

[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)

The `planning()` function is used to calculate a minimum sample size for audit
samples. It allows specification of statistical requirements for the sample with
respect to the performance materiality or the precision. The function returns an
object of class `jfaPlanning` which can be used with associated `summary()` and
`plot()` methods. To perform Bayesian planning, the input for the `prior`
argument can be an object of class `jfaPrior` as returned by the `auditPrior()`
function, or an object of class `jfaPosterior` as returned by the `evaluation()`
function.

*Full function with default arguments:*

```r
planning(materiality = NULL,
         min.precision = NULL,
         expected = 0,
         likelihood = c("poisson", "binomial", "hypergeometric"),
         conf.level = 0.95,
         N.units = NULL,
         by = 1,
         max = 5000,
         prior = FALSE)
```

*Supported options for the `likelihood` argument:*

- `poisson`: Poisson likelihood (Stewart, 2012).
- `binomial`: Binomial likelihood (Stewart, 2012).
- `hypergeometric`: Hypergeometric likelihood (Stewart, 2012).

*Example usage:*

```{r}
# Classical planning using the Poisson likelihood
x <- planning(materiality = 0.03, likelihood = "poisson")

# Bayesian planning using a default beta(1, 1) prior and binomial likelihood
x <- planning(materiality = 0.03, likelihood = "binomial", prior = TRUE)

# Bayesian planning using a custom beta(1, 10) prior and binomial likelihood
x <- planning(
  materiality = 0.03,
  prior = auditPrior(method = "param", likelihood = "binomial", alpha = 1, beta = 10)
)

summary(x) # Prints information about the planning
```

### Select sample items with `selection()`

[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)

The `selection()` function is used to perform statistical selection of audit
samples. It offers flexible implementations of the most common audit sampling
algorithms for attributes sampling and monetary unit sampling. The function
returns an object of class `jfaSelection` which can be used with associated
`summary()` and `plot()` methods. The input for the `size` argument can be an
object of class `jfaPlanning` as returned by the `planning()` function.

*Full function with default arguments:*

```r
selection(data,
          size,
          units = c("items", "values"),
          method = c("interval", "cell", "random", "sieve"),
          values = NULL,
          order = NULL,
          decreasing = FALSE,
          randomize = FALSE,
          replace = FALSE,
          start = 1)
```

*Supported options for the `units` argument:*

- `items`: Sampling units are items (rows) (Leslie, Teitlebaum, & Anderson, 1979).
- `values`: Sampling units are monetary units (Leslie, Teitlebaum, & Anderson, 1979).

*Supported options for the `method` argument:*

- `interval`: Select a fixed unit from each interval.
- `cell`: Select a random unit within each interval.
- `random`: Select random units without an interval.
- `sieve`: Select units using modified sieve sampling (Hoogduin, Hall, & Tsay, 2010).

*Example usage:*

```{r}
# Selection using random record (attributes) sampling
x <- selection(data = BuildIt, size = 100, units = "items", method = "random")

# Selection using fixed interval monetary unit sampling (using column 'bookValue' in BuildIt)
x <- selection(
  data = BuildIt, size = 100, units = "values",
  method = "interval", values = "bookValue"
)

summary(x) # Prints information about the selection
```

### Evaluate a sample with `evaluation()`

[![Lifecycle: stable](https://img.shields.io/badge/lifecycle-stable-brightgreen.svg)](https://lifecycle.r-lib.org/articles/stages.html#stable)

The `evaluation()` function takes a sample or summary statistics of the sample
and performs evaluation according to the specified method and sampling
objectives. The function returns an object of class `jfaEvalution` which can
be used with associated `summary()` and `plot()` methods. To perform Bayesian
evaluation, the input for the `prior` argument can be an object of class
`jfaPrior` as returned by the `auditPrior()` function, or an object of class
`jfaPosterior` as returned by the `evaluation()` function.

*Full function with default arguments:*

```r
evaluation(materiality = NULL, 
           method = c(
             "poisson", "binomial", "hypergeometric",
             "stringer", "stringer.meikle", "stringer.lta", "stringer.pvz",
             "rohrbach", "moment", "coxsnell",
             "direct", "difference", "quotient", "regression", "mpu"
           ),
           alternative = c("less", "two.sided", "greater"),
           conf.level = 0.95,
           data = NULL,
           values = NULL,
           values.audit = NULL,
           strata = NULL,
           times = NULL,
           x = NULL,
           n = NULL,
           N.units = NULL,
           N.items = NULL,
           pooling = c("none", "complete", "partial"), 
           prior = FALSE)
```

*Supported options for the `method` argument:*

- `poisson`: Poisson likelihood (Stewart, 2012).
- `binomial`: Binomial likelihood (Stewart, 2012).
- `hypergeometric`: Hypergeometric likelihood (Stewart, 2012).
- `stringer`: Stringer bound (Bickel, 1992).
- `stringer.meikle`: Stringer bound with Meikle's correction (Meikle, 1972).
- `stringer.lta`: Stringer bound with LTA correction (Leslie, Teitlebaum, & Anderson, 1979).
- `stringer.pvz`: Modified Stringer bound (Pap & van Zuijlen, 1996).
- `rohrbach`: Rohrbach's augmented variance estimator (Rohrbach, 1993).
- `moment`: Modified moment bound (Dworin & Grimlund, 1984).
- `coxsnell`: Cox and Snell bound (Cox & Snell, 1979).
- `mpu`: Mean-per-unit estimator (Touw & Hoogduin, 2011).
- `direct`: Direct estimator (Touw & Hoogduin, 2011).
- `difference`: Difference estimator (Touw & Hoogduin, 2011).
- `quotient`: Quotient (ratio) estimator (Touw & Hoogduin, 2011).
- `regression`: Regression estimator (Touw & Hoogduin, 2011).

*Example usage:*

```{r}
# Classical evaluation using the Poisson likelihood (and summary statistics)
x <- evaluation(materiality = 0.03, x = 1, n = 100, method = "poisson")

# Bayesian evaluation using a default minimal information prior (and summary statistics)
x <- evaluation(materiality = 0.03, x = 1, n = 100, method = "poisson", prior = TRUE)

# Bayesian evaluation using a custom beta(1, 10) prior (and summary statistics)
x <- evaluation(
  materiality = 0.03, x = 1, n = 100,
  prior = auditPrior(method = "param", likelihood = "binomial", alpha = 1, beta = 10)
)

summary(x) # Prints information about the evaluation
```

### Create a report with `report()`

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://lifecycle.r-lib.org/articles/stages.html#experimental)

The `report()` function takes an object of class `jfaEvaluation` as returned by
the `evaluation()` function and automatically creates a `html` or `pdf` report
containing the analysis results and their interpretation.

*Full function with default arguments:*

```r
report(object,
       file = "report.html",
       format = c("html_document", "pdf_document"))
```

*Example usage:*

```r
# Generate an automatic report
report(object = x, file = 'myReport.html')
```

For an example report, see the following [link](https://github.com/koenderks/jfa/raw/development/man/figures/readme/report/report.pdf).

## Benchmarks

To validate the statistical results, **jfa**'s automated
[unit tests](https://github.com/koenderks/jfa/tree/development/tests/testthat)
regularly verify the main output from the package against the following
benchmarks:

- [Audit Sampling: Audit Guide](https://www.aicpa-cima.com/cpe-learning/publication/audit-sampling-audit-guide-OPL) (Appendix A and Appendix C)
- [AuditSampler](https://cplusglobal.wordpress.com/solutions/auditsampler-statistical-sampling-software/)
- [EZ-quant](https://www.dcaa.mil/Checklists-Tools/EZ-Quant-Applications/)
- [MUS](https://cran.r-project.org/package=MUS) (R package version 0.1.6)
- [samplingbook](https://cran.r-project.org/package=samplingbook) (R package version 1.2.4)
- [SMASH21 + SMASH21-Bayes](https://steekproeven.eu/)
- [SRA](https://www.sra.nl/vaktechniek/accountancy/controle/praktijkhandreikingen/praktijkhandreiking-steekproeven-sra-steekproefmodel)
- Touw, P., and Hoogduin, L. (2011). *Statistiek voor audit en controlling*. Boom uitgevers, Amsterdam.

## Statistical tables

Below you can find several informative tables that contain statistical sample
sizes, upper limits, one-sided *p* values, and Bayes factors. These tables are
created using the `planning()` and `evaluation()` functions provided in the
package.

*Sample sizes*

- [Sample sizes based on the Poisson distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/pois.ss.pdf)
- [Sample sizes based on the binomial distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/binom.ss.pdf)
- [Sample sizes based on the hypergeometric distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/hyper.ss.pdf)

*Upper limits*

- [Upper limits based on the Poisson distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/pois.ub.pdf)
- [Upper limits based on the binomial distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/binom.ub.pdf)
- [Upper limits based on the hypergeometric distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables//pdf/hyper.ub.pdf)

*One-sided p values*

- [One-sided *p* values based on the Poisson distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/pois.p.pdf)
- [One-sided *p* values based on the binomial distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/binom.p.pdf)
- [One-sided *p* values based on the hypergeometric distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/hyper.p.pdf)

*Bayes factors*

- [Impartial Bayes factors based on the gamma distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/pois.lbfi.pdf)
- [Impartial Bayes factors based on the beta distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/binom.lbfi.pdf)
- [Impartial Bayes factors based on the beta-binomial distribution](https://github.com/koenderks/jfa/raw/development/man/figures/tables/pdf/hyper.lbfi.pdf)

## References

- Bickel, P. J. (1992). Inference and auditing: The Stringer bound. *International Statistical Review*, 60(2), 197–209. - [View online](https://www.jstor.org/stable/1403650)
- Cox, D. R., & Snell, E. J. (1979). On sampling and the estimation of rare errors. *Biometrika*, 66(1), 125-132. - [View online](https://doi.org/10.1093/biomet/66.1.125)
- Derks, K., de Swart, J., van Batenburg, P., Wagenmakers, E.-J., & Wetzels, R. (2021). Priors in a Bayesian audit: How integration of existing information into the prior distribution can improve audit transparency and efficiency. *International Journal of Auditing*, 25(3), 621-636. - [View online](https://doi.org/10.1111/ijau.12240)
- Derks, K., de Swart, J., Wagenmakers, E.-J., & Wetzels, R. (2021). The Bayesian Approach to Audit Evidence: Quantifying Statistical Evidence using the Bayes Factor. *PsyArXiv* - [View online](https://doi.org/10.31234/osf.io/kzqp5)
- Derks, K., de Swart. J., Wagenmakers, E.-J., Wille, J., & Wetzels, R. (2021). JASP for Audit: Bayesian Tools for the Auditing Practice. *Journal of Open Source Software*, 6(68), 2733. - [View online](https://doi.org/10.21105/joss.02733)
- Derks, K., de Swart, J., Wagenmakers, E.-J., & Wetzels, R. (2022). An Impartial Bayesian Hypothesis Test for Audit Sampling. *PsyArXiv* - [View online](https://doi.org/10.31234/osf.io/8nf3e)
- Derks, K., de Swart, J., Wagenmakers, E.-J., & Wetzels, R. (2022). Bayesian Generalized Linear Modeling: How to Incorporate Audit Information into the Statistical Model. *PsyArXiv* - [View online](https://doi.org/10.31234/osf.io/byj2a)
- Dworin, L. D. and Grimlund, R. A. (1984). Dollar-unit sampling for accounts receivable and inventory. *The Accounting Review*, 59(2), 218–241. - [View online](https://www.jstor.org/stable/247296)
- Dyer, D., & Pierce, R. L. (1993). On the choice of the prior distribution in hypergeometric sampling. *Communications in Statistics - Theory and Methods*, 22(8), 2125-2146. - [View online](https://www.tandfonline.com/doi/abs/10.1080/03610929308831139)
- Hoogduin, L. A., Hall, T. W., & Tsay, J. J. (2010). Modified sieve sampling: A method for single-and multi-stage probability-proportional-to-size sampling. *Auditing: A Journal of Practice & Theory*, 29(1), 125-148. - [View online](https://doi.org/10.2308/aud.2010.29.1.125)
- International Auditing and Assurance Standards Board (IAASB). (2018). *Handbook of international quality control, auditing review, other assurance, and related services pronouncements (Vol. I)*. New York: International Federation of Accountants.
- Meikle, G. R. (1972). *Statistical Sampling in an Audit Context*. Canadian Institute of Chartered Accountants.
- Leslie, D. A., Teitlebaum, A. D., & Anderson, R. J. (1979). *Dollar-unit Sampling: A Practical Guide for Auditors*. London: Pitman.
- Pap, G., & van Zuijlen, M. C. (1996). On the asymptotic behaviour of the Stringer bound. *Statistica Neerlandica*, 50(3), 367-389. - [View online](https://doi.org/10.1111/j.1467-9574.1996.tb01503.x)
- Rietveld, C. (1978). De zeefmethode als selectiemethode voor statistische steekproeven in de controlepraktijk (1). *Compact: Computer en Accountant*, 15, 2–11.
- Rohrbach, K. J. (1993). Variance augmentation to achieve nominal coverage probability in sampling from audit populations. *Auditing: A Journal of Practice & Theory*, 12(2), 79-97.
- Steele, A. (1992). *Audit Risk and Audit Evidence: The Bayesian Approach to Statistical Auditing*. San Diego: Academic Press.
- Stewart, T. R. (2012). *Technical Notes on the AICPA Audit Guide Audit Sampling*. American Institute of Certified Public Accountants, New York. - [View online](https://us.aicpa.org/content/dam/aicpa/publications/accountingauditing/keytopics/downloadabledocuments/sampling-guide-technical-notes.pdf)
- Stewart, T. R. (2013). *A Bayesian Audit Assurance Model with Application to the Component Materiality problem in Group Audits.* VU University, Amsterdam. - [View online](https://research.vu.nl/en/publications/a-bayesian-audit-assurance-model-with-application-to-the-componen)
- Talens, E. (2005). *Statistical Auditing and the AOQL-method*. University of Groningen, Groningen. - [View online](https://research.rug.nl/en/publications/statistical-auditing-and-the-aoql-method)
- Touw, P., and Hoogduin, L. (2011). *Statistiek voor Audit en Controlling*. Boom uitgevers, Amsterdam.
- Weiler, H. (1965). The use of incomplete beta functions for prior distributions in binomial sampling. *Technometrics*, 7(3), 335-347. - [View online](https://www.tandfonline.com/doi/abs/10.1080/00401706.1965.10490267)
