Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BRMS tutorial: add rstan example #346

Merged
merged 14 commits into from
Dec 6, 2024
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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,7 @@ content/**/*.html

# Local folders with binary data
data_gisclub/

# png images for the brms tutorial
/content/tutorials/r_brms/brms_eng/*.png
/content/tutorials/r_brms/brms_nl/*.png
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ confint(lm1, level = 0.9)


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


## -------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -230,7 +230,7 @@ fit_normal1 <- brm(
family = gaussian(), # we use the Normal distribution
data = ants_df, # specify data
chains = nchains, # MCMC parameters
warmup = burnin,
warmup = burnin,
iter = niter,
cores = nparallel,
thin = thinning,
Expand Down Expand Up @@ -335,7 +335,7 @@ mcmc_neff(ratios_fit_normal1) + yaxis_text(hjust = 1)

## ----simple-model-fit1----------------------------------------------------------------------------------------------------------------
# Visualise model fit via bayesplot package
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
group = "habitat")


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

## ----poisson-model-fit-vis------------------------------------------------------------------------------------------------------------
# Visualise model fit via bayesplot package
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
group = "habitat")


Expand All @@ -377,7 +377,7 @@ fit_poisson2 <- brm(
family = poisson(),
data = ants_df,
chains = nchains,
warmup = burnin,
warmup = burnin,
iter = niter,
cores = nparallel,
thin = thinning,
Expand All @@ -399,7 +399,7 @@ mcmc_rhat(rhats_fit_poisson2) + yaxis_text(hjust = 1)
## ----rand-intercept-model-fit-vis-----------------------------------------------------------------------------------------------------
# Visualise model fit of the Poisson model with random intercept via
# bayesplot package
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
group = "habitat")


Expand Down Expand Up @@ -518,7 +518,7 @@ fit_poisson2 %>%
# calculate average numbers and convert to long format for visualisation
mutate(bog = exp(b_Intercept),
forest = exp(b_Intercept + b_habitatForest)) %>%
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
values_to = "sp_rich") %>%
# visualise via ggplot()
ggplot(aes(y = sp_rich, x = habitat)) +
Expand Down
238 changes: 214 additions & 24 deletions content/tutorials/r_brms/brms_eng/workshop_1_mcmc_en_brms_eng.Rmd

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions content/tutorials/r_brms/brms_nl/bayesian_statistics_1_script.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ confint(lm1, level = 0.9)


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


## -------------------------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -232,7 +232,7 @@ fit_normal1 <- brm(
family = gaussian(), # we gebruiken de Normaal verdeling
data = ants_df, # ingeven data
chains = nchains, # MCMC parameters
warmup = burnin,
warmup = burnin,
iter = niter,
cores = nparallel,
thin = thinning,
Expand Down Expand Up @@ -298,7 +298,7 @@ as_draws_df(fit_normal1, variable = parameters) %>%


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

Expand Down Expand Up @@ -337,7 +337,7 @@ mcmc_neff(ratios_fit_normal1) + yaxis_text(hjust = 1)

## ----simpel-model-fit1----------------------------------------------------------------------------------------------------------------
# Visualiseer model fit via Bayesplot package
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
pp_check(fit_normal1, type = "dens_overlay_grouped", ndraws = 100,
group = "habitat")


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

## ----poisson-model-fit-vis------------------------------------------------------------------------------------------------------------
# Visualiseer model fit via Bayesplot package
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
pp_check(fit_poisson1, type = "dens_overlay_grouped", ndraws = 100,
group = "habitat")


Expand All @@ -379,7 +379,7 @@ fit_poisson2 <- brm(
family = poisson(),
data = ants_df,
chains = nchains,
warmup = burnin,
warmup = burnin,
iter = niter,
cores = nparallel,
thin = thinning,
Expand All @@ -401,7 +401,7 @@ mcmc_rhat(rhats_fit_poisson2) + yaxis_text(hjust = 1)
## ----rand-intercept-model-fit-vis-----------------------------------------------------------------------------------------------------
# Visualiseer model fit van het Poisson model met random intercept via
# Bayesplot package
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
pp_check(fit_poisson2, type = "dens_overlay_grouped", ndraws = 100,
group = "habitat")


Expand Down Expand Up @@ -520,7 +520,7 @@ fit_poisson2 %>%
# bereken gemiddelde aantallen en zet om naar lang formaat voor visualisatie
mutate(bog = exp(b_Intercept),
forest = exp(b_Intercept + b_habitatForest)) %>%
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
pivot_longer(cols = c("bog", "forest"), names_to = "habitat",
values_to = "sp_rich") %>%
# visualiseer via ggplot()
ggplot(aes(y = sp_rich, x = habitat)) +
Expand Down
7 changes: 0 additions & 7 deletions content/tutorials/r_brms/brms_nl/install_packages.R

This file was deleted.

93 changes: 0 additions & 93 deletions content/tutorials/r_brms/brms_nl/mcmc_functions.R

This file was deleted.

Loading
Loading