---
title: "Multistate model and treatment switching"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Multistate model and treatment switching}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 6, fig.height = 4)
```

```{r setup}
library(DesignComplexSurvival)
```

`ms_model()` defines the model of one treatment group. The patient moves
between five states with piecewise constant intensities:

1. progression-free and alive,
2. progressed without switch,
3. switched,
4. dead,
5. progression-free after a switch before progression (used only when
   `switch_pre` is positive).

Progression-free survival (PFS) is the time to leave the progression-free
states 1 and 5, and overall survival (OS) is the time to enter state 4.
Before progression the intensities are functions of the time since
randomization; after progression they are functions of the time since
progression or since randomization (the clock, see below). Dropout is
independent of the event process. This article shows how switching at, after
and before progression enters the model, and how the options change OS, PFS
and the power of the log-rank test.

## Switching at and after progression

In the control group the hazard of progression is 0.10 per month in the
first 4 months and 0.08 afterwards. At progression, 30 percent of the
patients switch to the experimental treatment, and the others may switch
later with a hazard of 0.04 per month. After progression the hazard of death
is 0.06 without switch and 0.035 after a switch. The treatment group has a
lower hazard of progression and no switching.

```{r switch-models}
ctl_sw <- ms_model(time = c(0, 4), prog = c(0.10, 0.08), death = 0.01,
                   switch_prob = 0.3, death_prog = 0.06, switch_rate = 0.04,
                   death_switch = 0.035, dropout = 0.003)
trt_sw <- ms_model(time = c(0, 4), prog = c(0.06, 0.05), death = 0.008,
                   death_prog = 0.055, dropout = 0.003)
pr <- ms_prob(ctl_sw, time = c(6, 12, 24))
pr
```

The state probabilities do not include dropout. At 24 months,
`r round(100 * pr$switched[pr$time == 24])` percent of the control patients
are alive after a switch and `r round(100 * pr$dead[pr$time == 24])` percent
have died.

The control group without switching has the same intensities with
`switch_prob` and `switch_rate` set to zero. The OS curves of the three
models:

```{r os-curves, fig.alt = "Overall survival of the control group with and without switching and of the treatment group over 48 months"}
ctl_ns <- ms_model(time = c(0, 4), prog = c(0.10, 0.08), death = 0.01,
                   death_prog = 0.06, dropout = 0.003)
models <- list(control = ctl_sw, control_no_switch = ctl_ns, treatment = trt_sw)
tt <- seq(0, 48, by = 1)
os <- sapply(models, function(m) ms_surv(m, tt, "os")$surv)
matplot(tt, os, type = "l", lty = 1:3, col = 1, ylim = c(0, 1),
        xlab = "Months since randomization", ylab = "Overall survival")
legend("topright", c("Control with switching", "Control without switching",
                     "Treatment"), lty = 1:3, bty = "n")
median_os <- function(m) {
  stats::uniroot(function(t) ms_surv(m, t, "os")$surv - 0.5, c(0.1, 500))$root
}
med <- sapply(models, median_os)
round(med, 1)
```

The median OS is `r round(med[["control"]], 1)` months in the control group
with switching, `r round(med[["control_no_switch"]], 1)` months without
switching and `r round(med[["treatment"]], 1)` months in the treatment group.
PFS does not depend on the switching after progression.

The design has an interim analysis at 60 percent of the final 300 OS events
and 500 patients enrolled at 25 per month:

```{r switch-power}
looks <- ms_looks("os", fraction = c(0.6, 1))
pw <- ms_power(ms_scenario(ctl_sw, trt_sw, accrual = ms_accrual(rate = 25)),
               n = 500, looks = looks, test = ms_test(), events = 300)
pw_ns <- ms_power(ms_scenario(ctl_ns, trt_sw, accrual = ms_accrual(rate = 25)),
                  n = 500, looks = looks, test = ms_test(), events = 300)
c(switching = pw$power, no_switching = pw_ns$power)
```

Switching to the experimental treatment in the control group moves the
hazard ratio for OS towards 1: the power is `r round(pw$power, 3)` with
switching and `r round(pw_ns$power, 3)` without.

## The clock of the post-progression intensities

The post-progression intensities are piecewise constant on intervals that
start at `time_post`. With `clock = "reset"` (the default) the intervals
refer to the time since progression (a semi-Markov model); with
`clock = "forward"` they refer to the time since randomization (a Markov
model). In the following model the hazard of death after progression is 0.08
in the first 6 months and 0.04 afterwards:

```{r clocks}
m_reset <- ms_model(prog = 0.08, death = 0.01, time_post = c(0, 6),
                    death_prog = c(0.08, 0.04))
m_fwd <- ms_model(prog = 0.08, death = 0.01, time_post = c(0, 6),
                  death_prog = c(0.08, 0.04), clock = "forward")
tc <- c(12, 24, 36)
os_clock <- sapply(list(reset = m_reset, forward = m_fwd),
                   function(m) ms_surv(m, tc, "os")$surv)
rownames(os_clock) <- paste("month", tc)
round(os_clock, 3)
```

With the reset clock, a patient who progresses at month 10 has the hazard
0.08 until month 16. With the forward clock the hazard is 0.04 from month 6
on, so this patient has the hazard 0.04 at once, and OS is longer. PFS is the
same under both clocks. When the post-progression intensities are constant,
the two clocks give the same model.

## The death hazard after a switch

By default (`switch_clock = "same"`) the hazard `death_switch` is measured on
the clock of the other post-progression intensities. With
`switch_clock = "switch"` it is measured from the time of the switch. In the
following model the patients switch after progression with a hazard of 0.05
per month, and the death hazard after a switch is 0.03 in the first interval
and 0.05 afterwards:

```{r switch-clock}
m_same <- ms_model(prog = 0.08, death = 0.01, time_post = c(0, 6),
                   death_prog = c(0.08, 0.06), switch_rate = 0.05,
                   death_switch = c(0.03, 0.05))
