inspect_data_dichotomous checks if an object contains data that is eligible to have been generated by a series of Bernoulli trials. This can be useful to validate inputs in user-defined functions.

inspect_data_dichotomous(data, success, allow_nas = TRUE, warning_nas = FALSE)

Arguments

data, success

Arbitrary objects. success is meant to indicate the value of data that corresponds to a success.

allow_nas

Logical value. If TRUE then NA and NaN values in data are allowed. If FALSE, execution is stopped and an error message is thrown in case there are NA or NaN values in data.

warning_nas

Logical value. If TRUE then the presence of NA or NaN values in data generates a warning message. NA and NaN values pass silently otherwise (if allow_nas is set toTRUE).

Value

inspect_data_dichotomous does not return any output. There are three possible outcomes:

  • The call is silent if:

    • data is eligible to have been generated by a series of Bernoulli trials and there are no NA or NaN values in data.

    • data is eligible to have been generated by a series of Bernoulli trials, there are some NA or NaN values in data, allow_nas is set to TRUE and warning_nas is set to FALSE.

  • An informative warning message is thrown if:

    • data is eligible to have been generated by a series of Bernoulli trials and success is not observed in data.

    • data is eligible to have been generated by a series of Bernoulli trials, there are NA or NaN values in data and both allow_nas and warning_nas are set to TRUE.

  • An informative error message is thrown and the execution is stopped if:

    • data is not eligible to have been generated by a series of Bernoulli trials.

    • data is eligible to have been generated by a series of Bernoulli trials, there are some NA or NaN values in data and allow_nas is set to FALSE.

Details

inspect_data_dichotomous conducts a series of tests to check if data is eligible to have been generated by a series of Bernoulli trials. Namely, inspect_data_dichotomous checks if:

  • data and success are NULL or empty.

  • data and success are atomic and have an eligible data type (logical, integer, double, character).

  • data and success have NA or NaN values.

  • The number of unique values in data and success are adequate.

  • success has length 1.

  • success is observed in data.

See also

Examples

# Calls that pass silently:
x1 <- c(1, 0, 0, 1, 0)
x2 <- c(FALSE, FALSE, TRUE)
x3 <- c("yes", "no", "yes")
x4 <- factor(c("yes", "no", "yes"))
x5 <- c(1, 0, 0, 1, 0, NA)
inspect_data_dichotomous(x1, success = 1)
inspect_data_dichotomous(x2, success = TRUE)
inspect_data_dichotomous(x3, success = "yes")
inspect_data_dichotomous(x4, success = "yes")
inspect_data_dichotomous(x5, success = 1)

# Calls that throw an informative warning message:
y1 <- c(1, 1, NA, 0, 0)
y2 <- c(0, 0)
success <- 1
try(inspect_data_dichotomous(y1, success = 1, warning_nas = TRUE))
#> Warning: There are NA or NaN values in y1.
try(inspect_data_dichotomous(y2, success = success))
#> Warning: success not observed in y2.

# Calls that throw an informative error message:
try(inspect_data_dichotomous(NULL, 1))
#> Error in inspect_data_dichotomous(NULL, 1) : 
#>   Invalid argument: NULL is NULL.
try(inspect_data_dichotomous(c(1, 0), NULL))
#> Error in inspect_data_dichotomous(c(1, 0), NULL) : 
#>   Invalid argument: NULL is NULL.
try(inspect_data_dichotomous(list(1, 0), 1))
#> Error in inspect_data_dichotomous(list(1, 0), 1) : 
#>   Invalid argument: list(1, 0) must be atomic.
try(inspect_data_dichotomous(c(1, 0), list(1)))
#> Error in inspect_data_dichotomous(c(1, 0), list(1)) : 
#>   Invalid argument: list(1) must be atomic and have length 1.
try(inspect_data_dichotomous(numeric(0), 0))
#> Error in inspect_data_dichotomous(numeric(0), 0) : 
#>   Invalid argument: numeric(0) is empty.
try(inspect_data_dichotomous(1, numeric(0)))
#> Error in inspect_data_dichotomous(1, numeric(0)) : 
#>   Invalid argument: numeric(0) must be atomic and have length 1.
try(inspect_data_dichotomous(NaN, 1))
#> Error in inspect_data_dichotomous(NaN, 1) : 
#>   Invalid argument: all elements of NaN are NA or NaN.
try(inspect_data_dichotomous(NA, 1))
#> Error in inspect_data_dichotomous(NA, 1) : 
#>   Invalid argument: all elements of NA are NA or NaN.
try(inspect_data_dichotomous(c(1, 0), NA))
#> Error in inspect_data_dichotomous(c(1, 0), NA) : 
#>   Invalid argument: NA is NA or NaN
try(inspect_data_dichotomous(c(1, 0), NaN))
#> Error in inspect_data_dichotomous(c(1, 0), NaN) : 
#>   Invalid argument: NaN is NA or NaN
try(inspect_data_dichotomous(c(1, 0), 2))
#> Error in inspect_data_dichotomous(c(1, 0), 2) : 
#>   Invalid argument: there are more than two levels'.