sahirbhatnagar/casebase

absoluteRisk and glmnet

Opened this issue · 0 comments

We still have a bug when using absoluteRisk with family = "glmnet". Specifically, the issue arises when we use fitSmoothHazard.fit with a matrix of covariate X without any column names:

library(casebase)
#> See example usage at http://sahirbhatnagar.com/casebase/
library(survival)
library(mvtnorm)

n <- 75
p <- 5

# Generate covariates
X <- rmvnorm(n = n, sigma = diag(p))
# Generate failure times
fail_times <- rexp(n = n,
                   rate = 1)
# Generate censoring times
cens_times <- runif(n = n, min = 1, max = 3) *
    rexp(n = n, rate = 1)

times <- pmin(fail_times, cens_times)
status <- as.numeric(fail_times < cens_times)

model <- fitSmoothHazard.fit(X, Surv(times, event = status), 
                             time = "time", event = "status", 
                             family = "glmnet")

# Next line fails
absoluteRisk(model, time = 1,
             newdata = X[1:5, ])
#> Error in h(simpleError(msg, call)): error in evaluating the argument 'x' in selecting a method for function 'as.matrix': Cholmod error 'X and/or Y have wrong dimensions' at file ../MatrixOps/cholmod_sdmult.c, line 88

# No longer fails if we add column names
colnames(X) <- paste0("V", seq_len(p))
absoluteRisk(model, time = 1,
             newdata = X[1:5, ])
#>  time                                                  
#>     0 0.0000000 0.0000000 0.0000000 0.0000000 0.0000000
#>     1 0.5258076 0.5258076 0.5258076 0.5258076 0.5258076

Created on 2021-06-30 by the reprex package (v2.0.0)

The culprit is these lines:

casebase/R/absoluteRisk.R

Lines 253 to 255 in c4d208c

newdata2 <- current_obs[,
colnames(newdata) != object$timeVar,
drop = FALSE]

When X has no column names, colnames(newdata) is simply NULL and therefore colnames(newdata) != object$timeVar is a zero-length logical vector. As a consequence, newdata2 is a matrix without any column!