You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
# 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%")
46
51
```
47
52
48
53
# Theoretical background
@@ -240,7 +245,7 @@ When taking a step uphill, the MCMC always continues. If the step is downhill, t
240
245
The absolute height of the mountain (posterior likelihood) in itself does not matter. Only the relative differences or shape of the mountain matters.
241
246
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.
242
247
243
-

248
+

244
249
245
250
A simple MCMC algorithm is the Metropolis algorithm. It is described by these formal rules:
246
251
@@ -298,7 +303,7 @@ confint(lm1, level = 0.9)
298
303
We source some short functions to calculate the (log) likelihood and the prior and to execute the MCMC metropolis algorithm.
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:
520
525
-[**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.
521
526
-[**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.
522
527
523
-

528
+

524
529
525
530
526
531
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
953
958
954
959
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).
955
960
956
-

961
+

957
962
958
963
```{r compare-loocv}
959
964
# Add leave-one-out model fit criterium to model objects
@@ -989,7 +994,7 @@ comp_loo %>%
989
994
990
995
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.
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:
508
513
-[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.
509
514
-[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.
510
515
511
-

516
+

512
517
513
518
Hoewel het pakket **brms** afhankelijk is van **rstan** als standaard backend, is er nog een andere softwaretool die het vermelden waard is: **cmdstanr**.
514
519
[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.
944
949
945
950
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.
946
951
947
-

952
+

948
953
949
954
```{r vergelijken-loocv}
950
955
# Voeg leave-one-out model fit criterium toe aan model objecten
@@ -980,7 +985,7 @@ comp_loo %>%
980
985
981
986
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.
982
987
983
-

988
+

984
989
985
990
```{r vergelijken-K-fold}
986
991
# Voeg K-fold model fit criterium toe aan model objecten
0 commit comments