R/debit-map-seq.R

#' DEBIT-test values around the reported ones
#'
#' @description `debit_map_seq()` varies the reported means, standard
#'   deviations, and sample sizes a few steps up and down and DEBIT-tests each
#'   resulting value set. This shows whether a small error in reporting or
#'   computing could explain an inconsistency.
#'
#'   Call [`audit()`] on the output for summary statistics.
#'
#' @param data A data frame that [`debit_map()`] could take.
#' @param digits_x,digits_sd Integer. The number of decimal places in `x` and
#'   `sd`, including trailing zeros. There are no defaults, since a numeric
#'   value does not keep them (`1.40` is `1.4`). Either a single number for the
#'   whole column or one number per row of `data`. If left out, they are taken
#'   from `digits_x` and `digits_sd` columns of `data`. They also set the step
#'   sizes for dispersing `x` and `sd`.
#' @param x,sd,n Optionally, the names of the columns in `data` that contain
#'   these values, if they differ from the defaults.
#' @param var String. The variables to disperse. Default is
#'   `c("x", "sd", "n")`.
#' @param out_min,out_max Limits on the dispersed values. Both default to
#'   `"auto"`, which keeps `n` at 2 or above (1 under `sd_meaning =
#'   "sd_pop"` or `"sd_sample_or_pop"`) and `x` and `sd` between 0 and 1. A
#'   number applies to every dispersed variable, and `NULL` removes the limit.
#' @param ... These dots must be empty. Every argument after them must be
#'   named in full.
#' @inheritParams debit_map
#' @inheritParams grim_map_seq
#'
#' @return A tibble with the columns of [`debit_map()`] output plus
#'   `diff_var`, `case`, and `var`, as in [`grim_map_seq()`]. As there, the
#'   other columns of `data` are dropped.
#'
#' @section Summaries with [`audit()`]: `audit()` on `debit_map_seq()`
#'   output returns a tibble with one row per case and these columns:
#'   - `x`, `sd`, and `n`: the reported values, with `data_type` and their
#'   `consistency`.
#'   - `hits_total`: the number of DEBIT-consistent value sets found within the
#'   `dispersion` range.
#'   - `hits_x`, `hits_sd`, `hits_n`: the number found by varying `x`, `sd`,
#'   and `n`, respectively. If both `include_reported` and `include_consistent`
#'   are `TRUE`, a consistent reported value set is counted as well: once per
#'   variable here, and once in `hits_total`.
#'   - `diff_x`: the distance from `x` to the nearest consistent dispersed
#'   value, in dispersion steps. `diff_x_up` and `diff_x_down` are the signed
#'   steps to the nearest consistent value above and below (`_down` is
#'   negative).
#'   - `diff_sd`, `diff_sd_up`, `diff_sd_down`: the same for `sd`.
#'   - `diff_n`, `diff_n_up`, `diff_n_down`: the same for `n`.
#'
#'   A count or distance is `NA` if it depends on a dispersed value set whose
#'   consistency is `NA`, on a variable whose reported value is missing, which
#'   is not dispersed, or on rows removed from the output, as by `filter()`.
#'
#'   Call `audit()` again on that summary to summarize these columns further.
#'   `na_count` and `na_rate` there count the cases without a consistent value
#'   within the `dispersion` range, and those that a missing value leaves
#'   unknown.
#'
#' @include function-map-seq.R
#'
#' @export
#'
#' @examples
#' # `debit_map_seq()` takes the same input as `debit_map()`:
#' pigs3
#'
#' out <- pigs3 |>
#'   dplyr::slice(3:4) |>
#'   debit_map_seq(digits_x = 2, digits_sd = 2, data_type = "integer")
#' out
#'
#' # Case-wise summaries:
#' out |>
#'   audit()

debit_map_seq <- function_map_seq(
  .fn = debit_map,
  .reported = c("x", "sd", "n"),
  .name_test = "DEBIT",
  # The mean and SD of binary data lie in `[0, 1]`, and `debit()` rejects
  # anything outside, so the dispersion must not cross these bounds:
  .var_bounds = list(
    n = function(args) c(sd_min_n(args$sd_meaning), NA),
    x = c(0, 1),
    sd = c(0, 1)
  )
)

Try the scrutiny package in your browser

Any scripts or data that you put into this service are public.

scrutiny documentation built on Nov. 11, 2026, 1:09 a.m.