sahirbhatnagar/casebase

fitSmoothHazard.fit and formula_time = ~1

Opened this issue · 0 comments

There's a bug when we try to specify formula_time = ~1 in fitSmoothHazard.fit. This would happen, for example, if we assumed an exponential hazard.

library(casebase)
#> See example usage at http://sahirbhatnagar.com/casebase/
N <- 1000; p <- 30
nzc <- 0.33 * p
x <- matrix(rnorm(N * p), N, p)
dimnames(x)[[2]] <- paste0("x", seq_len(p))
beta <- rnorm(nzc)
fx <- x[, seq(nzc)] %*% (0.33 * beta)
hx <- exp(fx)
ty <- rexp(N, hx)
tcens <- rbinom(n = N,
                prob = 0.3,
                size = 1) # censoring indicator
y <- cbind(time = ty, status = 1 - tcens)

# This one works
out1 <- fitSmoothHazard.fit(x, y, time = "time", 
                            event = "status",
                            ratio = 10, 
                            formula_time = ~ time)

# This one doesn't
out2 <- fitSmoothHazard.fit(x, y, time = "time", 
                            event = "status",
                            ratio = 10, 
                            formula_time = ~ 1)
#> Error in fitSmoothHazard.fit(x, y, time = "time", event = "status", ratio = 10, : length(timeVar) == 1 is not TRUE

Created on 2021-07-08 by the reprex package (v2.0.0)

It should be fairly easy to fix. The culprit are these lines here:

casebase/R/fitting.R

Lines 274 to 276 in c4d208c

timeVar <- if (length(formula_time) == 3) {
all.vars(formula_time[[3]])
} else all.vars(formula_time)

When formula_time = ~1, we end up with timeVar = character(0), hence the error.