inspect_prob checks if an object is a numeric vector of valid probability values. This can be useful to validate inputs, intermediate calculations or outputs in user-defined functions.

inspect_prob(x, allow_nas = TRUE, warning_nas = TRUE)

Arguments

x

An arbitrary object.

allow_nas

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

warning_nas

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

Value

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

  • The call is silent if:

    • x is a numeric vector of valid probability values and there are no NA or NaN values in x.

    • x is a numeric vector of valid probability values, there are some NA or NaN values in x, allow_nas is set to TRUE and warning_nas is set to FALSE.

  • An informative warning message is thrown if x is a numeric vector of valid probability values, there are some NA or NaN values in x and both allow_nas and warning_nas are set to TRUE.

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

    • x is not a numeric vector of valid probability values.

    • x is a numeric vector of valid probability values, there are some NA or NaN values in x and allow_nas is set to FALSE.

Details

inspect_prob conducts a series of tests to check if x is a numeric vector of valid probability values. Namely, inspect_prob checks if:

  • x is NULL or empty.

  • x is an atomic vector.

  • x is numeric.

  • x has NA or NaN values.

  • The values of x are in the [0, 1] interval.

See also

Examples

# Calls that pass silently:
x1 <- c(0.1, 0.2, 0.3, 0.4, 0.5)
x2 <- c(0.1, 0.2, 0.3, 0.4, 0.5, NA)
inspect_prob(x1)
inspect_prob(x2, warning_nas = FALSE)
inspect_prob(x2, allow_nas = TRUE, warning_nas = FALSE)

# Calls that throw an informative warning message:
y <- c(0.1, 0.2, NA, 0.4, 0.5)
try(inspect_prob(y))
#> Warning: There are NA or NaN values in y.
try(inspect_prob(y, allow_nas = TRUE))
#> Warning: There are NA or NaN values in y.
try(inspect_prob(y, allow_nas = TRUE, warning_nas = TRUE))
#> Warning: There are NA or NaN values in y.

# Calls that throw an informative error message:
z1 <- c(-0.9, 0, 0.1, 0.2, 0.3, 0.4, 0.5)
try(inspect_prob(z1))
#> Error in inspect_prob(z1) : 
#>   Invalid argument: all elements of z1 must be in the [0, 1] interval.
z2 <- c(NA, 0, 0.1, 0.2, 0.3, 0.4, 0.5)
try(inspect_prob(z2, allow_nas = FALSE))
#> Error in inspect_prob(z2, allow_nas = FALSE) : 
#>   Invalid argument: There are NA or NaN values in  z2.
mylist <- list(
  NULL, TRUE, factor(.5), matrix(0.5),
  "0.5", list(0.5), NA, NaN, numeric(0), 1.1, -0.5
)
try(inspect_prob(mylist[[1]]))
#> Error in inspect_prob(mylist[[1]]) : 
#>   Invalid argument: mylist[[1]] is NULL.
try(inspect_prob(mylist[[2]]))
#> Error in inspect_prob(mylist[[2]]) : 
#>   Invalid argument: the type of mylist[[2]] must be numeric.
try(inspect_prob(mylist[[3]]))
#> Error in inspect_prob(mylist[[3]]) : 
#>   Invalid argument: mylist[[3]] must be an atomic vector.
try(inspect_prob(mylist[[4]]))
#> Error in inspect_prob(mylist[[4]]) : 
#>   Invalid argument: mylist[[4]] must be an atomic vector.
try(inspect_prob(mylist[[5]]))
#> Error in inspect_prob(mylist[[5]]) : 
#>   Invalid argument: the type of mylist[[5]] must be numeric.
try(inspect_prob(mylist[[6]]))
#> Error in inspect_prob(mylist[[6]]) : 
#>   Invalid argument: mylist[[6]] must be an atomic vector.
try(inspect_prob(mylist[[7]]))
#> Error in inspect_prob(mylist[[7]]) : 
#>   Invalid argument: all elements of mylist[[7]] are NA or NaN.
try(inspect_prob(mylist[[8]]))
#> Error in inspect_prob(mylist[[8]]) : 
#>   Invalid argument: all elements of mylist[[8]] are NA or NaN.
try(inspect_prob(mylist[[9]]))
#> Error in inspect_prob(mylist[[9]]) : 
#>   Invalid argument: mylist[[9]] is empty.
try(inspect_prob(mylist[[10]]))
#> Error in inspect_prob(mylist[[10]]) : 
#>   Invalid argument: all elements of mylist[[10]] must be in the [0, 1] interval.
try(inspect_prob(mylist[[11]]))
#> Error in inspect_prob(mylist[[11]]) : 
#>   Invalid argument: all elements of mylist[[11]] must be in the [0, 1] interval.