To specify priors for the individual interactions between the Advertisement
variables and State
values (which, as other commenters have noted, is only possible for fixed effects), you need to use the names that brms is using internally for those coefficients. Below is an example with the penguins
data.
Given a model formula and a data frame, the get_prior()
function returns the coefficients in the model. The coef
column contains the names brms
is using for these coefficients, which will guide you in specifying priors.
library(palmerpenguins)
library(brms)
form = bill_length_mm ~ species*sex
get_prior(formula=form, data=penguins)
#> prior class coef group resp dpar nlpar
#> (flat) b
#> (flat) b sexmale
#> (flat) b speciesChinstrap
#> (flat) b speciesChinstrap:sexmale
#> (flat) b speciesGentoo
#> (flat) b speciesGentoo:sexmale
#> student_t(3, 44.5, 7) Intercept
#> student_t(3, 0, 7) sigma
#> lb ub source
#> default
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> (vectorized)
#> default
#> 0 default
No we can set priors for the interactions. The code below sets a global fixed effects prior of normal(0, 1) that will be applied to all fixed effects that don’t have a specific prior, and then sets specific priors for the interactions.
priors = c(prior(normal(0, 1), class="b"),
prior(normal(0, 0.5), class="b", coef="speciesChinstrap:sexmale"),
prior(normal(0, 2), class="b", coef="speciesGentoo:sexmale"))
Then, the model fitting code would be:
m = brm(formula=form, prior=priors, data=penguins)
So, in your case, run get_prior()
on your model formula and data, then use the coef
names to target the specific coefficients (in this case, the interactions between the Advertisement
variables and State
) for which you want to set individual priors.
In cases where you have lots of priors to set, there are also ways to reduce the amount of typing necessary to set individual priors. For example, in the code below we set a global prior of normal(0, 1)
for the fixed effects, which will be applied to all fixed effects coefficients without an explicit prior, and then set specific priors for each of the interaction coefficients.
library(tidyverse)
# Filter priors to target the global fixed effect prior and the interaction priors
priors = get_prior(formula=form, data=penguins) %>%
filter(class=="b", grepl(":", coef) | coef=="")
priors
#> prior class coef group resp dpar nlpar lb ub source
#> (flat) b default
#> (flat) b speciesChinstrap:sexmale (vectorized)
#> (flat) b speciesGentoo:sexmale (vectorized)
# Set each prior to our desired values
priors = priors %>%
mutate(prior = paste0("normal(0, ", c(1, 0.5, 2), ")"))
priors
#> prior class coef group resp dpar nlpar lb ub
#> normal(0, 1) b
#> normal(0, 0.5) b speciesChinstrap:sexmale
#> normal(0, 2) b speciesGentoo:sexmale
#> source
#> default
#> default
#> default
# Fit the model with the chosen priors
m = brm(formula=form, prior=priors, data=penguins)