paul-buerkner/brms

unused factor levels are always dropped

Closed this issue · 3 comments

wds15 commented

I wanted to include an unused factor level for a fixed effect in the brms model, but that does not work, since drop.unused.levels is by default TRUE and there is no way to change that. Instead I have to use a hack to get what I like to have. Here is an example:

library(brms)
AS_region <- bind_cols(RBesT::AS, region=sample(c("asia", "europe", "north_america"), 8, TRUE))

AS_region_all <- data.frame(region=c("asia", "europe", "north_america", "other")) %>%
    mutate(study=paste("new_study", region, sep="_"), r=0, n=6)

## to get brms to include the other factor level in the model, we have
## to add a fake row with region "other" and n=0
AS_region_2 <- mutate(bind_rows(AS_region, mutate(AS_region_all, n=0)[4,]),
                      region=factor(region, levels=c("asia", "europe", "north_america", "other")))

model_fixed <- bf(r | trials(n) ~ 1 + region + (1|study), family=binomial)

model_fixed_prior <- prior(normal(0, 2), class="Intercept") +
    prior(normal(0, 1), class="sd", coef="Intercept", group="study") +
    prior(normal(0, log(2)/1.96), class="b")

fixed_mc_brms  <- brm(model_fixed, AS_region_2, prior=model_fixed_prior,
                      seed=4767,
                      silent = 2, refresh = 0, control=list(adapt_delta=0.99))

It would be nice to control the drop.unused.levels from validate_data somehow.

I agree. I will add it to the next release.

This is now possible via argument drop_unused_levels in brm and related functions.

This is now possible via argument drop_unused_levels in brm and related functions.