brms.mmrm icon indicating copy to clipboard operation
brms.mmrm copied to clipboard

adding y | weights(weight) ~ ... to brm_formula

Open yonisidi opened this issue 5 months ago • 5 comments

Currently I don't think it is possible to add y | weights(weight) ~ ... in the brm_formula as it is constructed right now. I tried adding it through ... but it seemed to be ignored. Would an explicit formal need to be defined?

Here is a discussion about weights for observations in normal brms formula syntax.

https://discourse.mc-stan.org/t/weights-in-brm/4278/14

yonisidi avatar Jul 29 '25 16:07 yonisidi

Yes, both brm_formula() and brm_data() would need new arguments and infrastructure for weights.

Is this a common enough use case to need direct support in brms.mmrm? If the limitation affects only a small number of cases, there is a quick workaround:

library(brms.mmrm)
data <- brm_data(
  data = brm_simulate_simple()$data,
  outcome = "response",
  group = "group",
  time = "time",
  patient = "patient",
  reference_group = "group_1",
  reference_time = "time_1"
)
formula <- brm_formula(data)
text <- paste(formula[[1]][[2]], "| brms::weights(weight)")
formula[[1]][[2]] <- parse(text = text)[[1]]
formula
#> response | brms::weights(weight) ~ group + group:time + time + unstr(time = time, gr = patient) 
#> sigma ~ 0 + time

Created on 2025-07-30 with reprex v2.1.1

wlandau avatar Jul 30 '25 20:07 wlandau

I also wonder if it would be feasible to combine this with the existing ‘y | mi() ~’ syntax for multiple imputation during model fitting.

wlandau avatar Jul 30 '25 20:07 wlandau

It is pretty common for us to use obs weights in the models.

could there be a cleaner solution for all the non-standard formula pieces that are embedded into brms, so we don't have to do one offs? it seems like there could be all kinds of things we wouldn't think people would need, but would get reqs for.

yonisidi avatar Jul 31 '25 12:07 yonisidi

Yes, both brm_formula() and brm_data() would need new arguments and infrastructure for weights.

Is this a common enough use case to need direct support in brms.mmrm? If the limitation affects only a small number of cases, there is a quick workaround:

library(brms.mmrm) data <- brm_data( data = brm_simulate_simple()$data, outcome = "response", group = "group", time = "time", patient = "patient", reference_group = "group_1", reference_time = "time_1" ) formula <- brm_formula(data) text <- paste(formula[[1]][[2]], "| brms::weights(weight)") formula[[1]][[2]] <- parse(text = text)[[1]] formula #> response | brms::weights(weight) ~ group + group:time + time + unstr(time = time, gr = patient) #> sigma ~ 0 + time Created on 2025-07-30 with reprex v2.1.1

Thanks for the solution. I have tried implementing it. However, even if the "weight" variable is present in the dataset, brm_model function doesn't seem to recognize this weight. Of course, I can pass this through "stanvar" writing a new piece of stan code, but wanted to check if there is any other more efficient way. Thanks in advance for your help,

Image

U1004542 avatar Jul 31 '25 13:07 U1004542

could there be a cleaner solution for all the non-standard formula pieces that are embedded into brms, so we don't have to do one offs? it seems like there could be all kinds of things we wouldn't think people would need, but would get reqs for.

I'm not sure. Injecting arbitrary additional response information breaks the encapsulated brms_formula() interface. And to your point, I would like to avoid adding more one-offs because there are a lot of response modifiers. I started implementing #136 (now closed) before I realized there were so many.

I have tried implementing it. However, even if the "weight" variable is present in the dataset, brm_model function doesn't seem to recognize this weight.

I just learned that the brms function is actually brms::resp_weights(), which brms understands if you just write weights() without namespacing the call.

wlandau avatar Jul 31 '25 19:07 wlandau