gamlss-dev / gamlss.dist

gamlss.dist: Distributions for Generalized Additive Models for Location Scale and Shape
https://CRAN.R-project.org/package=gamlss.dist
4 stars 3 forks source link

pBCCG doesn't handle missing values well #2

Open colinorourke opened 1 year ago

colinorourke commented 1 year ago

I've found an issue with the BCCG distribution functions. They don't gracefully handle missing values due to the use of any(q < 0), which will return NA if any of its inputs are missing. Here's a reprex, and a comparison to the behaviour of pnorm, which behaves as expected:

# quantiles
q1 = c(0,1)
q2 = c(0,1,NA_real_)

# Expected output: 2 numbers
gamlss.dist::pBCCG(q1, mu = 1, sigma = 1, nu = 0)
#> [1] 0.0 0.5
pnorm(q1, 1, 0.5)
#> [1] 0.02275013 0.50000000

# Expected output: 2 numbers and a missing value. pnorm does this,
# but pBCCG returns an error
gamlss.dist::pBCCG(q2, mu = 1, sigma = 1, nu = 0)
#> Error in if (any(q < 0)) stop(paste("q must be positive", "\n", "")): missing value where TRUE/FALSE needed
pnorm(q2, mean = 1, sd = 0.5)
#> [1] 0.02275013 0.50000000         NA

Created on 2023-09-14 with reprex v2.0.2

zeileis commented 7 months ago

Colin @colinorourke, thanks for this and apologies for the delay. This is a good catch. The natural fix is probably to replace any(...) with any(..., na.rm = TRUE). The same fix should then be applied to other distributions where any(...) is used.

Mikis @mstasinopoulos can you have a look? Do you agree?