data( hosp )
# augmenting the data
hosp_augmented = augment( data = hosp, data_key = subj, n_events = adm_number, pattern = label_3,
t_start = dateIN, t_end = dateOUT, t_cens = dateCENS )
# let's define the initial transition matrix for our model
Qmat = matrix( data = 0, nrow = 3, ncol = 3, byrow = TRUE )
Qmat[ 1, 1:3 ] = 1
Qmat[ 2, 1:3 ] = 1
colnames( Qmat ) = c( 'IN', 'OUT', 'DEAD' )
rownames( Qmat ) = c( 'IN', 'OUT', 'DEAD' )
# attaching the msm package and running the model using
# gender and age as covariates
library( msm )
msm_model = msm( status_num ~ augmented_int,
subject = subj, data = hosp_augmented, covariates = ~ gender + age,
exacttimes = TRUE, gen.inits = TRUE, qmatrix = Qmat, method = 'BFGS',
control = list( fnscale = 6e+05, trace = 0,
REPORT = 1, maxit = 10000 ) )
# plotting the fitted and empirical survival
survplot( msm_model, km = TRUE, ci = 'none', verbose = FALSE, devnew = FALSE )
# returning fitted and empirical data
all_data = survplot( msm_model, ci = 'none',
return.km = TRUE, return.p = TRUE,
verbose = FALSE, do.plot = FALSE )Run the code above in your browser using DataLab