Closed isabellaghement closed 2 years ago
Thanks Isabella --- this looks like an issue in brms::posterior_epred()
. If you change your call to brms::posterior_epred()
the same error occurs:
epred_typical_class_typical_school <- brms::posterior_epred(
model,
newdata = data.frame(housepov = c(0, 1),
schoolid = NA,
classid = NA),
re_formula = ~ (1|schoolid/classid))
## Error: Levels 'NA_NA' of grouping factor 'schoolid:classid' cannot be found in the fitted model.
## Consider setting argument 'allow_new_levels' to TRUE.
If you file an issue on the brms repository they should be better able to help. Currently add_epred_draws()
doesn't modify anything related to re_formula
or random effects; it just passes that stuff down to posterior_epred()
and relies on its behavior.
Thanks very much for your prompt response, Matthew! It's good to know what is going on - I will file this as an issue on the brms repository as you advised. If you leave this issue open for now, I will report back here to confirm if Paul was able to fix this inconsistency.
Isabella
P.S. The brms issue is now filed here:
Looks like this is fixed now, closing.
Hi Matthew,
There seems to be an inconsistency in how tidybayes::add_epred_draws() works for a mixed effects model with nested random grouping factors. I found some data on the Internet which I will use to illustrate the problem: classes nested in schools. In this context, I am primarily interested in inference on the effect of a school-level variable.
As per https://lme4.r-forge.r-project.org/slides/2009-07-01-Lausanne/3SimpleD.pdf:
"The classroom data are a cross-section of students within classes within schools. The mathgain variable is the difference in mathematics achievement scores in grade 1 and kindergarten."
The inconsistency that I get comes from the re_formula being specified as re_formula = ~ (1|schoolid/classid) when both schoolid and classid are specified as NA in newdata. This throws R off and yields the following error message:
_Error: Levels 'NA_NA' of grouping factor 'schoolid:classid' cannot be found in the fitted model. Consider setting argument 'allow_newlevels' to TRUE.
It seems to me that, when schoolid and classid are both set to NA in newdata:
R should NOT complain when encountering re_formula = ~ (1|schoolid/classid) if it does NOT complain when encountering re_formula = ~ (1|schoolid)
OR
R should complain when encountering re_formula = ~ (1|schoolid/classid) but should also complain when encountering re_formula = ~ (1|schoolid)
Can you please look into this inconsistency for me and see if it's something that can be (hopefully easily) fixed to avoid confusing the users of tidybayes?
I haven't checked if this consistency is also an issue for brms::posterior_epred().
Thanks very much!
Isabella