m_since <- ms_model(prog = 0.08, death = 0.01, time_post = c(0, 6),
                    death_prog = c(0.08, 0.06), switch_rate = 0.05,
                    death_switch = c(0.03, 0.05), switch_clock = "switch")
os_switch <- sapply(list(same = m_same, since_switch = m_since),
                    function(m) ms_surv(m, tc, "os")$surv)
rownames(os_switch) <- paste("month", tc)
round(os_switch, 3)
```

A patient who switches 4 months after progression has the hazard 0.03 for 2
months with `"same"` and for 6 months with `"switch"`. With
`clock = "reset"` the two choices differ only for switches after
progression (`switch_rate`); switches at progression (`switch_prob`) start
both clocks at the same time.

## An accelerated failure time effect of the switch

With `switch_af`, the switch has an accelerated failure time effect: the
remaining time after the switch is the remaining time without switch divided
by `switch_af`, as in the rank preserving structural failure time model of
Robins and Tsiatis (1991). A value below 1 prolongs survival. When
`death_prog` is constant, this is the same as the death hazard
`switch_af * death_prog` after the switch:

```{r aft-constant}
m_af1 <- ms_model(prog = 0.08, death = 0.01, death_prog = 0.06,
                  switch_rate = 0.05, switch_af = 0.7)
m_ph1 <- ms_model(prog = 0.08, death = 0.01, death_prog = 0.06,
                  switch_rate = 0.05, death_switch = 0.7 * 0.06)
d_af <- max(abs(ms_surv(m_af1, tt, "os")$surv - ms_surv(m_ph1, tt, "os")$surv))
d_af
```

The largest difference between the two OS curves over 48 months is
`r format(signif(d_af, 2))`. When `death_prog` changes over time, the
accelerated failure time effect also changes the time scale on which the
hazard after the switch is read, and the two models differ:

```{r aft-piecewise}
m_af2 <- ms_model(prog = 0.08, death = 0.01, time_post = c(0, 6),
                  death_prog = c(0.08, 0.04), switch_rate = 0.05, switch_af = 0.7)
m_ph2 <- ms_model(prog = 0.08, death = 0.01, time_post = c(0, 6),
                  death_prog = c(0.08, 0.04), switch_rate = 0.05,
                  death_switch = 0.7 * c(0.08, 0.04))
os_af <- sapply(list(aft = m_af2, hazard_ratio = m_ph2),
                function(m) ms_surv(m, tc, "os")$surv)
rownames(os_af) <- paste("month", tc)
round(os_af, 3)
```

## Switching before progression

A patient who is progression-free may switch with the hazard `switch_pre`
and enters state 5. There the hazards of progression and death are
`prog_switch` and `death_switch_pre`; at progression the patient enters
state 3 and dies with the hazard `death_switch`.

```{r switch-pre}
ctl_pre <- ms_model(time = c(0, 4), prog = c(0.10, 0.08), death = 0.01,
                    death_prog = 0.06, death_switch = 0.035, switch_pre = 0.02,
                    prog_switch = 0.06, death_switch_pre = 0.007, dropout = 0.003)
ms_prob(ctl_pre, time = c(6, 12, 24))
```

The column `progression_free_switched` is the probability of state 5.
`pfs_switch` in `ms_scenario()` sets whether PFS is followed up to
progression or death after such a switch (`"event"`, the default) or
censored at the switch (`"censor"`, for example when the start of a new
treatment censors PFS). The censoring appears among the PFS dropouts of
`ms_events()`:

```{r pfs-switch}
acc <- ms_accrual(rate = 25)
scn_event <- ms_scenario(ctl_pre, trt_sw, accrual = acc)
scn_censor <- ms_scenario(ctl_pre, trt_sw, accrual = acc, pfs_switch = "censor")
ev <- rbind(event = ms_events(scn_event, n = 500, calendar_time = 24, endpoint = "pfs"),
            censor = ms_events(scn_censor, n = 500, calendar_time = 24, endpoint = "pfs"))
ev[, c("events_control", "dropouts_control", "events_treatment", "dropouts_treatment")]
pfs_power <- sapply(list(event = scn_event, censor = scn_censor), function(s)
  ms_power(s, n = 500, looks = ms_looks("pfs"), test = ms_test(endpoints = "pfs"),
           events = 300)$power)
pfs_power
```

With 300 PFS events at a single analysis, the power of the PFS test is
`r round(pfs_power[["event"]], 3)` when the switch does not change the
definition of PFS and `r round(pfs_power[["censor"]], 3)` when it censors
PFS.

## Censoring of PFS only

`dropout` censors both endpoints. `dropout_pfs` adds a censoring of PFS
only, for example the end of tumor assessments:

```{r dropout-pfs}
ctl_dp <- ms_model(time = c(0, 4), prog = c(0.10, 0.08), death = 0.01,
                   death_prog = 0.06, dropout = 0.003, dropout_pfs = 0.02)
scn_dp <- ms_scenario(ctl_dp, trt_sw, accrual = acc)
rbind(pfs = ms_events(scn_dp, n = 500, calendar_time = 24, endpoint = "pfs"),
      os = ms_events(scn_dp, n = 500, calendar_time = 24, endpoint = "os"))[
  , c("events_control", "dropouts_control")]
```

## References

Robins JM, Tsiatis AA (1991). Correcting for non-compliance in randomized
trials using rank preserving structural failure time models. Communications
in Statistics - Theory and Methods 20:2609-2631.
