For inspiration for code, have a look at the BDA R Demos and the specific Assignment code notebooks
Recommended additional self study exercises for each chapter in BDA3 are listed in the course web page. These will help to gain deeper understanding of the topic.
Deadlines for all assignments can be found on the course web page and in MyCourses.
You are allowed to discuss assignments with your friends, but it is not allowed to copy solutions directly from other students or from internet.
Do not share your answers publicly.
Do not copy answers from the internet or from previous years. We compare the answers to the answers from previous years and to the answers from other students this year.
If you have any suggestions or improvements to the course material, please post in the course chat feedback channel, create an issue, or submit a pull request to the public repository!
if (!require(brms)) {install.packages("brms")library(brms)}
Loading required package: brms
Loading required package: Rcpp
Loading 'brms' package (version 2.22.0). Useful instructions
can be found by typing help('brms'). A more detailed introduction
to the package is available through vignette('brms_overview').
Attaching package: 'brms'
The following object is masked from 'package:stats':
ar
cmdstan_installed <-function(){ res <-try(out <- cmdstanr::cmdstan_path(), silent =TRUE)!inherits(res, "try-error")}if(!cmdstan_installed()){install_cmdstan()}
2 Sleep deprivation
The dataset sleepstudy is available by using the command data(sleepstudy, package = "lme4")
Below is some code for fitting a brms model. This model is a simple pooled model. You will need to fit a hierarchical model as explained in the assignment, but this code should help getting started.
Load the dataset
data(sleepstudy, package ="lme4")
Error in find.package(package, lib.loc, verbose = verbose): there is no package called 'lme4'
prior class coef group resp dpar nlpar lb ub source
normal(250, 100) b Intercept <NA> <NA> user
normal(0, 20) b Days <NA> <NA> user
normal(0, 100) sigma <NA> <NA> user
---title: "Notebook for Assignment 8"author: "Aki Vehtari et al."format: html: toc: true code-tools: true code-line-numbers: true number-sections: true mainfont: Georgia, serifeditor: source---# General informationThis assignment relates to Lectures 8-9 and Chapter 7 of BDA3We recommend using [JupyterHub](https://jupyter.cs.aalto.fi) (which has all the needed packages pre-installed).::: hint**Reading instructions:**- [**The reading instructions for BDA3 Chapter 6**](../BDA3_notes.html#ch6) (posterior predictive checking).- [**The reading instructions for BDA3 Chapter 7**](../BDA3_notes.html#ch7) (predictive performance).- The [‘loo‘ package vignette on the basics of LOO](https://mc-stan.org/loo/articles/loo2-with-rstan.html)shows an example of how to modify Stan code and use the package with Stan models.- Also readabout PSIS-LOO in the [PSIS-LOO paper](https://link.springer.com/article/10.1007/s11222-016-9696-4).- [CV-FAQ](https://avehtari.github.io/modelselection/CV-FAQ.html) includes a lot of informative answers to frequent questions and misconceptions.:::{{< include includes/_general_cloze_instructions.md >}}```{r}if (!require(brms)) {install.packages("brms")library(brms)}if(!require(cmdstanr)){install.packages("cmdstanr", repos =c("https://mc-stan.org/r-packages/", getOption("repos")))library(cmdstanr)}cmdstan_installed <-function(){ res <-try(out <- cmdstanr::cmdstan_path(), silent =TRUE)!inherits(res, "try-error")}if(!cmdstan_installed()){install_cmdstan()}```# Sleep deprivationThe dataset `sleepstudy` is available by using the command`data(sleepstudy, package = "lme4")`Below is some code for fitting a brms model. This model is a simplepooled model. You will need to fit a hierarchical model as explainedin the assignment, but this code should help getting started.Load the dataset```{r}data(sleepstudy, package ="lme4")```Specify the formula and observation family:```{r}sleepstudy_pooled_formula <-bf( Reaction ~1+ Days,family ="gaussian",center =FALSE)```We can then specify the priors:```{r}(sleepstudy_pooled_priors <-c(prior(normal(250, 100),class ="b",coef ="Intercept" ),prior(normal(0, 20),class ="b",coef ="Days" ),prior(normal(0, 100),class ="sigma" )))```And then fit the model:```{r}sleepstudy_pooled_fit <-brm(formula = sleepstudy_pooled_formula,prior = sleepstudy_pooled_priors,data = sleepstudy)```We can add the leave-one-out CV criterion to the fit object, and then view the output```{r}sleepstudy_pooled_fit <-add_criterion( sleepstudy_pooled_fit,criterion ="loo")loo(sleepstudy_pooled_fit)```# Sleep deprivation extensionHere is an example of a pooled model with spline```{r}sleepstudy_pooled_spline_formula <-bf( Reaction ~1+ Days +s(Days),family ="gaussian",center =FALSE)sleepstudy_pooled_spline_priors <-c(prior(normal(250, 100),class ="b",coef ="Intercept" ),prior(normal(0, 20),class ="b",coef ="Days" ),prior(normal(0, 100),class ="sigma" ),prior(normal(0, 20),class ="b",coef ="sDays_1" ))sleepstudy_pooled_spline_fit <-brm(formula = sleepstudy_pooled_spline_formula,prior = sleepstudy_pooled_spline_priors,data = sleepstudy,family ="gaussian")```Adding the loo criterion:```{r}sleepstudy_pooled_spline_fit <-add_criterion( sleepstudy_pooled_spline_fit,criterion ="loo")```Compare to the other model```{r}loo_compare(loo(sleepstudy_pooled_fit),loo(sleepstudy_pooled_spline_fit))```If there are high Pareto-k valuesFirst try moment matching:```{r}sleepstudy_pooled_spline_fit <-add_criterion( sleepstudy_pooled_spline_fit,criterion ="loo",moment_match =TRUE,overwrite =TRUE)```Then if there are a few remaining (and refitting is not going to take forever), try moment matching and reloo```{r}sleepstudy_pooled_spline_fit <-add_criterion( sleepstudy_pooled_spline_fit,criterion ="loo",moment_match =TRUE,reloo =TRUE,overwrite =TRUE)```