Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unused factor levels are always dropped #1346

Closed
wds15 opened this issue May 4, 2022 · 3 comments
Closed

unused factor levels are always dropped #1346

wds15 opened this issue May 4, 2022 · 3 comments

Comments

@wds15
Copy link
Contributor

wds15 commented May 4, 2022

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.

@paul-buerkner
Copy link
Owner

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

@paul-buerkner
Copy link
Owner

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

@paul-buerkner
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants