Skip to content

Commit 75efd94

Browse files
committed
brms-rstan: knit files; image issues
1 parent 4f731a9 commit 75efd94

5 files changed

Lines changed: 583 additions & 282 deletions

File tree

.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/workshop_1_mcmc_en_brms_eng.Rmd

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ conflicted::conflicts_prefer(brms::pstudent_t)
4343
conflicted::conflicts_prefer(brms::qstudent_t)
4444
conflicted::conflicts_prefer(brms::rstudent_t)
4545
conflicted::conflict_prefer("rhat", "brms")
46+
47+
# reduce html export file size by specifying graphics size
48+
knitr::opts_chunk$set(dev = "jpeg", dpi = 56,
49+
fig.width = 4, fig.height = 3,
50+
out.width = "60%")
4651
```
4752

4853
# Theoretical background
@@ -240,7 +245,7 @@ When taking a step uphill, the MCMC always continues. If the step is downhill, t
240245
The absolute height of the mountain (posterior likelihood) in itself does not matter. Only the relative differences or shape of the mountain matters.
241246
This means that we do not need to calculate the denominator of Bayes' rule, which is the reason why the algorithm is frequently used in Bayesian statistics.
242247

243-
![Metropolis rules](../media/MCMC4b.png)
248+
![Metropolis rules](./mcmc4b.png)
244249

245250
A simple MCMC algorithm is the Metropolis algorithm. It is described by these formal rules:
246251

@@ -298,7 +303,7 @@ confint(lm1, level = 0.9)
298303
We source some short functions to calculate the (log) likelihood and the prior and to execute the MCMC metropolis algorithm.
299304

300305
```{r}
301-
source(file = here("code", "brms_modeling", "mcmc_functions.R"))
306+
source(file = here("static", "code", "brms_modeling", "mcmc_functions.R"))
302307
```
303308

304309
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'.
@@ -520,7 +525,7 @@ Some alternatives also exist:
520525
- [**rstan**](https://cran.r-project.org/web/packages/rstan/vignettes/rstan.html) is the basis of **brms**. You need **rstan** to use **brms** because it communicates directly with Stan. A model composed in "pure" Stan code can be fitted with the **rstan** package in R (so without using **brms**), but can be quite complex.
521526
- [**rstanarm**](https://mc-stan.org/rstanarm/articles/index.html) is similar to **brms** in terms of complexity of the syntax. Moreover, with the **rstanarm** package you can use some pre-compiled Stan models. This means that fitting your model takes less time. A large part of the process time of **brms** goes into compiling the Stan program.
522527

523-
![Overview of various Stan software (source: https://jtimonen.github.io/posts/post-01/)](software.png)
528+
![Overview of various Stan software (source: https://jtimonen.github.io/posts/post-01/)](./stan_software.png)
524529

525530

526531
Although the package **brms** depends on **rstan** as a default backend, there is another software tool worth mentioning: **cmdstanr**.
@@ -953,7 +958,7 @@ Based on the PPCs we can already see which model fits the data best. Furthermore
953958

954959
Cross-validation (CV) is a family of techniques that attempts to estimate how well a model would predict unknown data through predictions of the model fitted to the known data. You do not necessarily have to collect new data for this. You can split your own data into a test and training dataset. You fit the model to the training dataset and then use that model to estimate how well it can predict the data in the test dataset. With leave-one-out CV (LOOCV) you leave out one observation each time as test dataset and refit the model based on all other observations (= training dataset).
955960

956-
![Figure LOOCV.](loocv.png)
961+
![Figure LOOCV.](./loocv.png)
957962

958963
```{r compare-loocv}
959964
# Add leave-one-out model fit criterium to model objects
@@ -989,7 +994,7 @@ comp_loo %>%
989994

990995
With K-fold cross-validation, the data is split into $K$ groups. We will use $K = 10$ groups (= folds) here. So instead of leaving out a single observation each time, as with leave-one-out CV, we will leave out one $10^{th}$ of the data here. Via the arguments `folds = "stratified"` and `group = "habitat"` we ensure that the relative frequencies of habitat are preserved for each group. This technique will therefore be less precise than the previous one, but will be faster to calculate if you work with a lot of data.
991996

992-
![Figure K-fold CV.](k-foldcv.png)
997+
![Figure K-fold CV.](./k-foldcv.png)
993998

994999
```{r compare-K-fold}
9951000
# Add K-fold model fit criterium to model objects

content/tutorials/r_brms/brms_nl/workshop_1_mcmc_en_brms.Rmd

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ conflicted::conflicts_prefer(brms::pstudent_t)
4242
conflicted::conflicts_prefer(brms::qstudent_t)
4343
conflicted::conflicts_prefer(brms::rstudent_t)
4444
conflicted::conflict_prefer("rhat", "brms")
45+
46+
# html bestandsgrootte verminderen door grafiekparameters te specifiëren
47+
knitr::opts_chunk$set(dev = "jpeg", dpi = 56,
48+
fig.width = 4, fig.height = 3,
49+
out.width = "60%")
4550
```
4651

