inspect_par_multinomial checks if an object is an eligible vector of Multinomial proportions. This can be useful to validate inputs, intermediate calculations or outputs in user-defined functions.

inspect_par_multinomial(x)

Arguments

x

An arbitrary object.

Value

inspect_par_multinomial does not return any output. There are two possible outcomes:

  • The call is silent if x is an eligible vector of Multinomial proportions.

  • An informative error message is thrown otherwise.

Details

inspect_par_multinomial conducts a series of tests to check if x is an eligible vector of Multinomial proportions. Namely, inspect_par_multinomial checks if:

  • x is NULL or empty.

  • x is an atomic vector

  • x is numeric

  • x has NA or NaN values.

  • All elements of x are in the (0, 1) interval.

  • x sums to 1.

See also

Examples

# Calls that pass silently:
x1 <- c(0.5, 0.5)
x2 <- rep(1 / 5, 5)
inspect_par_multinomial(x1)
inspect_par_multinomial(x2)

# Calls that throw an informative error message:
mylist <- list(
  NULL, TRUE, factor(0.5, 0.5),
  matrix(c(0.5, 0.5)), c("0.5", "0.5"), list(0.5, 0.5),
  c(0.9, NA), c(0.9, NaN), numeric(0), NA, c(0.9, 0.6), c(-0.1, 0.9)
)
try(inspect_par_multinomial(mylist[[1]]))
#> Error in inspect_par_multinomial(mylist[[1]]) : 
#>   Invalid argument: mylist[[1]] is NULL.
try(inspect_par_multinomial(mylist[[2]]))
#> Error in inspect_par_multinomial(mylist[[2]]) : 
#>   Invalid argument: mylist[[2]] must be numeric.
try(inspect_par_multinomial(mylist[[3]]))
#> Error in inspect_par_multinomial(mylist[[3]]) : 
#>   Invalid argument: mylist[[3]] must be an atomic vector.
try(inspect_par_multinomial(mylist[[4]]))
#> Error in inspect_par_multinomial(mylist[[4]]) : 
#>   Invalid argument: mylist[[4]] must be an atomic vector.
try(inspect_par_multinomial(mylist[[5]]))
#> Error in inspect_par_multinomial(mylist[[5]]) : 
#>   Invalid argument: mylist[[5]] must be numeric.
try(inspect_par_multinomial(mylist[[6]]))
#> Error in inspect_par_multinomial(mylist[[6]]) : 
#>   Invalid argument: mylist[[6]] must be an atomic vector.
try(inspect_par_multinomial(mylist[[7]]))
#> Error in inspect_par_multinomial(mylist[[7]]) : 
#>   Invalid argument: there are NA or NaN values in mylist[[7]].
try(inspect_par_multinomial(mylist[[8]]))
#> Error in inspect_par_multinomial(mylist[[8]]) : 
#>   Invalid argument: there are NA or NaN values in mylist[[8]].
try(inspect_par_multinomial(mylist[[9]]))
#> Error in inspect_par_multinomial(mylist[[9]]) : 
#>   Invalid argument: mylist[[9]] is empty.
try(inspect_par_multinomial(mylist[[10]]))
#> Error in inspect_par_multinomial(mylist[[10]]) : 
#>   Invalid argument: there are NA or NaN values in mylist[[10]].
try(inspect_par_multinomial(mylist[[11]]))
#> Error in inspect_par_multinomial(mylist[[11]]) : 
#>   Invalid argument: mylist[[11]] must sum to 1.
try(inspect_par_multinomial(mylist[[12]]))
#> Error in inspect_par_multinomial(mylist[[12]]) : 
#>   Invalid argument: all elements of mylist[[12]] values must be in the (0, 1) interval.