---
title: "Audit sample evaluation"
output: 
  html_document:
    theme: readable
---

```{r, include = FALSE}
colorize <- function(x, color) {
  if (knitr::is_latex_output()) {
    sprintf("\\textcolor{%s}{%s}", color, x)
  } else if (knitr::is_html_output()) {
    sprintf(
      "<span style='color: %s;'>%s</span>", color,
      x
    )
  } else {
    x
  }
}
```

```{css, echo=FALSE}
.math {
font-size: small;
}
#header {
background: rgb(34, 63, 135);
padding: 1em;
color: rgb(255, 255, 255);
}
```

---

<img src="jfaLogo.png" width="133" height="154" alt="logo" align="right" margin-left="20" margin-right="20"/>

This report was created by `r version$version.string` and its package **jfa** (version `r utils::packageVersion("jfa")`). 
**jfa** is free software and you can redistribute it and or modify it under the terms of the GNU GPL-3 as published by the 
Free Software Foundation. The package is distributed in the hope that it will be useful, but without any warranty; without 
even the implied warranty of merchantability of fitness for a particular purpose.

---

### Results

```{r echo = FALSE}
if (!is.null(object[["prior"]])) {
  inttype <- "Credible"
} else {
  inttype <- "Confidence"
}
df <- data.frame(n = object[["n"]])
if (!is.null(object[["N.units"]])) {
  df <- cbind(N = object[["N.units"]], df)
}
df$x <- object[["x"]]
df$t <- object[["t"]]
df$mle <- object[["mle"]]
df$int <- paste0("[", round(object[["lb"]], 3), "; ", round(object[["ub"]], 3), "]")
df$precision <- object[["precision"]]
if (object[["materiality"]] < 1) {
  if (!is.null(object[["prior"]])) {
    df$stat <- object[["posterior"]][["hypotheses"]]$bf.h1
  } else {
    df$stat <- object[["p.value"]]
  }
}
if (!is.null(object[["strata"]])) {
  rows <- data.frame(n = object[["strata"]][, "n"])
  if (!is.null(object[["N.units"]])) {
    rows <- cbind(rows, N = object[["strata"]][, "N"])
  }
  rows <- cbind(rows, object[["strata"]][, c("x", "t", "mle")])
  rows$int <- paste0("[", round(object[["strata"]]$lb, 3), "; ", round(object[["strata"]]$ub, 3), "]")
  rows$precision <- object[["strata"]][, "precision"]
  if (object[["materiality"]] < 1) {
    if (!is.null(object[["prior"]])) {
      rows$stat <- object[["strata"]][, "bf10"]
    } else {
      rows$stat <- object[["strata"]][["p.value"]]
    }
  }
  df <- rbind(df, rows)
  rownames(df) <- c("Population", rownames(object[["strata"]]))
} else {
  rownames(df) <- "Population"
}
if (object[["materiality"]] < 1) {
  if (!is.null(object[["prior"]])) {
    col_names <- c(
      "Samples", "Misstatements", "Taint", "Estimate",
      paste0(round(object[["conf.level"]] * 100, 2), "% ", inttype, " interval\u00b9"), "Precision", "BF\u2081\u2080\u00b2"
    )
  } else {
    col_names <- c(
      "Samples", "Misstatements", "Taint", "Estimate",
      paste0(round(object[["conf.level"]] * 100, 2), "% ", inttype, " interval\u00b9"), "Precision", "p-value\u00b2"
    )
  }
} else {
  col_names <- c(
    "Samples", "Misstatements", "Taint", "Estimate",
    paste0(round(object[["conf.level"]] * 100, 2), "% ", inttype, " interval\u00b9"), "Precision"
  )
}
if (!is.null(object[["N.units"]])) {
  col_names <- c("Units", col_names)
}
colnames(df) <- col_names
tab <- knitr::kable(df,
  digits = 3, caption = "Table 1: Evaluation summary", format = "html",
  format.args = list(big.mark = ",", scientific = FALSE),
  table.attr = "style=''", align = "c"
)
if (object[["alternative"]] == "two.sided") {
  alternative_string <- paste0("The displayed ", tolower(inttype), " interval is two-sided.")
} else {
  alternative_string <- paste0("The displayed ", tolower(inttype), " interval is one-sided.")
}
if (object[["materiality"]] < 1) {
  test_string <- switch(object[["alternative"]],
    "two.sided" = paste0("For all tests, the alternative hypothesis H\u2081 specifies that misstatement \u2260 ", round(object[["materiality"]], 3), "."),
    "less" = paste0("For all tests, the alternative hypothesis H\u2081 specifies that misstatement < ", round(object[["materiality"]], 3), "."),
    "greater" = paste0("For all tests, the alternative hypothesis H\u2081 specifies that misstatement > ", round(object[["materiality"]], 3), ".")
  )
} else {
  test_string <- NULL
}
if (is.nan(object[["t"]])) {
  taint_string <- "The taint could not be calculated because some book values are zero."
} else {
  taint_string <- NULL
}
prior_string <- NULL
if (!is.null(object[["prior"]])) {
  prior_string <- paste0("The prior distribution for the population misstatement is ", tolower(object[["prior"]]$prior), ".")
}
tab <- kableExtra::footnote(tab,
  number = c(alternative_string, test_string, taint_string, prior_string),
  fixed_small_size = TRUE
)
kableExtra::kable_classic(tab, position = "center", html_font = "Cambria")
```

```{r echo=FALSE, fig.align='center', fig.height=4.5, fig.width=7}
plot(object, type = "estimates")
```

`r if (!is.null(object[["data"]])){ "### Data" }`

```{r, echo = FALSE}
if (!is.null(object[["data"]])) {
  tab <- knitr::kable(object[["data"]],
    digits = 3,
    caption = "Table 2: Sample data",
    align = "c",
    format = "html",
    row.names = FALSE,
    format.args = list(big.mark = ",", scientific = FALSE),
    table.attr = "style='width:70%;'"
  )
  kableExtra::kable_classic(tab, position = "center", html_font = "Cambria")
}
```

### Citation

```{r echo = FALSE}
citation("jfa")
```
