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)library(brms)
Loading required package: Rcpp
Loading 'brms' package (version 2.21.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:stats':
ar
library(tidybayes)
Attaching package: 'tidybayes'
The following objects are masked from 'package:brms':
dstudent_t, pstudent_t, qstudent_t, rstudent_t
library(tidyr)theme_set(theme_minimal())penguins <- penguins |>filter(!is.na(sex), species =="Chinstrap") ggplot(penguins, aes(bill_depth_mm, bill_length_mm, color = sex)) +geom_point()
The model
Say we want to model bill_length as a function of both bill_depth and sex.
There are different approaches to adding a categorial predictor to a model, sometimes called “indicator” (or “dummy”) variables, or “index” variables. McElreath comes down strongly in favor of using “index” variables. For more discussion, see here and links therein.
The code
Fitting the model
First we can fit a model that will just fit different intercepts for the different species:
Warning:
2 (2.9%) p_waic estimates greater than 0.4. We recommend trying loo instead.
depth_length_sex_brm$criteria
$waic
Computed from 2000 by 68 log-likelihood matrix.
Estimate SE
elpd_waic -80.2 5.7
p_waic 4.1 1.3
waic 160.3 11.3
2 (2.9%) p_waic estimates greater than 0.4. We recommend trying loo instead.
$loo
Computed from 2000 by 68 log-likelihood matrix.
Estimate SE
elpd_loo -80.3 5.7
p_loo 4.2 1.4
looic 160.5 11.4
------
MCSE of elpd_loo is 0.2.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.2, 1.1]).
All Pareto k estimates are good (k < 0.7).
See help('pareto-k-diagnostic') for details.
Compare this to our old model, with no term for sex:
Warning:
1 (1.5%) p_waic estimates greater than 0.4. We recommend trying loo instead.
depth_length_brm$criteria
$waic
Computed from 2000 by 68 log-likelihood matrix.
Estimate SE
elpd_waic -89.2 6.8
p_waic 3.6 1.7
waic 178.4 13.5
1 (1.5%) p_waic estimates greater than 0.4. We recommend trying loo instead.
$loo
Computed from 2000 by 68 log-likelihood matrix.
Estimate SE
elpd_loo -89.2 6.8
p_loo 3.6 1.7
looic 178.4 13.5
------
MCSE of elpd_loo is 0.1.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.7, 1.0]).
All Pareto k estimates are good (k < 0.7).
See help('pareto-k-diagnostic') for details.