Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ videos
*.mp4

/.quarto/
**/.DS_Store
**/.DS_Store
**/*.quarto_ipynb
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ $(OBJS) : $(SRCDIR)/setup.R

PHONY += clean
clean:
# rm -rf $(OUTDIR)/*
rm -rf $(OUTDIR)/*
rm -rf src/*.html
rm -rf brms-cache
rm -rf .brms-cache
Expand Down
3 changes: 2 additions & 1 deletion src/01a_introduction.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ author:
# Introduction {#sec-introduction}

```{r, include = FALSE}
source(here::here("src/setup.R"))
here::i_am("src/01a_introduction.qmd")
source(here::here("src", "setup.R"))
```

Applied modeling can facilitate interpretation of clinical data and is
Expand Down
34 changes: 19 additions & 15 deletions src/01b_basic_workflow.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ library(dplyr)
library(tidyr)
# other utilities
library(knitr) # used for the "kable" command
library(here) # useful to specify path's relative to project
here::i_am("src/01b_basic_workflow.qmd") # useful to specify path's relative to project
# ...

# instruct brms to use cmdstanr as backend and cache all Stan binaries
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here("_brms-cache"))
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here::here("_brms-cache"))

# create cache directory if not yet available
dir.create(here("_brms-cache"), FALSE)
dir.create(here::here("_brms-cache"), FALSE)

# set the R random seed of the session for reproducibility
set.seed(5886935)
Expand All @@ -91,8 +91,7 @@ set.seed(5886935)
#| eval: true
#| include: false
# invisible to the reader additional setup steps, which are optional
# {{< include setup.R >}}
source("setup.R")
source(here::here("src", "setup.R"))
```

As `brms` models are translated to Stan model files, which must be
Expand All @@ -112,16 +111,21 @@ preamble:
```{r, echo=TRUE, eval=FALSE}
# use 4 cores for parallel sampling by default - only recommended if
# model takes > 1 minute to run for each chain otherwise the
# parallelization introduces substantial overhead
# parallelization introduces substantial overhead.
options(mc.cores = 4)
```

It is discouraged to run chains always in parallel, since the
parallelization itself consumes some ressources and is only beneficial
if the model runtime is at least at the order of minutes. In case the
model runtime becomes excessivley long, then each chain can itself be
run with multiple cores as discussed in the section [Parallel
computation](03b_parallel.qmd).
if the model runtime is at least at the order of minutes. This is in
particular the case if a simulation study is run which itslef is
repeated many times such that the computational resources are already
exhausted by the number of replications the simulation study is
run. In this case the use of any parallelization of the `brms` model
is not advisable. However, when one develops a model and model
runtimes become excessivley long, then each chain can itself be run
with multiple cores as discussed in the section [Parallel
computation](03b_parallel.qmd).

## Model formula

Expand Down Expand Up @@ -211,7 +215,7 @@ the intercept only term is defined. To now define a prior for the
intercept, we may use the `prior` command as:

```{r}
prior_meta_fixed <- prior(normal(0,2), class="Intercept")
prior_meta_fixed <- prior(normal(0,2), class=Intercept)
```

The prior for the random effects model is slightly more complicated as
Expand All @@ -230,7 +234,7 @@ the fixed effect case as:

```{r}
prior_meta_random <- prior_meta_fixed +
prior(normal(0,1), class="sd", coef="Intercept", group="study")
prior(normal(0,1), class=sd, coef=Intercept, group=study)
```

The logic to defined priors on specifc parameters is to use the
Expand All @@ -246,7 +250,7 @@ specific like:

```{r, eval=FALSE}
prior_meta_random <- prior_meta_fixed +
prior(normal(0,1), class="sd")
prior(normal(0, 1), class=sd)
```

This statement assign to *all* random effect standard deviations of
Expand Down Expand Up @@ -297,7 +301,7 @@ parameter one may use:

```{r}
prior_meta_random_alt <- prior_meta_fixed +
prior(normal(0,0.5), class="sd", coef="Intercept", group="study")
prior(normal(0,0.5), class=sd, coef=Intercept, group=study)

fit_meta_random_alt <- update(fit_meta_random, prior=prior_meta_random_alt,
control=list(adapt_delta=0.95),
Expand All @@ -313,7 +317,7 @@ step is avoided, e.g. when looking at data subsets:

```{r}
prior_meta_random_alt <- prior_meta_fixed +
prior(normal(0,0.5), class="sd", coef="Intercept", group="study")
prior(normal(0,0.5), class=sd, coef=Intercept, group=study)

fit_meta_random_alt2 <- update(fit_meta_random, newdata=slice_head(arm_data, n=4),
control=list(adapt_delta=0.95),
Expand Down
9 changes: 4 additions & 5 deletions src/01c_priors.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,18 @@ library(bayesplot)
library(tidybayes)
library(forcats)
library(RBesT)
library(here)
here::i_am("src/01c_priors.qmd")
# instruct brms to use cmdstanr as backend and cache all Stan binaries
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here("_brms-cache"))
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here::here("_brms-cache"))
# create cache directory if not yet available
dir.create(here("_brms-cache"), FALSE)
dir.create(here::here("_brms-cache"), FALSE)
set.seed(593467)
theme_set(theme_bw(12))
```

```{r, include=FALSE, echo=FALSE, eval=TRUE, cache=FALSE}
# invisible to the reader additional setup steps, which are optional
# {{< include setup.R >}}
source(here("src", "setup.R"))
source(here::here("src", "setup.R"))
```

Within the Bayesian regression modeling in Stan framework `brms`
Expand Down
8 changes: 4 additions & 4 deletions src/02a_meta_analysis.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ library(brms)
library(posterior)
library(bayesplot)
library(RBesT)
library(here)
here::i_am("src/02a_meta_analysis.qmd")
# instruct brms to use cmdstanr as backend and cache all Stan binaries
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here("_brms-cache"))
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here::here("_brms-cache"))
# create cache directory if not yet available
dir.create(here("_brms-cache"), FALSE)
dir.create(here::here("_brms-cache"), FALSE)
set.seed(593467)
```

```{r, include=FALSE, echo=FALSE, eval=TRUE,cache=FALSE}
# invisible to the reader additional setup steps, which are optional
source(here("src", "setup.R"))
source(here::here("src", "setup.R"))
```

## Background
Expand Down
9 changes: 4 additions & 5 deletions src/02ab_meta_analysis_trtdiff.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,17 @@ library(ggrepel)
library(brms)
library(posterior)
library(meta)
library(here)
here::i_am("src/02ab_meta_analysis_trtdiff.qmd")
# instruct brms to use cmdstanr as backend and cache all Stan binaries
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here("_brms-cache"))
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here::here("_brms-cache"))
# create cache directory if not yet available
dir.create(here("_brms-cache"), FALSE)
dir.create(here::here("_brms-cache"), FALSE)
set.seed(979356)
```

```{r, include=FALSE, echo=FALSE, eval=TRUE}
# invisible to the reader additional setup steps, which are optional
# {{< include setup.R >}}
source("setup.R")
source(here::here("src", "setup.R"))
```

## Background
Expand Down
9 changes: 4 additions & 5 deletions src/02ac_meta_analysis_strata.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ library(posterior)
library(bayesplot)
library(RBesT)
library(GGally)
library(here)
here::i_am("src/02ac_meta_analysis_strata.qmd")
# instruct brms to use cmdstanr as backend and cache all Stan binaries
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here("_brms-cache"))
options(brms.backend="cmdstanr", cmdstanr_write_stan_file_dir=here::here("_brms-cache"))
# create cache directory if not yet available
dir.create(here("_brms-cache"), FALSE)
dir.create(here::here("_brms-cache"), FALSE)
set.seed(57339)
# allow for wider and prettier printing
options(width=120, digits=2)
Expand All @@ -56,8 +56,7 @@ options(width=120, digits=2)

```{r, include=FALSE, echo=FALSE, eval=TRUE}
# invisible to the reader additional setup steps, which are optional
# {{< include setup.R >}}
source("setup.R")
source(here::here("src", "setup.R"))
```

## Background
Expand Down
Loading