Learn R Programming

msmtools (version 1.0)

prevplot: Plot observed and expected prevalences for a multi-state model.

Description

Provides a graphical indication of goodness of fit of a multi-state model computed by msm. It also computes a rough indicator of where the data depart from the fitted Markov model.

Usage

prevplot(x, prev.obj, M = FALSE, exacttimes = TRUE, ci = FALSE,
  grid = 100L, x.lab.grid = 500L, xlab = "Time",
  ylab = "Prevalence (%)", lty.fit = 1, lwd.fit = 1, col.fit = "red",
  lty.ci.fit = 2, lwd.ci.fit = 1, col.ci.fit = col.fit, lwd.obs = 1,
  lty.obs = 1, col.obs = "darkblue", legend.pos = "topright",
  par.col = 3, plot.width = 10, plot.height = 5, max.m = 0.1,
  devnew = TRUE, verbose = TRUE)

Arguments

x
A msm object.
prev.obj
A list computed by prevalence.msm. It can be with or without confidence intervals. prevplot will behaves accordingly.
M
If TRUE, then a rough indicator of deviance from the model is computed (see 'Details'). Default is FALSE.
exacttimes
If TRUE (default) then transition times are known and exact. This is inherited from msm and should be set the same way.
ci
If TRUE, then confidence intervals, if they exist, are plotted. Default is FALSE.
grid
Define how many points should be used to build the x axis. Defaul is 100.
x.lab.grid
Define the interval on the x axis at which draw tick marks. Default is 500.
xlab
x axis label.
ylab
y axis label.
lty.fit
Line type for the expected prevalences. See par.
lwd.fit
Line width for the expected prevalences. See par.
col.fit
Line color for the expected prevalences. See par.
lty.ci.fit
Line type for the expected prevalences confidence limits. See par.
lwd.ci.fit
Line width for the expected prevalences confidence limits. See par.
col.ci.fit
Line color for the expected prevalences confidence limits. See par.
lwd.obs
Line width for the observed prevalences. See par.
lty.obs
Line type for the observed prevalences. See par.
col.obs
Line color for the observed prevalences. See par.
legend.pos
Where to position the legend. Default is "topright", but x and y coordinate can be passed. If NULL, then legend is not shown.
par.col
The number of columns of the plot. Default is 3.
plot.width
Width of new graphical device. Default is 7. See par.
plot.height
Height of new graphical device. Default is 7. See par.
max.m
If M = TRUE, it adjusts the upper y limit when plotting M.
devnew
Set the graphical device where to plot. By default, prevplot plots on a new device by setting dev.new. If FALSE, then a plot is drawn onto the current device as specified by dev.cur. If FALSE
verbose
If FALSE, all information produced by print, cat and message are suppressed. All is done internally so that no global options are changed. verbose can be set to FALSE on all com

Details

When M = TRUE, a rough indicator of the deviance from the Markov model is computed according to Titman and Sharples (2008). A comparison at a given time $t_i$ of a patient k in the state s between observed counts $O_{is}$ with expected ones $E_{is}$ is build as follows: $$M_{is} = \frac{(O_{is} - E_{is})^2}{E_{is}}$$

References

Titman, A. and Sharples, L.D. (2010). Model diagnostics for multi-state models, Statistical Methods in Medical Research, 19, 621-651. Titman, A. and Sharples, L.D. (2008). A general goodness-of-fit test for Markov and hidden Markov models, Statistics in Medicine, 27, 2177-2195. Jackson, C.H. (2011). Multi-State Models for Panel Data: The msm Package for R. Journal of Statistical Software, 38(8), 1-29. URL http://www.jstatsoft.org/v38/i08/.

See Also

plot.prevalence.msm, msm, prevalence.msm

Examples

Run this code
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 ) )

# defining the times at which compute the prevalences
t_min = min( hosp_augmented$augmented_int )
t_max = max( hosp_augmented$augmented_int )
steps = 100L

# computing prevalences
prev = prevalence.msm( msm_model, covariates = 'mean', ci = 'normal',
                       times = seq( t_min, t_max, steps ) )

# and plotting them using prevplot()
prevplot( msm_model, prev, ci = TRUE, devnew = FALSE, verbose = FALSE )

Run the code above in your browser using DataLab