-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_survival_prob.R
57 lines (57 loc) · 1.9 KB
/
plot_survival_prob.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
plot_survival_prob<-function (x, from = NULL, to = NULL, range = NULL, covariates = "mean",
legend.pos = NULL, xlab = "Time", ylab = "Fitted survival probability",
lwd = 1, ylim = NULL, ...)
{
if (!inherits(x, "msm"))
stop("expected x to be a msm model")
if (is.null(from))
from <- transient.msm(x)
else {
if (!is.numeric(from))
stop("from must be numeric")
if (any(!(from %in% 1:x$qmodel$nstates)))
stop("from must be a vector of states in 1, ..., ",
x$qmodel$nstates)
}
if (is.null(to))
to <- max(absorbing.msm(x))
else {
if (!is.numeric(to))
stop("to must be numeric")
if (!(to %in% absorbing.msm(x)))
stop("to must be an absorbing state")
}
if (is.null(range))
rg <- range(model.extract(x$data$mf, "time"))
else {
if (!is.numeric(range) || length(range) != 2)
stop("range must be a numeric vector of two elements")
rg <- range
}
if (is.null(ylim))
ylim <- c(0,1)
else {
ylim <- ylim
}
timediff <- (rg[2] - rg[1])/50
times <- seq(rg[1], rg[2], timediff)
pr <- numeric()
cols <- rainbow(length(from))
for (t in times) pr <- c(pr, pmatrix.msm(x, t, times[1],
covariates)[from[1], to])
plot(times, 1 - pr, type = "l", xlab = xlab, ylab = ylab,
lwd = lwd, ylim = ylim, lty = 1, col = cols[1], ...)
lt <- 2
for (st in from[-1]) {
pr <- numeric()
for (t in times) pr <- c(pr, pmatrix.msm(x, t, times[1],
covariates)[st, to])
lines(times, 1 - pr, type = "l", lty = lt, lwd = lwd,
col = cols[lt], ...)
lt <- lt + 1
}
if (!is.numeric(legend.pos) || length(legend.pos) != 2)
legend.pos <- c(max(times) - 15 * timediff, 1)
legend(legend.pos[1], legend.pos[2], legend = paste("From state",from), lty = seq(lt - 1), col = cols, lwd = lwd)
invisible()
}