chjackson / flexsurv

The flexsurv R package for flexible parametric survival and multi-state modelling
http://chjackson.github.io/flexsurv/
54 stars 28 forks source link

Cannot call pmatrix.simfs from inside a function #73

Closed nskourlis closed 4 years ago

nskourlis commented 4 years ago

Hello,

My name is Nick. I am trying to call pmatrix.simfs and pmatrix.fs from inside a self-made function. Up untill one week ago, it was working without any issues. After reinstalling the flexsurv package, these 2 functions started giving an error when called from inside my function.

The error I get seems to be from inside pmatrix.simfs Issue_pmatrix_error

Below I give a simple exapmle that leads to that error:

library("flexsurv")
library("mstate")

data(bosms3)
bosms3$group=c(rbinom(nrow(bosms3),1,0.7))
bosms3$sex=c(rbinom(nrow(bosms3),1,0.5))

tmat <- rbind(c(NA, 1, 2), c(NA, NA, 3), c(NA, NA, NA)) #Provide transition matrix

## List of models
crfpm.list<-vector(3,mode="list")

for (i in 1:3) {
     crfpm.list[[i]]<-flexsurvreg(Surv(years,status)~group+sex,subset=(trans==i),
                               dist="weibull",data=bosms3)
}

### Covariate patterns i am interested in
wh1 <- which(bosms3$group == 1 & bosms3$sex == 1)
pat1 <- bosms3[rep(wh1[1], 3), 9:10]
attr(pat1, "trans") <- tmat

wh2 <- which(bosms3$group == 1 & bosms3$sex == 0)
pat2 <- bosms3[rep(wh2[1], 3), 9:10]
attr(pat2, "trans") <- tmat

fu <- function(model , mat,vartime,ci.json, cl.json, B.json, tcovs, Mjson,covariates_list ) {

      nstates=ncol(mat)

      for (g in 1:length(covariates_list)) {

            pm=list()

            for (k in 1:length(vartime)) {

             pm[[k]]= pmatrix.simfs(x=model, trans=mat, t = vartime[k],  
                                           newdata =covariates_list[[g]], ci = ci.json,
                                           tcovs = tcovs, B = B.json, cl = cl.json, M = Mjson)
            }   
     }
}

fu(model=crfpm.list , mat=tmat,vartime=1,ci.json=TRUE, 
       cl.json=0.95, B.json=100, tcovs=NULL, 
       Mjson=100,covariates_list=list(pat1,pat2) )

I would really appreciate your help!

chjackson commented 4 years ago

I think this is fixed now in 869adcc546e441f35ce3d2a74594d1f630eaf6a1 . Thanks for the report.