correctSingleHeap: Correct a Single Age Heap

View source: R/correctHeap.R

correctSingleHeapR Documentation

Correct a Single Age Heap

Description

While correctHeaps corrects regular heaping patterns, this function allows correction of a single specific heap value. This is useful when heaping occurs at irregular intervals or when only a particular age shows excessive heaping.

Usage

correctSingleHeap(
  x,
  heap,
  before = 2,
  after = 2,
  method = "lnorm",
  fixed = NULL,
  seed = NULL,
  na.action = "omit",
  verbose = FALSE,
  sd = NULL
)

Arguments

x

numeric vector representing ages (typically integers).

heap

numeric value specifying the age for which heaping should be corrected. Must be present in x.

before

numeric value specifying the number of years before the heap to use as the lower bound for replacement values. Will be rounded to an integer. Default is 2.

after

numeric value specifying the number of years after the heap to use as the upper bound for replacement values. Will be rounded to an integer. Default is 2.

method

character string specifying the distribution used for correction:

"lnorm"

truncated log-normal distribution (default). Parameters are estimated from the input data.

"norm"

truncated normal distribution. Parameters are estimated from the input data.

"unif"

uniform distribution within the truncation bounds.

"kernel"

kernel density estimation for nonparametric sampling.

fixed

numeric vector of indices indicating observations that should not be changed. Useful for preserving known accurate values.

seed

optional integer for random seed to ensure reproducibility.

na.action

character string specifying how to handle NA values: "omit" (default) or "fail".

verbose

logical. If TRUE, return diagnostic information.

sd

optional numeric value for standard deviation when method = "norm".

Details

Correct a specific age heap in a vector containing ages.

Value

A numeric vector of the same length as x with the specified heap corrected, or a list with diagnostics if verbose = TRUE.

Author(s)

Matthias Templ, Bernhard Meindl

See Also

correctHeaps for correcting regular heaping patterns.

Other heaping correction: correctHeaps()

Examples

# Create artificial age data
set.seed(123)
age <- rlnorm(10000, meanlog = 2.466869, sdlog = 1.652772)
age <- round(age[age < 93])

# Artificially introduce a heap at age 23
age23 <- c(age, rep(23, length = sum(age == 23)))

# Correct with reproducible results
age23_corrected <- correctSingleHeap(age23, heap = 23, before = 5, after = 5,
                                     method = "lnorm", seed = 42)

# Get diagnostic information
result <- correctSingleHeap(age23, heap = 23, before = 5, after = 5,
                            verbose = TRUE, seed = 42)
print(result$n_changed)

heaping documentation built on Feb. 10, 2026, 1:08 a.m.