Skip to content

Commit bfa0a19

Browse files
committed
brms_rstan: windows testing, index, author
1 parent 7b170ac commit bfa0a19

4 files changed

Lines changed: 37 additions & 12 deletions

File tree

content/tutorials/r_brms/brms_eng/workshop_1_mcmc_en_brms_eng.Rmd

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ library(tidybayes) # wrangling and visualising Bayesian models
3636
# Conflicten tussen packages
3737
conflicted::conflicts_prefer(dplyr::filter)
3838
conflicted::conflicts_prefer(dplyr::lag)
39+
conflicted::conflicts_prefer(tidyr::extract)
3940
conflicted::conflicts_prefer(brms::ar)
4041
conflicted::conflicts_prefer(brms::dstudent_t)
4142
conflicted::conflicts_prefer(brms::pstudent_t)
@@ -716,7 +717,7 @@ as_draws_df(fit_normal1, variable = parameters) %>%
716717
pivot_longer(cols = starts_with(parameters), names_to = "name",
717718
values_to = "value") %>%
718719
# split columns names at the last underscore
719-
extract(name, into = c("parameter", "statistic"), "(.*)_([^_]+)$") %>%
720+
tidyr::extract(name, into = c("parameter", "statistic"), "(.*)_([^_]+)$") %>%
720721
ggplot(aes(y = value, x = .iteration, colour = factor(.chain))) +
721722
geom_line(aes(linetype = statistic), linewidth = 0.8) +
722723
facet_wrap(~parameter, nrow = 3, scales = "free")
@@ -1087,10 +1088,12 @@ conflicted::conflicts_prefer(brms::loo)
10871088

10881089
### Model Definition
10891090
RMarkdown can handle `stan` code chunks, though more general model definition is outsourced to a separate "*.stan" file.
1091+
Alternatively, you can define your model in a big text block, as shown below.
10901092
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.
10911093

10921094

1093-
```{stan, output.var="stan_poisson_model", class.source='fold-show'}
1095+
```{r stan_poisson_simple, eval=TRUE, output.var="", class.source='fold-show'}
1096+
stan_poisson_model_code = '
10941097
data {
10951098
int<lower=1> N; // sample size
10961099
vector[N] habitat_obs; // habitat
@@ -1112,7 +1115,7 @@ model {
11121115
11131116
}
11141117
1115-
1118+
'
11161119
```
11171120

11181121

@@ -1123,9 +1126,10 @@ Yet note how even Stan is not without convenience functions: we can use the `poi
11231126

11241127
When working outside RStudio/RMarkdown, you might prefer loading the model from a file:
11251128

1126-
```{r stan_load_model, eval=FALSE, class.source='fold-show'}
1129+
```{r stan_load_model, eval=TRUE, class.source='fold-show'}
11271130
stan_poisson_model <- stan_model(
1128-
file = "./poisson_model.stan",
1131+
# file = "./poisson_model.stan",
1132+
model_code = stan_poisson_model_code,
11291133
model_name = "stan poisson model"
11301134
)
11311135
```

content/tutorials/r_brms/brms_nl/workshop_1_mcmc_en_brms.Rmd

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ library(tidybayes) # nabewerking en visualisatie van Bayesiaanse modellen
3535
# Conflicten tussen packages
3636
conflicted::conflicts_prefer(dplyr::filter)
3737
conflicted::conflicts_prefer(dplyr::lag)
38+
conflicted::conflicts_prefer(tidyr::extract)
3839
conflicted::conflicts_prefer(brms::ar)
3940
conflicted::conflicts_prefer(brms::dstudent_t)
4041
conflicted::conflicts_prefer(brms::pstudent_t)
@@ -705,7 +706,7 @@ as_draws_df(fit_normal1, variable = parameters) %>%
705706
pivot_longer(cols = starts_with(parameters), names_to = "name",
706707
values_to = "value") %>%
707708
# splits naam kolom op bij laatste underscore
708-
extract(name, into = c("parameter", "statistiek"), "(.*)_([^_]+)$") %>%
709+
tidyr::extract(name, into = c("parameter", "statistiek"), "(.*)_([^_]+)$") %>%
709710
ggplot(aes(y = value, x = .iteration, colour = factor(.chain))) +
710711
geom_line(aes(linetype = statistiek), linewidth = 0.8) +
711712
facet_wrap(~parameter, nrow = 3, scales = "free")
@@ -1079,9 +1080,12 @@ conflicted::conflicts_prefer(brms::loo)
10791080
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.
10801081

10811082
RMarkdown kan `stan`-chunks verwerken, hoewel de algemenere modeldefinitie is uitbesteed aan een apart "*.stan"-bestand.
1083+
Alternatief kan je je model ook als grote tekstblok definiëren, zoals hieronder te zien.
10821084
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.
10831085

1084-
```{stan, output.var="stan_poisson_model", class.source='fold-show'}
1086+
1087+
```{r stan_poisson_simple, eval=TRUE, output.var="", class.source='fold-show'}
1088+
stan_poisson_model_code = '
10851089
data {
10861090
int<lower=1> N; // sample size
10871091
vector[N] habitat_obs; // habitat
@@ -1103,19 +1107,21 @@ model {
11031107
11041108
}
11051109
1106-
1110+
'
11071111
```
11081112

11091113

1114+
11101115
Zie dit als een "kijkje achter de schermen"!
11111116
Je moet expliciet de modelstructuur, priors en zelfs de gegevenstypen van invoervariabelen definiëren.
11121117
Maar let op hoe zelfs Stan niet zonder handige functies is: we kunnen de `poisson_log` posterior gebruiken om de $log(\lambda )$ te modelleren.
11131118

11141119
Als je buiten RStudio/RMarkdown werkt, kun je het model het beste laden vanuit een bestand:
11151120

1116-
```{r stan_load_model, eval=FALSE, class.source='fold-show'}
1121+
```{r stan_load_model, eval=TRUE, class.source='fold-show'}
11171122
stan_poisson_model <- stan_model(
1118-
file = "./poisson_model.stan",
1123+
# file = "./poisson_model.stan",
1124+
model_code = stan_poisson_model_code,
11191125
model_name = "stan poisson model"
11201126
)
11211127
```

content/tutorials/r_brms/index.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
---
22
title: "Tutorial Bayesian statistics with brms"
33
description: "This tutorial starts with a theoretical overview of bayesian statistics and the MCMC algorithm. Next, we fit, check, and analyse Bayesian models with the brms package."
4-
author: [raisacarmen, wardlangeraert, toonvandaele]
4+
author: [raisacarmen, wardlangeraert, toonvandaele, falkmielke]
55
date: 2024-01-02
66
categories: ["r", "statistics"]
7-
tags: ["generalized linear regression", "brms", "r", "mixed models"]
7+
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.
1111
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.
12+
For more advanced use cases, we added an example of how to directly use [**stan**](https://mc-stan.org) via the `rstan` package.
1213
All course material can be found in Dutch and English on the following pages:
1314

1415
- 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.
1516
- 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+

data/authors/falkmielke.toml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
id = "falkmielke"
2+
3+
[email]
4+
username = ""
5+
host = ""
6+
7+
[name]
8+
display = "Falk Mielke"
9+
10+
[social]
11+
email = ""
12+
facebook = ""
13+
twitter = ""

0 commit comments

Comments
 (0)