Please forgive my ignorance of the proper hierarchical regression nomenclature and conventions. I am sure that what I want is quite easily achieved, but I don’t know the right terms under which to search. I want to fit a model of the following form:
where i indexes individual units, r is a binary factor, and t is a 4-level categorical factor. My data include observations of (y_i, n_i) for all 2x4 = 8 possible combinations of the levels of r and t.
I have attempted to specify this model in brms using the formula:
y | trials(n) ~ r + (1 | gr(t, by = r)) + (1 | i)
but this results in an error message to the effect that “some levels of ‘t’ correspond to multiple levels of ‘r’.” Can someone explain to me the right way of specifying this model in the brms/lmr4 syntax?
Here is some example data, if this helps at all:
data <- data.frame(
y = as.integer(c(37, 2, 18, 21, 49, 12, 9, 17, 45, 21, 29, 26, 24, 20, 38, 5)),
n = as.integer(c(185, 173, 165, 194, 150, 194, 130, 141, 198, 188, 120, 111, 132, 160, 110, 165)),
r = factor(c(0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1)),
t = factor(c(0, 0, 1, 1, 2, 2, 3, 3, 0, 0, 1, 1, 2, 2, 3, 3)),
i = factor(0:15)
)