gbm-developers / gbm

Gradient boosted models (the old gbm package)
Other
51 stars 27 forks source link

switch to inherits #58

Closed turgeonmaxime closed 8 months ago

turgeonmaxime commented 3 years ago

TLDR: I replaced statements like class(y) == "Surv" by the safer inherits(y, "Surv). It's safer, because it handles cases where class(y) is a vector of length greater than 1.

In a recent update to R, they started triggering warnings when the condition in an if statement has length greater than 1, for example:

foo <- matrix(NA, nrow = 2, ncol = 2)
class(foo)
#> [1] "matrix" "array"
if(class(foo) == "matrix"){
    # Do something
}
#> Warning in if (class(foo) == "matrix") {: the condition has length > 1 and only
#> the first element will be used
#> NULL

Created on 2021-02-03 by the reprex package (v1.0.0)

I've seen a few examples of these in gbm, but there's one instance in particular that triggers warnings in the casebase package, for which I'm a developer:

library(casebase)
#> See example usage at http://sahirbhatnagar.com/casebase/
N <- 1000; p <- 30
x <- matrix(rnorm(N * p), N, p)
hx <- exp(x)
ty <- rexp(N, hx)
tcens <- rbinom(n = N,
                prob = 0.3,
                size = 1) # censoring indicator
y <- cbind(time = ty, status = 1 - tcens) # y=Surv(ty,1-tcens) with survival
fitSmoothHazard.fit(x, y, time = "time",
                    event = "status",
                    family = "gbm", ratio = 10)
#> Error in if (nrow(x) != ifelse(class(y) == "Surv", nrow(y), length(y))) {: the condition has length > 1

Created on 2021-02-03 by the reprex package (v0.3.0)

(Note: It triggers an error on my machine because I turned on a couple flags that CRAN uses to easily find these warnings.)

koenderks commented 11 months ago

Since R 4.2.0 these warnings have become error messages (see https://cran.r-project.org/doc/manuals/r-release/NEWS.html, R 4.2.0, SIGNIFICANT USER-VISIBLE CHANGES, number 3).

"Calling if() or while() with a condition of length greater than one gives an error rather than a warning."

gregridgeway commented 8 months ago

Fixed