inspect_prob {inspector} | R Documentation |
Validate vectors of probabilities
Description
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.
Usage
inspect_prob(x, allow_nas = TRUE, warning_nas = TRUE)
Arguments
x |
An arbitrary object. |
allow_nas |
Logical value. If |
warning_nas |
Logical value. If |
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
isNULL
or empty. -
x
is an atomic vector. -
x
is numeric. -
x
hasNA
orNaN
values. The values of
x
are in the [0, 1] interval.
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 noNA
orNaN
values inx
. -
x
is a numeric vector of valid probability values, there are someNA
orNaN
values inx
,allow_nas
is set toTRUE
andwarning_nas
is set toFALSE
.
-
An informative warning message is thrown if
x
is a numeric vector of valid probability values, there are someNA
orNaN
values inx
and bothallow_nas
andwarning_nas
are set toTRUE
.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 someNA
orNaN
values inx
andallow_nas
is set toFALSE
.
-
See Also
-
inspect_par_bernoulli
to check if an object is a valid Bernoulli/Binomial proportion. -
inspect_par_multinomial
to check if an object is a numeric vector of valid Multinomial proportions.
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))
try(inspect_prob(y, allow_nas = TRUE))
try(inspect_prob(y, allow_nas = TRUE, warning_nas = TRUE))
# 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))
z2 <- c(NA, 0, 0.1, 0.2, 0.3, 0.4, 0.5)
try(inspect_prob(z2, allow_nas = FALSE))
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]]))
try(inspect_prob(mylist[[2]]))
try(inspect_prob(mylist[[3]]))
try(inspect_prob(mylist[[4]]))
try(inspect_prob(mylist[[5]]))
try(inspect_prob(mylist[[6]]))
try(inspect_prob(mylist[[7]]))
try(inspect_prob(mylist[[8]]))
try(inspect_prob(mylist[[9]]))
try(inspect_prob(mylist[[10]]))
try(inspect_prob(mylist[[11]]))