Skip to content

Commit 4f731a9

Browse files
committed
brms-rstan: re-organize static files
1 parent 4740347 commit 4f731a9

10 files changed

Lines changed: 26 additions & 124 deletions

File tree

content/tutorials/r_brms/brms_eng/bayesian_statistics_1_script_eng.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ confint(lm1, level = 0.9)
4848

4949

5050
## -------------------------------------------------------------------------------------------------------------------------------------
51-
source(file = "./source/mcmc_functions.R")
51+
source(file = here("code", "brms_modeling", "mcmc_functions.R"))
5252

5353

5454
## -------------------------------------------------------------------------------------------------------------------------------------
@@ -230,7 +230,7 @@ fit_normal1 <- brm(
230230
family = gaussian(), # we use the Normal distribution
231231
data = ants_df, # specify data
232232
chains = nchains, # MCMC parameters
233-
warmup = burnin,
233+
warmup = burnin,
234234
iter = niter,
235235
cores = nparallel,
236236
thin = thinning,
@@ -335,7 +335,7 @@ mcmc_neff(ratios_fit_normal1) + yaxis_text(hjust = 1)
335335

336336
## ----simple-model-fit1----------------------------------------------------------------------------------------------------------------
337337
# Visualise model fit via bayesplot package
338-
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
338+
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
339339
group = "habitat")
340340

341341

@@ -346,7 +346,7 @@ fit_poisson1 <- brm(
346346
family = poisson(), # we use the Poisson distribution
347347
data = ants_df, # specify the data
348348
chains = nchains, # MCMC parameters
349-
warmup = burnin,
349+
warmup = burnin,
350350
iter = niter,
351351
cores = nparallel,
352352
thin = thinning,
@@ -366,7 +366,7 @@ mcmc_rhat(rhats_fit_poisson1) + yaxis_text(hjust = 1)
366366

367367
## ----poisson-model-fit-vis------------------------------------------------------------------------------------------------------------
368368
# Visualise model fit via bayesplot package
369-
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
369+
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
370370
group = "habitat")
371371

372372

@@ -377,7 +377,7 @@ fit_poisson2 <- brm(
377377
family = poisson(),
378378
data = ants_df,
379379
chains = nchains,
380-
warmup = burnin,
380+
warmup = burnin,
381381
iter = niter,
382382
cores = nparallel,
383383
thin = thinning,
@@ -399,7 +399,7 @@ mcmc_rhat(rhats_fit_poisson2) + yaxis_text(hjust = 1)
399399
## ----rand-intercept-model-fit-vis-----------------------------------------------------------------------------------------------------
400400
# Visualise model fit of the Poisson model with random intercept via
401401
# bayesplot package
402-
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
402+
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
403403
group = "habitat")
404404

405405

@@ -518,7 +518,7 @@ fit_poisson2 %>%
518518
# calculate average numbers and convert to long format for visualisation
519519
mutate(bog = exp(b_Intercept),
520520
forest = exp(b_Intercept + b_habitatForest)) %>%
521-
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
521+
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
522522
values_to = "sp_rich") %>%
523523
# visualise via ggplot()
524524
ggplot(aes(y = sp_rich, x = habitat)) +

content/tutorials/r_brms/brms_eng/workshop_1_mcmc_en_brms_eng.Rmd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ confint(lm1, level = 0.9)
298298
We source some short functions to calculate the (log) likelihood and the prior and to execute the MCMC metropolis algorithm.
299299

300300
```{r}
301-
source(file = "./mcmc_functions.R")
301+
source(file = here("code", "brms_modeling", "mcmc_functions.R"))
302302
```
303303

