Express in stan_glm and brms a model written in rstan?

Hello to all,
The linear predictor in a model already defined in rstan is of the below form:

  vector[n_games] mu =team_abil[home_team]-team_abil[away_team];
  // priors including all constants 
  target += normal_lpdf(c |0,10); 
  target += normal_lpdf(team_abil_raw |0,10); 
  //likelihood-systematic component
  for (g in 1:n_games) {
    target += ordered_logistic_lpmf(dif_sets[g] | mu[g], c);

How can mu= mu =team_abil[home_team]-team_abil[away_team]; be expressed in brms or stan_glm formula without considering two parameters (one for the home team and one for the away team) and simultaneously preserve the - sign between them?

Also, I would like to ask If the brms library supports sum to zero cornstraint as the stan_glm (contrasts=sum())

Hi, sorry for not getting to you earlier.

I don’t think the model as you’ve written it can be easily expressed in brms. You could possible hack around brms to make it do this (as brms let’s you inject custom Stan code in a lot of places), but I don’t think you could do this in any easy manner.

I don’t think this is supported directly, but note that contrasts = sum is not a constraint on the parameters, but rather just a different way to build to model matrix, so you should be able to always express by building part of the model matrix as additional columns in your data.

Best of luck with your model!

I think that I found a solution. By specifying

train_data$w1 ← rep(1, 1000)
train_data$w2 ← rep(-1, 1000)

and the linear predictor in brms should be written by this way
+(1|mm(own_team, opp_team, weights = cbind(w1, w2),scale=F))

my question was solved. However, the sum-to-zero constraint in the categorical variable was not easy for me to find in bia the brms function.

Thank you for your time and effort @martinmodrak

1 Like