-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path00-Setup-Instructions.Rmd
143 lines (104 loc) · 5.6 KB
/
00-Setup-Instructions.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
---
title: "Installation Instructions"
author: "Eric Novik, Generable Inc."
date: "18 Jun 2021"
output:
pdf_document:
toc: true
bibliography: references.bib
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
## Introduction
This markdown document explains how to install required software for the Bayesian Survival and Joint Models using Rstanarm for the [NCB 2021 confernece](https://community.amstat.org/biop/events/ncb/index) class taught by [Jacqueline Buros](https://scholar.google.com/citations?user=O_AWJTwAAAAJ&hl=en).
Please, exexecute all steps and verify that the software works before the class. We will not have time to troubleshoot installation problems during the class but if you experience any problems please send us an email to [**ncb2021\@generable.com**](mailto:[email protected]){.email}.
[**Stan**](https://mc-stan.org/) [@carpenter2017] is a programming language with its own compiler and in this class, we will be accessing Stan from R directly via the [RStan](https://mc-stan.org/users/interfaces/rstan) package and indirectly from an [RStanArm](https://mc-stan.org/users/interfaces/rstanarm) package which provides a set of pre-compiled Stan models including Survival models.
## Installing RStan
For this class, you should be using R version 4.0 or later and we recommend using RStudio version 1.4 or later. To install RStan, execute the following commands.
```{r, eval=FALSE}
install.packages("rstan", dependencies = TRUE)
```
On most systems, this should work without a problem, but if you are experiencing installation issues, take a look a the [RStan Getting Started](https://github.com/stan-dev/rstan/wiki/RStan-Getting-Started) document.
Execute the following code to test your installation.
```{r, eval=FALSE}
# In general, you should write your Stan code in a separate file
# but this will work for testing:
stanmodelcode <- "
data {
int<lower=0> N;
real y[N];
}
parameters {
real mu;
}
model {
target += normal_lpdf(mu | 0, 10);
target += normal_lpdf(y | mu, 1);
}
"
library(rstan)
y <- rnorm(20)
dat <- list(N = 20, y = y);
# This will compile the model and start the sampling process
# It may take 10-30 seconds depending on your machine
fit <- stan(model_code = stanmodelcode,
model_name = "example",
data = dat)
print(fit)
```
The last command should produce something like the following.
Inference for Stan model: example.
4 chains, each with iter=2000; warmup=1000; thin=1;
post-warmup draws per chain=1000, total post-warmup draws=4000.
mean se_mean sd 2.5% 25% 50% 75% 97.5% n_eff Rhat
mu -0.08 0.01 0.23 -0.54 -0.24 -0.07 0.08 0.38 1236 1
lp__ -30.48 0.02 0.78 -32.72 -30.63 -30.18 -29.99 -29.94 1246 1
In addition, install the following R packages.
```{r, eval=FALSE}
p <- c('survival', 'bayesplot', 'RISCA', 'tidyverse', 'remotes')
install.packages(p)
# helper functions
remotes::install_github('generable/rgeco')
```
## Installing RStanArm Survival Branch
The other major package we will use is RStanArm. The advantage of RStanArm is that it comes with a large set of pre-compiled models that have been tested for computational efficiency.
As of this writing, the Survival functions are not available on CRAN, and so you have to install the package directly from GitHub as follows.
```{r, eval=FALSE}
# This will take a while -- more than a few minutes as lots of Stan
# models are being compiled. Go have a cup of coffee if that's your thing.
remotes::install_github("stan-dev/rstanarm", ref = "feature/survival", build_vignettes = FALSE)
```
To test if the installation worked execute the following code.
```{r, eval=FALSE}
library(rstanarm)
library(survival)
library(dplyr)
data(leukemia) # from Survival package
fit <- stan_surv(formula = Surv(time, status) ~ x,
data = leukemia,
basehaz = "exp")
print(fit)
```
After executing the print command, you should see something like this.
stan_surv
baseline hazard: exponential
formula: Surv(time, status) ~ x
observations: 23
events: 18 (78.3%)
right censored: 5 (21.7%)
delayed entry: no
------
Median MAD_SD exp(Median)
(Intercept) -4.1 0.4 NA
xNonmaintained 0.9 0.5 2.6
------
* For help interpreting the printed output see ?print.stanreg
* For info on the priors used see ?prior_summary.stanreg
## Getting Help and Other Resouces
For informaiton on the Bayesian Survival analysis see the paper [*Bayesian Survival Analysis Using the rstanarm R*](https://arxiv.org/abs/2002.09633) [@brilleman2020]. For a description of the Joint Models in Bayesian context see the *Joint longitudinal and time-to-event models for multilevel hierarchical data* paper [@brilleman2019].
For the introduction to the RStanArm pacakge see [*How to Use the rstanarm Package*](https://mc-stan.org/rstanarm/articles/rstanarm.html) vignette. For more information on Joint Models in RStanArm see [this](https://cran.r-project.org/web/packages/rstanarm/vignettes/jm.html) and [this](http://mc-stan.org/rstanarm/reference/stan_jm.html).
Stan is extremely well documented. Stan User Manual and other Stan documentation are available [here](https://mc-stan.org/users/documentation/).
If you have a question about Stan including the language and models, check out the user-friendly [community forum](https://discourse.mc-stan.org/).
We hope you engoy the workshop. If there anything we can do to improve, please send us feebback to [**ncb2021\@generable.com**](mailto:[email protected]){.email}**.**
## References