304304
For this simple model with a small data set, we can calculate and plot the posterior for a large number of combinations of 'beta_0' and 'beta_1'.
@@ -1219,7 +1219,7 @@ conflicted::conflicts_prefer(brms::loo)
12191219
```
12201220

12211221
## Model Definition
1222-
RMarkdown can handle `stan` code chunks, though more general model definition is outsourced to a separate "*.stan" file.
1222+
RMarkdown can handle `stan` code chunks, though more general model definition is outsourced to a separate "*.stan" file (e.g. [here](https://github.com/inbo/tutorials/tree/master/static/code/brms_modeling/poisson_model.stan)).
12231223
Alternatively, you can define your model in a big text block, as shown below.
12241224
The simple poisson model resembles [one of the `stan`-dard examples](https://mc-stan.org/docs/stan-users-guide/posterior-prediction.html#posterior-prediction-for-regressions), which you can refer to for all further details and more.
12251225

@@ -1260,7 +1260,7 @@ When working outside RStudio/RMarkdown, you might prefer loading the model from
12601260

12611261
```{r stan_load_model, eval=TRUE, class.source='fold-show'}
12621262
stan_poisson_model <- stan_model(
1263-
# file = "./poisson_model.stan",
1263+
# file = here("code", "brms_modeling", "poisson_model.stan"),
12641264
model_code = stan_poisson_model_code,
12651265
model_name = "stan poisson model"
12661266
)

content/tutorials/r_brms/brms_nl/bayesian_statistics_1_script.R

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ confint(lm1, level = 0.9)
4848

4949

5050
## -------------------------------------------------------------------------------------------------------------------------------------
51-
source(file = "./source/mcmc_functions.R")
51+
source(file = here::here("code", "brms_modeling", "mcmc_functions.R"))
5252

5353

5454
## -------------------------------------------------------------------------------------------------------------------------------------
@@ -232,7 +232,7 @@ fit_normal1 <- brm(
232232
family = gaussian(), # we gebruiken de Normaal verdeling
233233
data = ants_df, # ingeven data
234234
chains = nchains, # MCMC parameters
235-
warmup = burnin,
235+
warmup = burnin,
236236
iter = niter,
237237
cores = nparallel,
238238
thin = thinning,
@@ -298,7 +298,7 @@ as_draws_df(fit_normal1, variable = parameters) %>%
298298

299299

300300
## ----simpel-model-posterior-density2--------------------------------------------------------------------------------------------------
301-
# Visualisatie density plot van de posterior voor ieder van de chains apart in
301+
# Visualisatie density plot van de posterior voor ieder van de chains apart in
302302
# overlay. via Bayesplot package
303303
mcmc_dens_overlay(fit_normal1, pars = parameters)
304304

@@ -337,7 +337,7 @@ mcmc_neff(ratios_fit_normal1) + yaxis_text(hjust = 1)
337337

338338
## ----simpel-model-fit1----------------------------------------------------------------------------------------------------------------
339339
# Visualiseer model fit via Bayesplot package
340-
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
340+
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
341341
group = "habitat")
342342

343343

@@ -348,7 +348,7 @@ fit_poisson1 <- brm(
348348
family = poisson(), # we gebruiken de Poisson verdeling
349349
data = ants_df, # ingeven data
350350
chains = nchains, # MCMC parameters
351-
warmup = burnin,
351+
warmup = burnin,
352352
iter = niter,
353353
cores = nparallel,
354354
thin = thinning,
@@ -368,7 +368,7 @@ mcmc_rhat(rhats_fit_poisson1) + yaxis_text(hjust = 1)
368368

369369
## ----poisson-model-fit-vis------------------------------------------------------------------------------------------------------------
370370
# Visualiseer model fit via Bayesplot package
371-
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
371+
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
372372
group = "habitat")
373373

374374

@@ -379,7 +379,7 @@ fit_poisson2 <- brm(
379379
family = poisson(),
380380
data = ants_df,
381381
chains = nchains,
382-
warmup = burnin,
382+
warmup = burnin,
383383
iter = niter,
384384
cores = nparallel,
385385
thin = thinning,
@@ -401,7 +401,7 @@ mcmc_rhat(rhats_fit_poisson2) + yaxis_text(hjust = 1)
401401
## ----rand-intercept-model-fit-vis-----------------------------------------------------------------------------------------------------
402402
# Visualiseer model fit van het Poisson model met random intercept via
403403
# Bayesplot package
404-
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
404+
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
405405
group = "habitat")
406406

407407

