This is the template for assignment 8. You can download the qmd-file or copy the code from this rendered document after clicking on </> Code in the top right corner.
Please replace the instructions in this template by your own text, explaining what you are doing in each exercise.
Setup
This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission.Make sure that this does not get displayed in the PDF!
The following loads several needed packages:
library(bayesplot)
This is bayesplot version 1.10.0
- Online documentation and vignettes at mc-stan.org/bayesplot
- bayesplot theme set to bayesplot::theme_default()
* Does _not_ affect other ggplot2 plots
* See ?bayesplot_theme_set for details on theme setting
library(cmdstanr)
This is cmdstanr version 0.5.3
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- CmdStan path: /root/.cmdstan/cmdstan-2.31.0
- CmdStan version: 2.31.0
A newer version of CmdStan is available. See ?install_cmdstan() to install it.
To disable this check set option or environment variable CMDSTANR_NO_VER_CHECK=TRUE.
library(dplyr)
Attaching package: 'dplyr'
The following objects are masked from 'package:stats':
filter, lag
The following objects are masked from 'package:base':
intersect, setdiff, setequal, union
library(ggplot2)library(ggdist) # for stat_dotsintervallibrary(posterior)
This is posterior version 1.4.0
Attaching package: 'posterior'
The following object is masked from 'package:bayesplot':
rhat
The following objects are masked from 'package:stats':
mad, sd, var
The following objects are masked from 'package:base':
%in%, match
library(brms)
Loading required package: Rcpp
Loading 'brms' package (version 2.19.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 objects are masked from 'package:ggdist':
dstudent_t, pstudent_t, qstudent_t, rstudent_t
The following object is masked from 'package:bayesplot':
rhat
The following object is masked from 'package:stats':
ar
# Globally specfiy cmdstan backend for brmsoptions(brms.backend="cmdstanr")# Tell brms to cache results if possibleoptions(brms.file_refit="on_change")# Set more readable themes with bigger font for plotting packagesggplot2::theme_set(theme_minimal(base_size =14))bayesplot::bayesplot_theme_set(theme_minimal(base_size =14))
2 A hierarchical model for chicken weight time series
In brms, a regression can be specified as below, see also below (#m) or the last template. Fill in the appropriate variables, data, and likelihood family. Specify the priors, then run the model (by removing #| eval: false below).
priors <-c(prior(normal(0, <value>), coef ="Time"),prior(normal(0, <value>), coef ="Diet2"),prior(normal(0, <value>), coef ="Diet3"),prior(normal(0, <value>), coef ="Diet4"))f1 <- brms::brm(# This specifies the formula<OUTCOME>~1+<PREDICTOR>+<PREDICTOR>,# This specifies the datasetdata =<data>,# This specifies the observation model familyfamily =<observation_family>,# This passes the priors specified above to brmsprior = priors,# This causes brms to cache the resultsfile ="additional_files/assignment8/f1")
2.6 (d)
# Useful functions: brms::pp_check
2.7 (e)
# Useful functions: brms::pp_check(..., type = ..., group=...)
This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission.Make sure that this does not get displayed in the PDF!
Creating a dummy fit just to be able to generate an example plot below. Generate a similar plot for your hierarchical model.
# The brms-formula (weights ~ ...) below is not one that you should be using in your models!dummy_fit <- brms::brm( weight ~1+ Time + Chick,data = ChickWeight,file="additional_files/assignment8/dummy_fit")# Adjust the chicken_idxs variable to select appropriate chickenschicken_idxs =c(1,3,11,43)# Create this plot for your hierarchical model for selected chickensbrms::pp_check( dummy_fit, type ="intervals_grouped", group ="Chick", newdata=ChickWeight |>filter(Chick %in% chicken_idxs))
2.17 Model comparison using the RMSE
2.18 (l)
rmse function implementation
This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission.Make sure that this does not get displayed in the PDF!
The below function takes a brms fit object and computes either the root-mean-square error (RMSE) or the PSIS-LOO-RMSE, i.e. the RMSE using LOO-CV estimated using PSIS-LOO.
---title: "Assignment 8"subtitle: "LOO-CV model comparison"author: anonymous # <-- hand in anonymouslyformat: html: toc: true code-tools: true code-line-numbers: true number-sections: true mainfont: Georgia, serif page-layout: article pdf: geometry: - left=1cm,top=1cm,bottom=1cm,right=7cm number-sections: true code-annotations: noneeditor: source---# General informationThis is the template for [assignment 8](assignment8.html). You can download the [qmd-file](https://avehtari.github.io/BDA_course_Aalto/assignments/template8.qmd) or copy the code from this rendered document after clicking on `</> Code` in the top right corner.**Please replace the instructions in this template by your own text, explaining what you are doing in each exercise.** :::: {.content-hidden when-format="pdf"}::: {.callout-warning collapse=false}## Setup*This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission.***Make sure that this does not get displayed in the PDF!**The following loads several needed packages:```{r}#| label: importslibrary(bayesplot)library(cmdstanr)library(dplyr)library(ggplot2)library(ggdist) # for stat_dotsintervallibrary(posterior)library(brms)# Globally specfiy cmdstan backend for brmsoptions(brms.backend="cmdstanr")# Tell brms to cache results if possibleoptions(brms.file_refit="on_change")# Set more readable themes with bigger font for plotting packagesggplot2::theme_set(theme_minimal(base_size =14))bayesplot::bayesplot_theme_set(theme_minimal(base_size =14))```:::::::# A hierarchical model for chicken weight time series## Exploratory data analysis## (a)```{r}# Useful functions: ggplot, aes(x=...), geom_histogram```## (b)```{r}# Useful functions: ggplot, aes(x=...,y=...,group=...,color=...), geom_line```## Linear regression## (c)In `brms`, a regression can be specified as below, see also [below (#m)](#m) or [the last template](template7.html#b-1). Fill in the appropriate variables,data, and likelihood family. Specify the priors, then run the model (by removing `#| eval: false` below).```{r}#| eval: falsepriors <-c(prior(normal(0, <value>), coef ="Time"),prior(normal(0, <value>), coef ="Diet2"),prior(normal(0, <value>), coef ="Diet3"),prior(normal(0, <value>), coef ="Diet4"))f1 <- brms::brm(# This specifies the formula<OUTCOME>~1+<PREDICTOR>+<PREDICTOR>,# This specifies the datasetdata =<data>,# This specifies the observation model familyfamily =<observation_family>,# This passes the priors specified above to brmsprior = priors,# This causes brms to cache the resultsfile ="additional_files/assignment8/f1")```## (d)```{r}# Useful functions: brms::pp_check```## (e)```{r}# Useful functions: brms::pp_check(..., type = ..., group=...)```## Log-normal linear regression## (f)```{r}log_priors <-c(prior(normal(0, log(3)), coef ="Time"),prior(normal(0, log(5)), coef ="Diet2"),prior(normal(0, log(5)), coef ="Diet3"),prior(normal(0, log(5)), coef ="Diet4"))```## Hierarchical log-normal linear regression## (g)## (h)## Model comparison using the ELPD## (i)```{r}# Useful functions: loo, loo_compare```## (j)```{r}# Useful functions: plot(loo(...), label_points = TRUE)```## (k):::: {.content-hidden when-format="pdf"}::: {.callout-warning collapse=false}## Creating a dummy example plot *This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission.***Make sure that this does not get displayed in the PDF!**Creating a dummy fit just to be able to generate an example plot below. Generate a similar plot for your hierarchical model.```{r}#| output: false# The brms-formula (weights ~ ...) below is not one that you should be using in your models!dummy_fit <- brms::brm( weight ~1+ Time + Chick,data = ChickWeight,file="additional_files/assignment8/dummy_fit")# Adjust the chicken_idxs variable to select appropriate chickenschicken_idxs =c(1,3,11,43)# Create this plot for your hierarchical model for selected chickensbrms::pp_check( dummy_fit, type ="intervals_grouped", group ="Chick", newdata=ChickWeight |>filter(Chick %in% chicken_idxs))```:::::::## Model comparison using the RMSE## (l):::: {.content-hidden when-format="pdf"}::: {.callout-warning collapse=false}## `rmse` function implementation*This block will only be visible in your HTML output, but will be hidden when rendering to PDF with quarto for the submission.***Make sure that this does not get displayed in the PDF!**The below function takes a brms fit object and computes either the [root-mean-square error (RMSE)](https://en.wikipedia.org/wiki/Root-mean-square_deviation) or the PSIS-LOO-RMSE, i.e. the RMSE using LOO-CV estimated using PSIS-LOO.```{r}# Compute RMSE or LOO-RMSErmse <-function(fit, use_loo=FALSE){ mean_y_pred <-if(use_loo){ brms::loo_predict(fit) }else{colMeans(brms::posterior_predict(fit)) }sqrt(mean( (mean_y_pred - brms::get_y(fit))^2 ))}```:::::::