brms: how do I set prior on categorical variable?
Asked Answered
C

1

5

I am building a binomial regression model using 2 categorical variables. This is from an example in the book, Statistical rethinking. In the book, while using the rethinking package, we can set priors on each categorical variable as shown below

m11.5 <- ulam(
 alist(
   pulled_left ~ dbinom( 1 , p ) ,
   logit(p) <- a[actor] + b[treatment] ,
   a[actor] ~ dnorm( 0 , 1.5 ),
   b[treatment] ~ dnorm( 0 , 0.5 )
) ,
data=d , chains=4 , log_lik=TRUE )

I am trying to do the same in brms.


priors <- c(prior(normal(0, 1.5), class = b, coef = "actor"),
            prior(normal(0, 0.5), class = b, coef = "treatment"))

m11.5 <- brm(data = d, family = binomial,
          formula = pulled_left | trials(1) ~ 1 + actor + treatment,
          sample_prior = T, prior = priors,
          cores = 4, chains = 4)

I would like to set priors for all of the actor levels and the treatment levels mentioned once. However the above code doesn't go through with the following error message,

prior error

Upon using get_prior, I see the following (implying that the levels are internally split)

get_prior in brms

I donot want to specify the prior for the each level of the categorical variables. How do I do it? Please advice.

Conventional answered 24/11, 2019 at 16:54 Comment(0)
R
7

You can just paste and set multiple coefficients:

priors <- c(prior_string("normal(0, 1.5)", class = "b", coef = paste("actor", 2:7, sep="")),
            prior_string("normal(0, 0.5)", class = "b", coef = paste("treatment", 2:4, sep="")))
Riba answered 29/11, 2019 at 0:47 Comment(4)
This doesn't seem to work, I get the following: Error: The following priors do not correspond to any model parameter: b_paste("actor", 2:7, sep = "") ~ normal(0, 1.5) b_paste("treatment", 2:4, sep = "") ~ normal(0, 0.5) Function 'get_prior' might be helpful to you.Conventional
Switch prior to prior_stringRiba
You also have to update the function call as well, I edited it to change it all.Riba
Thank you @kreuni for the update, I am now able to use your solution. <Fixed typo with my previous post>Conventional

© 2022 - 2024 — McMap. All rights reserved.