# NOT RUN {
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 from state = 1
survplot( msm_model, km = TRUE, ci = 'none',
verbose = FALSE )
# plotting the fitted and empirical survival from state = 2 and
# adding it to the previous plot
survplot( msm_model, from = 2, km = TRUE, ci = 'none', add = TRUE,
verbose = FALSE )
# returning fitted and empirical data
all_data = survplot( msm_model, ci = 'none', return.all = TRUE,
verbose = FALSE, do.plot = FALSE )
# saving them separately
km_data = all_data[[ 1 ]]
fitted_data = all_data[[ 2 ]]
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab