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:

```
<- define_survival(
surv_dist_1 distribution = "exp",
rate = .5
)
<- define_spline_survival(
surv_dist_2 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()`

:

```
library(flexsurv)
<- flexsurvreg(
fit_w formula = Surv(futime, fustat) ~ 1,
data = ovarian, dist = "weibull"
)plot(fit_w)
<- flexsurvspline(
fit_spl 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.

```
<- flexsurvreg(
fit_cov formula = Surv(rectime, censrec) ~ group,
dist = "weibull",
data = bc
)plot(fit_cov)
<- set_covariates(fit_cov, group = "Good")
fitcov_good <- set_covariates(fit_cov, group = "Medium")
fitcov_medium <- set_covariates(fit_cov, group = "Poor") fitcov_poor
```

Similar functionality is also available for Kaplan-Meiers created using `survival::survfit()`

```
library(survival)
<- survfit(
km_1 formula = Surv(futime, fustat) ~ 1,
data = ovarian
)<- survfit(
km_cov formula = Surv(rectime, censrec) ~ group,
data = bc
)plot(km_cov)
<- set_covariates(km_cov, group = "Good")
km_good <- set_covariates(km_cov, group = "Medium")
km_medium <- set_covariates(km_cov, group = "Poor") km_poor
```

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()`

.

```
<- apply_hr(km_poor, hr = 0.5)
km_poor_ph <- apply_af(km_medium, af = 1.2) km_medium_af
```

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()`

.

```
<- join(
km_poor_join
km_poor,
fitcov_poor,at = 365
)<- mix(
models_all
fitcov_good, fitcov_medium, fitcov_poor,weights = c(0.25, 0.25, 0.5)
)<- add_hazards(
combined_risks
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.

`compute_surv(surv_dist_2, time = 1:5)`

All these operations can be chained with the `%>%`

piping operator.

```
%>%
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)
```

For the example we define a simple model with only 1 strategy.

```
<- define_parameters(
param 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
)
)
<- define_transition(
tm - p2, p2,
C, p1 0, C, p2,
0, 0, C
)
plot(tm)
<- define_state(
sA cost = 10, ut = 1
)<- define_state(
sB cost = 20, ut = .5
)<- define_state(
sC cost = 0, ut = 0
)
<- define_strategy(
stratTM transition = tm,
A = sA, B = sB, C = sC
)
<- run_model(
resTM parameters = param,
stratTM,cycles = 15,
cost = cost, effect = ut
)
```

`plot(resTM)`

A partitioned survival model can also be computed:

```
<- define_part_surv(
ps pfs = surv_dist_1,
os = km_1 %>%
join(fit_w, at = 730),
cycle_length = c(1, 365) # 1 for pfs, 365 for os
)
<- define_strategy(
stratPS transition = ps,
A = sA, B = sB, C = sC
)
<- run_model(
resPS
stratPS,cycles = 15,
cost = cost, effect = ut
)
plot(resPS)
```