Different hyperparameters for grouping factor

#1

Dear Paul,

Is it true that I cannot provide a categorical variable to the by argument of the gr() function IF the levels of the factor are not numbers?

Kindest regards,
Pascal

  • Operating System: CentOS 7
  • brms Version: 2.8.0
#2

I would expect this to work. didn’t it work for you?

#3

It does not work until I recode the factor to numeric values… I will provide the error message later in the day…

#4

ok. a minimal reproducible example would be nice indeed. thanks for taking the time to prepare one!

#5

This example works for me in brms 2.8.0+

dat <- data.frame(
    y = rnorm(100), x = rnorm(100),
    g = rep(1:10, each = 10),
    z = factor(rep(c("a", "b", "c", "d", "e"), each = 20))
)
fit <- brm(y ~ x + (1 | gr(g, by = z)), dat)
#6

Your example works for me, the following does not work:

dat <- data.frame(
    y = rnorm(100), x = rnorm(100),
    g = rep(1:10, each = 10),
    z = factor(rep(c("Control", "Group 1", "Group 2", "Group 3", "Group 4"), each = 20))
)
fit <- brm(y ~ x + (1 | gr(g, by = z)), dat)

The error yielded is the following:

Compiling the C++ model
Start sampling
Error in FUN(X[[i]], ...) : Stan does not support NA (in Jby_1) in data
failed to preprocess the data; sampling not done

Sorry for letting you do all the work and thank you very much!

#7

I think the problem is that your category names have spaces, which
I would recommend to avoid in general. I will see if I can fix this,
but in the meantime, just remove the spaces.

#8

You are absolutely right! I totally neglected the whitespace in the categories!
After replacing the spaces with underscores, everything is working like a charm!
Many thanks!

#9

great! I just fixed it so that whitespaces should now also be handled correctly in the github version of brms.

1 Like