flexsurvjson: flexsurvjson

View source: R/flexsurvjson.R

flexsurvjsonR Documentation

flexsurvjson

Description

Function flexsurvjson can receive models from flexsurv package. It then uses functions from flexsurv package internally to estimate multi-state model measures such as transition probabilities, length of stay, and confidence intervals of the estimations. Function flexsurvjson then take these results and reshapes them so that they can be fed to MSMplus properly as a json file.

Usage

flexsurvjson(
  model,
  vartime = seq(1, 1, by = 1),
  qmat,
  process = "Markov",
  totlos = FALSE,
  ci.json = FALSE,
  cl.json = 0.95,
  B.json = 50,
  tcovs = NULL,
  Mjson = 50,
  variance = FALSE,
  covariates_list = list(),
  jsonpath = "",
  name = "predictions.json"
)

Arguments

model

The hazard model (or list of hazard models), fit with flexsurv

vartime

A vector of time points for which predictions will be made. Default: 0. Functions from flexsurv package used: pmatrix.fs, pmatrix.simfs

qmat

the user has to supply the transition matrix

process

"Markov" for clock forward approach, "semiMarkov" for clock reset approach, Default: 'Markov'

totlos

Estimate total length of stay spent in each state "TRUE", "FALSE", Default: "FALSE"

ci.json

Estimate confidence intervals, "TRUE", "FALSE", Default: "FALSE"

cl.json

Specify confidence level, Default: 0.95

B.json

Number of simulations from the normal asymptotic distribution used to calculate variances. Decrease for greater speed at the expense of accuracy, Default: 50

tcovs

Predictable time-dependent covariates , Default: NULL

Mjson

Number of individuals to simulate in order to approximate the transition probabilities. Users should adjust this to obtain the required precision. Default: 50

variance

Calculate the variances and covariances of the transition cumulative hazards (TRUE or FALSE). This is based on simulation from the normal asymptotic distribution of the estimates, which is computationally-expensive., Default: FALSE

covariates_list

The user can specify different covariate patterns for which predictions will be made, Default: list()

jsonpath

specify the path of the folder that the json file should be saved, Default: "" saves the json file to the current working directory

name

Specify the name of the output json file, Default: 'predictions.json'

Details

DETAILS

Value

returns a list of objects: the time variable the number of covariate patterns, the names of covariate patterns, the transition matrix, the number of transitions, the transition probabilities, transition intensities length of stay, their confidence intervals

See Also

stri_sort

Examples

## Not run: 
 #EXAMPLE
 
 library("MSMplus")
 library("mstate")
 library("flexsurv")
 library("dplyr")
 head(ebmt)


### Let's first define the transition matrix

tmat <- transMat(x = list(c(2, 3),c(3), c() ), names = c("Transplant", "Platelet Recovery", "Relapse/Death" ) )


### We will now create dummy variables for the age categories

ebmt$age2=  recode(ebmt$age, ">40" =0, "20-40"=1,"<=20" =0 )
ebmt$age3=  recode(ebmt$age, ">40" =1, "20-40"=0,"<=20" =0 )


#Data preparation- From one row per participant to multiple rows per participant, one for each allowed transition.

msebmt <- msprep(data = ebmt, trans = tmat, 
                 time = c(NA, "prtime", "rfstime"), status = c(NA, "prstat", "rfsstat"), keep=c("age2","age3"))

head(msebmt)

## Multi-state model analysis: Using flexsurvjson function together with flexsurv package

### Provide time vector

tgrid <- seq(1, 10, by = 1)   

### Provide transition matrix

tmat <- rbind(c(NA, 1, 2), c(NA, NA, 3), c(NA, NA, NA)) 


### Run transition specific hazard models: Clock forward approach


cfwei.list<-vector(3,mode="list")

for (i in 1:3) {
  
  cfwei.list[[i]]<-flexsurvreg(Surv(Tstart,Tstop,status)~age2+age3,subset=(trans==i),
                               dist="weibull",data=msebmt)
}



### Prediction for different covariate patterns (the 3 age categories)

wh1 <- which(msebmt$age2 == 0 & msebmt$age3 == 0)
pat1 <- msebmt[rep(wh1[1], 1), 9:10]



wh2 <- which(msebmt$age2 == 1 & msebmt$age3 == 0)
pat2 <- msebmt[rep(wh2[1], 1), 9:10]


wh3 <- which(msebmt$age2 == 0 & msebmt$age3 == 1)
pat3 <- msebmt[rep(wh3[1], 1), 9:10]



#We now run the flexsurvjson function to perform the multi-state model analysis using the function 
#from package flexsurv and the pack the predictions in a json file.

results_cf <- MSMplus::flexsurvjson( model=cfwei.list, vartime=seq(0,3652.5,by=365.25), qmat=tmat, process="Markov",
                                    totlos=TRUE, ci.json=FALSE, cl.json=0.95, B.json=50, tcovs=NULL,
                                    Mjson=100, variance=FALSE,
                                    covariates_list=list(pat1,pat2,pat3), 
                                    jsonpath="",
                                    name="predictions_EBMT_flex_fw.json" ) 




 
 

## End(Not run)


nskourlis/MSMplus documentation built on Dec. 7, 2023, 4:53 a.m.