selective-inference / R-software

R software for selective inference
GNU General Public License v2.0
34 stars 18 forks source link

Cox for fixedLassoInf -- "Error in term1[o] <- ff(a[o]) * exp(-(a[o]^2 - z[o]^2)/2) : NAs are not allowed in subscripted assignments" throws despite no NA's in data #58

Open violet-nova opened 11 months ago

violet-nova commented 11 months ago

I have verified there is no missing data. The glmnet function runs fine. beta_hat creates a vector of values for every predictor. But when I run fixedLassoInf I get the error. I have tried tweaking arguments like gridrange, tol.beta, tol.kkt and it does not go away.

For this dataset, n = 398, p = 51 (after dummifying factors).

I believe I've faithfully followed the steps recommended for Cox in example documentation. Below is the traceback, and then the code I've been using that creates it.

Traceback:

My code:

` Make a df that contains: predictors, censor time, and outcome (0 or 1 numeric) coxdf<- df %>%dplyr::select(any_of(preds), censor_time, outcome)

[Make X matrix] xsurv <- coxdf %>%dplyr::select(any_of(preds)) %>% makeX(na.impute = TRUE) %>% scale(TRUE, FALSE) ## Centering x

[Determine Optimal Lambda from CV] coxLassocv <- cv.glmnet(x = xsurv, y = Surv(coxdf$censor_time, as.numeric(coxdf$outcome)-1 ), family = "cox", ) ## runs without issue

[Originally, I tried going straight from cv.glmnet. When that failed, I built a separate object from glmnet() but it did not fix the issue:] coxglm <- glmnet(x = xsurv, y = Surv(coxdf$censor_time, as.numeric(coxdf$outcome)-1 ), lambda = coxLassocv$lambda.min, family = "cox" ) ## also runs without issue

[Generate beta_hat as recommended in documentation, this seemingly runs without issue: ] beta_hat = as.numeric(coef(coxglm, x=xsurv, y=Surv(coxdf$censor_time, as.numeric(coxdf$outcome)-1 ), s=coxLassocv$lambda.min/398, ## n = 398 for this dataset. exact=TRUE ) # the beta values from lasso ) ## Generates a vector of values for all predictors, as expected status = as.numeric(coxdf$outcome)-1 ## I tried assigning a variable to this rather than calling it directly, it again changed nothing

[The moment of truth, this is where I get the error message:] out = fixedLassoInf(x=xsurv, y = coxdf$censor_time, beta = beta_hat, lambda = coxLassocv$lambda.min, status= status, family="cox" ) ## At this step, I keep getting: "Error in term1[o] <- ff(a[o]) * exp(-(a[o]^2 - z[o]^2)/2) : NAs are not allowed in subscripted assignments"`