# Weights function in brms

I’m following this page to run multivariate multilevel models. Estimating Multivariate Models with brms • brms (paul-buerkner.github.io)

I am not sure if weights can be applied in multivariate models. The weights option works fine when I run univariate multilevel models. Hope you can help. Thank you very much.

My code:
brm.0 <- brm(
mvbind(stflife, happy) ~ 1 + (1|(cntry, weights = “anweight”)),
data = X, chains = 2, cores = 2, control = list(max_treedepth = 15))

Error: unexpected ‘,’ in:
“brm.0 <- brm(
mvbind(stflife, happy) ~ 1 + (1|(cntry,”
data = X, chains = 2, cores = 2, control = list(max_treedepth = 15))
Error: unexpected “,” in " data = X,"

• Operating System: Windows 10
• brms Version: 2.13.5
1 Like

I am not sure I understsand completely what you are trying to achieve - could you also share the univariate code you are trying to use?

My understanding is that weights are given on the left-hand side of the formula, i.e. `y | weights ~ ... `. Also note that you can always specify the model as a set of univariate formulas, because `mvbind(A,B) ~ X` is just a shorthand for `bf(A ~ X) + bf(B ~ X)`.

Best of luck with your model!

P.S. Note that you can use triple backticks (```) to format code blocks in posts here on the forums :-)

Thanks for this. Yes, you’re right. The weights option needs to be on the left. This code works now.

``````brm.0 <- brm(
mvbind(stflife, happy)|weights(anweight) ~ 1 + (1|(cntry)),
data = X, chains = 2, cores = 2, control = list(max_treedepth = 15))
``````

You asked about the univariate code. I was following the example in the brms vignette. The weights option was on the right of the formula and I don’t know why. Thanks again!

``````# Multigroup membership

# simulate some data
dat <- data.frame(
y = rnorm(100), x1 = rnorm(100), x2 = rnorm(100),
g1 = sample(1:10, 100, TRUE), g2 = sample(1:10, 100, TRUE)
)

# multi-membership model with two members per group and equal weights

fit4 <- brm(y ~ x1 + (1|mm(g1, g2)), data = dat)
summary(fit4)

# weight the first member two times for than the second member
dat\$w1 <- rep(2, 100)
dat\$w2 <- rep(1, 100)

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

Oh I see where the confusion comes from: the weights in a multi-membership model (i.e. a model where a student can have multiple teachers where you want to treat the teacher effects as a varying intercept - the weights would let you express for example the number of classes with each teacher the student had) are a completely separate concept from weights in the likelihood - which basically let you put more importance on some data points. The weights in the multi-membership models are on the right-hand side as an argument to the `mm` function while weights for the likelihod are on the left-hand side as I suggested.

So not sure if you are trying to build a multi-membership model or using weights for your likelihood.

Hope that clarifies more than confuses.

1 Like

Thanks for this. I was trying to use weights in the likelihood. It’s the weight for a survey. Some groups need to have more weights than others because of different sample sizes in each group. It works fine now. Thanks again.

1 Like

The weights in brms (specified by the outcome) are best described as frequency weights, in contrast to precision weights and sampling weights. Recommended reading: Weights in statistics - Biased and Inefficient (rbind.io)