Skip to content

Commit 8c46fb7

Browse files
authored
Merge pull request #346 from inbo/rstan
BRMS tutorial: add `rstan` example
2 parents fde1619 + 75efd94 commit 8c46fb7

14 files changed

Lines changed: 1047 additions & 440 deletions

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,7 @@ content/**/*.html
6262

6363
# Local folders with binary data
6464
data_gisclub/
65+
66+
# png images for the brms tutorial
67+
/content/tutorials/r_brms/brms_eng/*.png
68+
/content/tutorials/r_brms/brms_nl/*.png

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: 214 additions & 24 deletions
Large diffs are not rendered by default.

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.

0 commit comments

Comments
 (0)