is_age_coherent: check for coherence within Age and between Age and AgeInt

View source: R/utilsAge.R

is_age_coherentR Documentation

check for coherence within Age and between Age and AgeInt

Description

A few checks are carried out to test if Age is internally consistent, that OAG is consistent with AgeInt, and that Age and AgeInt are consistent with one another. For Age to be internally consistent, we cannot have redundant values, and values must be sequential.

Usage

is_age_coherent(Age, AgeInt, OAG = TRUE)

Arguments

Age

integer vector of single ages (lower bound)

AgeInt

integer vector. Age interval widths

OAG

logical (default TRUE). Is the final age group open?

Details

If OAG is TRUE then AgeInt must be coded as NA. If Age is not sorted then we sort both Age and AgeInt, assuming that they are in matched order. This isn't incoherence in itself, but a message is returned to the console.

Value

logical. TRUE if the arguments are considered consistent.

Examples

Age    <- 0:99
AgeInt <- rep(1, 100)
# closed, sequential, non-redundant ages, any easy yes:
is_age_coherent(Age = Age, AgeInt = AgeInt, OAG = FALSE)     # TRUE

# incorrectly coded OAG
is_age_coherent(Age = Age, AgeInt = AgeInt, OAG = TRUE)      # FALSE

# correctly coded OAG
AgeInt[100] <- NA
is_age_coherent(Age = Age, AgeInt = AgeInt, OAG = TRUE)      # TRUE

# correct unordered, but this isn't incoherence per se.
# watch out though!
aaoo <- order(sample(Age, 100, replace = FALSE))
is_age_coherent(Age[aaoo], AgeInt = AgeInt[aaoo], OAG = TRUE) # TRUE

# check redundancy
AgeRed    <- c(0:100,70)
AgeInt    <- c(rep(1, 100), NA, NA)
ao        <- order(AgeRed)
AgeRed    <- AgeRed[ao]
AgeIntRed <- AgeInt[ao]
is_age_coherent(AgeRed, AgeInt, TRUE)  # FALSE

timriffe/DemoTools documentation built on Jan. 28, 2024, 5:13 a.m.