Brms - custom families and vectorisation?

Hi!

I am right now doing my first baby steps with defining my own custom family in brms. When doing so I noticed that the code generated is not vectorized. Just take the default beta-binomial example from the vignette:

https://cran.r-project.org/web/packages/brms/vignettes/brms_customfamilies.html

The Stan code will then loop over each observation.

I tried the “loop=FALSE” option from the dev version, but that still loops over each observation, because trials is being passed to the custom density. So I get this code then… which is presumably a bug (@paul.buerkner ?):

    for (n in 1:N) {
      target += beta_binomial2_lpmf(Y[n] | mu, phi, vint1[n]);
    }

That does not look quite right to me. I would expect to get this:

target += beta_binomial2_lpmf(Y | mu, phi, vint1);

… so no loop at all, please.

Here is the R code to generate the above (second case):

brms-beta-binom-example.R (1.4 KB)

1 Like

Yeah, I agree. Let me check.

1 Like

You had a type in your second custom family. You wrote vars = "vint1[n]" but it should have been vars = "vint1". That said, this mistake can be captured and I will add a check for that.

1 Like

I have now added the error message: Invalid use of index '[n]' in an unlooped custom likelihood.

1 Like

Oh…user error… sorry for the noise. An error message would great indeed.

BTW… I took me quite some time to figure out how to pass into user likelihoods multiple int vectors. It’s vars=c("vint1", "vint2") and then I need to use vint(col1, col2)… but I don’t think it’s anywhere documented that you can pass to vint multiple columns. I always tried vint(col1) + vint(col2), but failed. It totally makes sense how you did it… but I overlooked the doc for this. A note for this would be great.

1 Like

Just for the record I am working on a PR on the docs for exactly this :)

2 Likes

It’s indeed missing in the doc. @jsocolar stumbled upon the same issue just a few days ago.

ahh… and the model I implemented is the one in OncoBayes2:

I did code it up for the 2 drug case… if that’s of interest to evaluate further as a future family in brms, then let me know and I post an issue for this. EDIT: I did not include the “EXNEX” stuff which requires heavy mixture model coding.

1 Like

I don’t know if it should be implemented in brms itself but the code for the custom family is certainly very helpful to other users in any case.

Is there a place where this type of thing is collected?

I had added a place (GitHub - paul-buerkner/custom-brms-families: R scripts for custom brms families) but nobody cared so far with adding anything :-D

1 Like

looks pretty empty so far… let’s change that

3 Likes

Maybe add it to the vignette on custom families to encourage people to contribute there? :) I will make a PR for my code of the natural scale parameterization of the lognormal

Good idea!