Bug: Compute_probs() failing when requesting probabilities with more than 3 bins if the data has NAs
I found this bug in the Compute_probs() function in modules/Skill/s2s.probs.R that is called by compute_probabilities() in Skill.R. Mostly opening an issue to report on the solution to it.
This was the error that Apply()
was raising:
Error in names(dim(arrays_of_results[[component]])) <- `*vtmp*` :
attempt to set an attribute on NULL
In addition: Warning messages:
1: In arrays_of_results[[component]][(1:prod(component_dims)) + (m - :
number of items to replace is not a multiple of replacement length
2: In arrays_of_results[[component]][(1:prod(component_dims)) + (m - :
number of items to replace is not a multiple of replacement length
3: In arrays_of_results[[component]][(1:prod(component_dims)) + (m - :
number of items to replace is not a multiple of replacement length
Ultimately I found that the source of the error was the hard-coding in the definition of the function to convert to probabilities:
if (na.rm == FALSE) {
c2p <- function(x, t) {
colMeans(convert2prob(as.vector(x), threshold = as.vector(t)))
}
} else {
c2p <- function(x, t) {
if (any(!is.na(x))) {
colMeans(convert2prob(as.vector(x), threshold = as.vector(t)))
} else {
c(NA, NA, NA)
}
}
}
The definition under the else condition can be generalized to any number of probability bins like so:
c2p <- function(x, t) {
if (any(!is.na(x))) {
colMeans(convert2prob(as.vector(x), threshold = as.vector(t)))
} else {
rep(NA, dim(t)[['bin']] + 1) # vector with as many NAs as probability bins.
}
}
Victòria