4752
# Theoretische achtergrond
@@ -235,7 +240,7 @@ Bij een stap bergop gaat de MCMC altijd door. Is de stap bergaf, dan gaat de MCM
235240

236241
De absolute hoogte van de berg (posterior likelihood) doet er op zich niet toe. Enkel de relatieve verschillen.
237242

238-
![Metropolis regels](../media/MCMC4b.png)
243+
![Metropolis regels](./mcmc4b.png)
239244

240245
Een eenvoudig MCMC-algoritme is het Metropolis algoritme. Het wordt beschreven door deze formele regels:
241246

@@ -295,7 +300,7 @@ We lezen enkele korte functies in om de (log) likelihood en de prior te berekene
295300

296301
```{r}
297302
298-
source(file = here("code", "brms_modeling", "mcmc_functions.R"))
303+
source(file = here("static", "code", "brms_modeling", "mcmc_functions.R"))
299304
```
300305

301306
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.
@@ -508,7 +513,7 @@ Er bestaan ook enkele alternatieven:
508513
- [RStan](https://cran.r-project.org/web/packages/rstan/vignettes/rstan.html) is de basis van `brms`. Je hebt `RStan`nodig om `brms`te kunnen gebruiken omdat het rechtstreeks met Stan communiceert. Een model definiëren in "Pure" Stan code is echter redelijk omslachtig.
509514
- [rstanarm](https://mc-stan.org/rstanarm/articles/index.html) is nog makkelijker dan `brms`qua syntax. Bovendien kan je met `rstanarm` gebruik maken van enkele vooral gecompileerde Stan modellen. Hierdoor duurt het fitten van je model minder lang. Een groot deel van de procestijd van `brms`gaat immers naar het compileren van het Stan programma.
510515

511-
![Overzicht van verschillende Stan software (bron: https://jtimonen.github.io/posts/post-01/)](software.png)
516+
![Overzicht van verschillende Stan software (bron: https://jtimonen.github.io/posts/post-01/)](./stan_software.png)
512517

513518
Hoewel het pakket **brms** afhankelijk is van **rstan** als standaard backend, is er nog een andere softwaretool die het vermelden waard is: **cmdstanr**.
514519
[Vergeleken met **rstan** profiteert **cmdstanr**]( https://mc-stan.org/cmdstanr/articles/cmdstanr.html#comparison-with-rstan) van frequentere updates en enkele performance-voordelen, maar vereist het [extra installatiestappen](https://mc-stan.org/cmdstanr/articles/cmdstanr.html#installing-cmdstan).
@@ -944,7 +949,7 @@ Op basis van de PPCs kunnen we reeds zien welk model het best past bij de data.
944949

945950
Cross-validation (CV) is een familie van technieken die probeert in te schatten hoe goed een model onbekende data zou voorspellen via predicties van het model gefit op de bekende data. Hiervoor moet je niet per se nieuwe data gaan inzamelen. Je kan jouw eigen data opsplitsen in een test en training dataset. Je fit het model op de training dataset en je gebruikt dat model dan om te schatten hoe goed het de data in de test dataset kan voorspellen. Bij leave-one-out CV (LOOCV) ga je telkens één observatie weglaten en het model opnieuw fitten o.b.v. alle andere observaties.
946951

947-
![Illustratie LOOCV.](loocv.png)
952+
![Illustratie LOOCV.](./loocv.png)
948953

949954
```{r vergelijken-loocv}
950955
# Voeg leave-one-out model fit criterium toe aan model objecten
@@ -980,7 +985,7 @@ comp_loo %>%
980985

981986
Bij K-fold cross-validation worden de data in $K$ groepen opgesplitst. Wij zullen hier $K = 10$ groepen (= folds) gebruiken. In plaats van dus telkens één enkele observatie weg te laten zoals bij leave-one-out CV gaan we hier $1/10$e van de data weglaten. Via de argumenten `folds = "stratified"` en `group = "habitat"` zorgen we ervoor dat voor elke groep de relatieve frequenties van habitat bewaard blijven. Deze techniek zal dus minder precies zijn dan de vorige, maar zal sneller zijn om te berekenen indien je met heel veel data werkt.
982987

983-
![Illustratie K-fold CV.](k-foldcv.png)
988+
![Illustratie K-fold CV.](./k-foldcv.png)
984989

985990
```{r vergelijken-K-fold}
986991
# Voeg K-fold model fit criterium toe aan model objecten

static/html/workshop_1_mcmc_en_brms.html

Lines changed: 283 additions & 139 deletions
Large diffs are not rendered by default.

static/html/workshop_1_mcmc_en_brms_eng.html

Lines changed: 276 additions & 133 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)