@@ -520,7 +520,7 @@ fit_poisson2 %>%
520520
# bereken gemiddelde aantallen en zet om naar lang formaat voor visualisatie
521521
mutate(bog = exp(b_Intercept),
522522
forest = exp(b_Intercept + b_habitatForest)) %>%
523-
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
523+
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
524524
values_to = "sp_rich") %>%
525525
# visualiseer via ggplot()
526526
ggplot(aes(y = sp_rich, x = habitat)) +

content/tutorials/r_brms/brms_nl/install_packages.R

Lines changed: 0 additions & 7 deletions
This file was deleted.

content/tutorials/r_brms/brms_nl/mcmc_functions.R

Lines changed: 0 additions & 93 deletions
This file was deleted.

content/tutorials/r_brms/brms_nl/workshop_1_mcmc_en_brms.Rmd

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,8 @@ confint(lm1, level = 0.9)
294294
We lezen enkele korte functies in om de (log) likelihood en de prior te berekenen en het MCMC metropolis algoritme uit te voeren.
295295

296296
```{r}
297-
source(file = "./mcmc_functions.R")
297+
298+
source(file = here("code", "brms_modeling", "mcmc_functions.R"))
298299
```
299300

300301
Voor dit eenvoudig model met een kleine dataset kunnen we de posterior voor een groot aantal combinaties voor `beta_0` en `beta_1` uitrekenen en plotten.
@@ -1206,7 +1207,7 @@ conflicted::conflicts_prefer(brms::loo)
12061207
```
12071208

12081209
## Model Definition
1209-
RMarkdown kan `stan`-chunks verwerken, hoewel de algemenere modeldefinitie is uitbesteed aan een apart "*.stan"-bestand.
1210+
RMarkdown kan `stan`-chunks verwerken, hoewel de algemenere modeldefinitie is uitbesteed aan een apart "*.stan"-bestand (bv. [hier](https://github.com/inbo/tutorials/tree/master/static/code/brms_modeling/poisson_model.stan)).
12101211
Alternatief kan je je model ook als een groot tekstblok definiëren, zoals hieronder te zien.
12111212
Het eenvoudige poissonmodel lijkt op [een van de `stan`-daardvoorbeelden](https://mc-stan.org/docs/stan-users-guide/posterior-prediction.html#posterior-prediction-for-regressions), waarnaar u terecht kunt voor verdere details en meer.
12121213

@@ -1247,7 +1248,7 @@ Als je buiten RStudio/RMarkdown werkt, kun je het model het beste laden vanuit e
12471248

12481249
```{r stan_load_model, eval=TRUE, class.source='fold-show'}
12491250
stan_poisson_model <- stan_model(
1250-
# file = "./poisson_model.stan",
1251+
# file = here("code", "brms_modeling", "poisson_model.stan"),
12511252
model_code = stan_poisson_model_code,
12521253
model_name = "stan poisson model"
12531254
)

content/tutorials/r_brms/index.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@ tags: ["generalized linear regression", "brms", "r", "mixed models", "stan"]
88
---
99

1010
In the fall of 2023, a tutorial on Bayesian statistics with the [**brms**](https://paul-buerkner.github.io/brms/) packages was organised at INBO.
11-
Before you start the tutorial, please follow the instructions in [this R script](https://github.com/inbo/tutorials/blob/master/content/tutorials/r_brms/brms_eng/install_packages.R) to install brms properly.
11+
Before you start the tutorial, please follow the instructions in [this R script](https://github.com/inbo/tutorials/blob/master/static/code/brms_modeling/install_packages.R) to install brms properly.
1212
For more advanced use cases, we added an example of how to directly use [**stan**](https://mc-stan.org) via the `rstan` package.
1313
All course material can be found in Dutch and English on the following pages:
1414

1515
- The [English tutorial](../../html/workshop_1_mcmc_en_brms_eng.html) with all [course material](https://github.com/inbo/tutorials/tree/master/content/tutorials/r_brms/brms_eng) to test the examples yourself.
1616
- The [Dutch tutorial](../../html/workshop_1_mcmc_en_brms.html) with all [course material](https://github.com/inbo/tutorials/tree/master/content/tutorials/r_brms/brms_nl) to test the examples yourself.
17+
- [General files](https://github.com/inbo/tutorials/tree/master/static/code/brms_modeling) useful for both tutorials.
1718

File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)