 # Explicit subtraction of effects in brms

I wrote a model in Stan to fit the goal difference `goal_diff` of a football match as a function of the difference in strength between the `home` team and the `away` team. This is the data:

``````data {
int<lower=1> N;  // number of observations
int<lower=1> T;  // number of teams
int<lower=1,upper=T> home[N];  // home team
int<lower=1,upper=T> away[N];  // away team
vector[N] goal_diff;  // goal difference
}
``````

And this is the relevant part of the model:

``````model {
real goal_diff_hat;
for (i in 1:N) {
goal_diff_hat = Intercept + b[home[i]] - b[away[i]];
target += double_exponential_lpdf(goal_diff[i] | goal_diff_hat, s);
}
}
``````

It works fine, and has nice results. Evidently, two data `int`s are used to estimate a single effect `vector`. I would like to fit this model with brms, as it’s easier to iterate over different models in it. Initially I thought I could do something with the multi-membership `mm` helper, but the explicit subtraction in `b[home[i]] - b[away[i]]` is necessary to correctly estimate `b`. Is it possible to code this model with a brms formula?

You can use negative weights in mm() to achieve this.

I had overlooked this! Thanks, Paul.

Actually, with brms 2.13.0, when I try to reproduce the `mm` example with negative weights:

``````dat <- data.frame(
y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100),
g1 = sample(1:10, 100, TRUE), g2 = sample(1:10, 100, TRUE)
)
dat\$w1 <- rep(1, 100)
dat\$w2 <- rep(-1, 100)

fit <- brm(
y ~ x1 + (1|mm(g1, g2, weights = cbind(w1, w2))),
data = dat
)
``````

I get:

``````Error: Cannot scale negative weights.
``````

With traceback:

``````7: stop(..., call. = FALSE)
6: stop2("Cannot scale negative weights.")
5: data_gr_local(x, data = data, ranef = ranef)
4: data_predictor.brmsterms(bterms, data = data, prior = prior,
data2 = data2, ranef = ranef, basis = basis)
3: data_predictor(bterms, data = data, prior = prior, data2 = data2,
ranef = ranef, basis = basis)
2: .make_standata(bterms, data = data, prior = prior, data2 = data2,
stanvars = stanvars)
1: brm(y ~ x1 + (1 | mm(g1, g2, weights = cbind(w1, w2))), data = dat)
``````

Set scale = FALSE in mm()

I had realized this just after posting, so I tried to delete the post. I canceled the deletion to keep this thread coherent. Thanks again!