-
Notifications
You must be signed in to change notification settings - Fork 25
/
j_survival.Rmd
231 lines (190 loc) · 5.31 KB
/
j_survival.Rmd
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
---
title: "Survival models"
date: "`r Sys.Date()`"
output:
rmarkdown::html_vignette:
toc: true
vignette: >
%\VignetteIndexEntry{Survival models}
%\VignetteEngine{knitr::rmarkdown}
\usepackage[utf8]{inputenc}
---
```{r setup, include=FALSE}
library(heemod)
```
## Distributions
The `heemod` package provides a number of ways to estimate transition probabilities from survival distributions. Survival distributions can come from at least three different sources:
* User-definded parametric distributions.
* Fitted parametric distributions with `flexsurv::flexsurvreg()`.
* Fitted Kaplan-Meiers with `survival::survfit()`.
Once defined, each of these types of distributions can be combined and modified using a standard set of operations.
User-defined parametric distributions are created using the `define_survival()` and `define_spline_survival()` functions:
```{r}
surv_dist_1 <- define_survival(
distribution = "exp",
rate = .5
)
surv_dist_2 <- define_spline_survival(
scale = "odds",
gamma = c(-11.643, 1.843, 0.208),
knots = c(4.077537, 5.883183, 6.458338)
)
```
Fitted parametric models are created using `flexsurv::flexsurvreg()` and `flexsurv::flexsurvspline()`:
```{r fig.width=6, fig.height=6}
library(flexsurv)
fit_w <- flexsurvreg(
formula = Surv(futime, fustat) ~ 1,
data = ovarian, dist = "weibull"
)
plot(fit_w)
fit_spl <- flexsurvspline(
formula = Surv(futime, fustat) ~ 1,
data = ovarian,
scale = "odds",
k=1
)
plot(fit_spl)
```
Fitted models can include covariates. In order to use a model with covariates in heemod, you can use the `set_covariates()` function on the fitted model and provide as additional arguments the covariate values you want to model. You can also provide a data frame of covariate levels to aggregate survival probabilites over different groups. By default, heemod will aggregate over predicted survival probabilities for each subject in the dataset to which the model was fit.
```{r fig.width=6, fig.height=6}
fit_cov <- flexsurvreg(
formula = Surv(rectime, censrec) ~ group,
dist = "weibull",
data = bc
)
plot(fit_cov)
fitcov_good <- set_covariates(fit_cov, group = "Good")
fitcov_medium <- set_covariates(fit_cov, group = "Medium")
fitcov_poor <- set_covariates(fit_cov, group = "Poor")
```
Similar functionality is also available for Kaplan-Meiers created using `survival::survfit()`
```{r fig.width=6, fig.height=6}
library(survival)
km_1 <- survfit(
formula = Surv(futime, fustat) ~ 1,
data = ovarian
)
km_cov <- survfit(
formula = Surv(rectime, censrec) ~ group,
data = bc
)
plot(km_cov)
km_good <- set_covariates(km_cov, group = "Good")
km_medium <- set_covariates(km_cov, group = "Medium")
km_poor <- set_covariates(km_cov, group = "Poor")
```
## Operations
Once defined, treatment effects of various types can be applied to any survival distribution:
* Hazard ratio: `apply_hr()`.
* Odds ratio: `apply_or()`.
* Acceleration factor: `apply_af()`.
```{r}
km_poor_ph <- apply_hr(km_poor, hr = 0.5)
km_medium_af <- apply_af(km_medium, af = 1.2)
```
In addition, distributions can be combined using a variety of operations:
* Join survival distributions together: `join()`.
* Mix two (or more) survival distributions: `mix()`.
* Combine two (or more) survival distributions as independent risks: `add_hazards()`.
```{r}
km_poor_join <- join(
km_poor,
fitcov_poor,
at = 365
)
models_all <- mix(
fitcov_good, fitcov_medium, fitcov_poor,
weights = c(0.25, 0.25, 0.5)
)
combined_risks <- add_hazards(
fit_w, fitcov_good
)
```
The transition or survival probabilities are computed with `compute_surv()`. Time (usually `model_time` or `state_time`) needs to be passed to the function as a `time` argument.
```{r}
compute_surv(surv_dist_2, time = 1:5)
```
All these operations can be chained with the `%>%` piping operator.
```{r}
fit_cov %>%
set_covariates(group = "Good") %>%
apply_hr(hr = 2) %>%
join(
fitcov_poor,
at = 3
) %>%
mix(
fitcov_medium,
weights = c(0.25, 0.75)
) %>%
add_hazards(
fit_w
) %>%
compute_surv(time = 1:5)
```
## Applications
### Transition matrix
For the example we define a simple model with only 1 strategy.
```{r fig.width=6, fig.height=6}
param <- define_parameters(
p1 = compute_surv(
surv_dist_1,
time = model_time # can also be state_time
),
p2 = km_1 %>%
join(fit_w, at = 730) %>%
compute_surv(
time = model_time,
cycle_length = 365 # time is in days in km_medium, in years in model_time
)
)
tm <- define_transition(
C, p1 - p2, p2,
0, C, p2,
0, 0, C
)
plot(tm)
sA <- define_state(
cost = 10, ut = 1
)
sB <- define_state(
cost = 20, ut = .5
)
sC <- define_state(
cost = 0, ut = 0
)
stratTM <- define_strategy(
transition = tm,
A = sA, B = sB, C = sC
)
resTM <- run_model(
parameters = param,
stratTM,
cycles = 15,
cost = cost, effect = ut
)
```
```{r fig.width=6, fig.height=4}
plot(resTM)
```
### Partitioned survival model
A partitioned survival model can also be computed:
```{r fig.width=6, fig.height=4}
ps <- define_part_surv(
pfs = surv_dist_1,
os = km_1 %>%
join(fit_w, at = 730),
cycle_length = c(1, 365) # 1 for pfs, 365 for os
)
stratPS <- define_strategy(
transition = ps,
A = sA, B = sB, C = sC
)
resPS <- run_model(
stratPS,
cycles = 15,
cost = cost, effect = ut
)
plot(resPS)
```