---
title: "survHE: Survival analysis in health economic evaluation"
linktitle: "survHE"
author: "Gianluca Baio"
date: 2018-03-10T22:16:14-05:00
publishdate: 2020-10-08
categories: ["Bayesian statistics","Health economics"]
disable_comments: true
weight: 20
links:
  - icon_pack: fab
    icon: github
    name: Code (`survHE`)
    url: 'https://github.com/giabaio/survHE'
  - name: "Go to project site"
    icon: briefcase
    icon_pack: fas
    url: /research/survival
  - icon_pack: fab
    icon: github
    name: Code (`survHEhmc`)
    url: 'https://github.com/giabaio/survHEhmc'
  - name: "Go to project site"
    icon: briefcase
    icon_pack: fas
    url: /research/survival
  - icon_pack: fab
    icon: github
    name: Code (`survHEinla`)
    url: 'https://github.com/giabaio/survHEinla'
  - name: "Go to project site"
    icon: briefcase
    icon_pack: fas
    url: /research/survival
toc: false
bibliography: ["/home/gianluca/Dropbox/Rstuff/Website/publications/mypubs.bib"]
nocite: |
   @baio-2019
    
---

{{< include ../info.qmd >}}

## Survival analysis in health economic evaluation

Contains a suite of functions to streamline systematically the workflow involving survival analysis in health economic evaluation. `survHE` can fit a large range of survival models using both a frequentist approach (by calling the <tt>R</tt> package [flexsurv](https://CRAN.R-project.org/package=flexsurv)) and a Bayesian perspective. For a selected range of models, both Integrated Nested Laplace Integration (via the <tt>R</tt> package [INLA](http://www.r-inla.org/)) and Hamiltonian Monte Carlo (HMC; via the <tt>R</tt> package [rstan](https://CRAN.R-project.org/package=rstan)) are possible. HMC models are pre-compiled so that they can run in a very efficient and fast way. In addition to model fitting, `survHE` provides a set of specialised functions, for example to perform Probabilistic Sensitivity Analysis, export the results of the modelling to a spreadsheet, plotting survival curves and uncertainty around the mean estimates.

`survHE` can take care of the following modelling aspects:

- Reconstruct individual level dataset from digitised data (e.g. from Kaplan-Meier curves)
- Analyse datasets using a hybrid of <tt>R</tt> `formula` and specialised commands, i.e. `fit.models`, which allow the user to select the inferential engine required (`mle`, `inla` or `hmc`), for a range of parametric models (as suggested e.g. by [NICE guidelines](http://nicedsu.org.uk/wp-content/uploads/2016/03/NICE-DSU-TSD-Survival-analysis.updated-March-2013.v2.pdf))
- Perform Probabilistic Sensitivity Analysis directly on the computed parametric survival curves
- Export the output of the statistical model to e.g. a spreadsheet, to complete the economic evaluation (e.g. using Markov models) --- of course this step is **not** necessary and the whole analysis can be embedded in a much bigger (Bayesian) model and performed directly in <tt>R</tt>!

```{r echo=FALSE, out.width="45%", fig.align="center"}
knitr::include_graphics("survHE-schema.png")
```
<center>A schematic representation of the main `survHE` functions and how they interact</center><br><br> 

```{r setup,comment=NA,echo=FALSE,results="hide"}
suppressMessages(library(survHE))
data(bc)
mle=fit.models(formula=Surv(recyrs,censrec)~group,data=bc,distr="exp",method="mle")
inla=suppressMessages(fit.models(formula=Surv(recyrs,censrec)~group,data=bc,distr="exp",method="inla"))
hmc=suppressMessages(fit.models(formula=Surv(recyrs,censrec)~group,data=bc,distr="exp",method="hmc"))
```

<div style="overflow:hidden;">
<div style="float: left;margin-left: 20px;margin-top: 0px;margin-bottom:0px; width:48%;"> 
<!--<div style="display: inline-block">-->

```{r,comment=NA,eval=FALSE}
# Loads the package
library(survHE)
# Loads some data (Breast Cancer from the package 'flexsurv') 
data(bc)
# Fits a model with treatment arm ('group') as the only covariate and 
# accounting for censoring ('censrec') using MLE, INLA or HMC
mle=fit.models(formula=Surv(recyrs,censrec)~group,data=bc,distr="exp",method="mle")
inla=fit.models(formula=Surv(recyrs,censrec)~group,data=bc,distr="exp",method="inla")
hmc=fit.models(formula=Surv(recyrs,censrec)~group,data=bc,distr="exp",method="hmc") 
# Now prints & plots the output
print(mle); print(inla); print(hmc)
plot(MLE=mle,INLA=inla,HMC=hmc,add.km=TRUE,lab.profile=c("Good","Medium","Poor"))
```
<span style="display:block; margin-top:-10px;">

```{r,echo=FALSE,comment=NA,fig.width=10, fig.height=9,out.width="70%"}
plot(MLE=mle,INLA=inla,HMC=hmc,add.km=TRUE,lab.profile=c("Good","Medium","Poor"),legend.position=c(.2,.25))
```
</span>
</div>
<!--<div style="display: inline-block vertical-align:middle">-->

<div style="float: left;margin-left: 20px;margin-top: 0px;margin-bottom:0px; width:48%;"> 

```{r,comment=NA,echo=FALSE}
# Now print the output
print(mle); print(inla); print(hmc)
```
</div>
</div>
A full documentation (published in the *Journal of Statistical Software*) is available [here](https://www.jstatsoft.org/article/view/v095i14).

## Installation
The main module (`survHE`) can be installed directly from [CRAN](https://cran.r-project.org/), running the following commands

```R
install.packages("survHE")
```
This installs on your computer the backbone of the package, but, by default, only allows to do the simplest MLE-based model fitting (using [`flexsurv`](https://CRAN.R-project.org/package=flexsurv) as the underlying inferential engine). It also installs all the modular facilities to pre- and post-processing the output of the model fitting (including plots and summary tables, as well as the probabilistic sensitivity analysis). The basic `survHE` module is also available from its [GitHub repository](https://github.com/giabaio/survHE); intermediate developments (which can be triggered by [pull requests](https://github.com/giabaio/survHE/pulls) or [issues](https://github.com/giabaio/survHE/issues)) can be installed using the following commands:

```R
install.packages(
   "survHE", 
   repos = c("https://giabaio.r-universe.dev", "https://cloud.r-project.org")
)
```

In order to "unlock" the Bayesian modelling modules, they need to be installed separately (see also [here](https://gianluca.statistica.it/blog/2022-01-18-survhe-light/) --- although the methods of installation described in that blog post are now superseded: in previous versions of `survHE`, the two Bayesian modules were stored in "branches" of the main GitHub repository. This was somewhat inefficient and so they now have been promoted to their own separate repositories). Hamiltonian Monte Carlo (HMC) through `rstan` is made available by installing the module `survHEhmc` from its [GitHub repository](https://github.com/giabaio/survHEhmc), using similar commands to above.

```R
# Bayesian models using HMC/Stan
install.packages(
   "survHEhmc", 
   repos = c("https://giabaio.r-universe.dev", "https://cloud.r-project.org"),
   dependencies=TRUE
)
```
This process can be quite lengthy, if you miss many of the relevant packages. Also, the pre-compiled `rstan` models do take some time at *installation* (but this steps produces substantial savings at *compilation* and *running* time --- this is the main reason why `survHEhmc` has been separated by the default installation of `survHE`).

Similarly, the Bayesian module to use Integrated Nested Laplace Approximation through the `INLA` package can be installed from its own [GitHub repository](https://github.com/giabaio/survHEinla) using the following commands.

```R
# Bayesian models using INLA
install.packages(
   "survHEinla", 
   repos = c(
      "https://giabaio.r-universe.dev", 
      "https://cloud.r-project.org",
      "https://inla.r-inla-download.org/R/stable"
   ),
   dependencies=TRUE
)
```

:::{.callout-note}
This is generally rather quick --- the bottle neck here is the installation of [`INLA`](https://www.r-inla.org/download-install). It may be useful to change `R`'s default options in terms of "timeout" (the time spent on a website attempting to download files before giving up), which can be done using the following command (which increases the default from 1 to 10 minutes).
```R
options(timeout=600)
```
This is because `INLA` is a big package and, depending on your internet connection, it may take longer to download from its own repository.
:::

### Installation issues
Previous versions had some installation issues; in particular, installation of the development version via `devtools:install_github()` could fail in a `MS Windows` environment with the following error message:
```R
Error in .shlib_internal(args) : C++14 standard requested but CXX14 is not defined
```
This was due to known issues (see for example [here](https://community.rstudio.com/t/error-in-shlib-internal-args-c-14-standard-requested-but-cxx14-is-not-defined/16819)) with new(er) versions of `rstan` (which `survHE` uses for full Bayesian modelling). `rstan` uses by default version 14 of the `C++` compiler, so `R` needs to know and act accordingly. This can be solved by running the following code

```R
dotR <- file.path(Sys.getenv("HOME"), ".R")
if (!file.exists(dotR)) 
  dir.create(dotR)
M <- file.path(dotR, "Makevars.win")
if (!file.exists(M)) 
  file.create(M)
cat("\nCXX14FLAGS=-O3 -Wno-unused-variable -Wno-unused-function",
    "CXX14 = $(BINPREF)g++ -m$(WIN) -std=c++1y",
    "CXX11FLAGS=-O3 -Wno-unused-variable -Wno-unused-function",
    file = M, sep = "\n", append = TRUE)
```
None of this should affect the current versions of `survHE` and its Bayesian modules.

<br><br>

:::: {.g-col-12}
::: {.last-updated}
Last updated: `r paste(lubridate::day(Sys.Date()),lubridate::month(Sys.Date(),abbr=FALSE,label=TRUE),lubridate::year(Sys.Date()))`
:::
::::

## Relevant publications
