`re_formula = NA` or `NULL`: some inconsistency between modelbased and brms
Mantenedores costumam responder em até 1 dia
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 35/100
Direção de pesquisa
Comece executando os exemplos fornecidos de sleepstudy e Oxboys, comparando modelbased::estimate_means com brms::conditional_effects para re_formula=NULL e NA. Rastreie como cada ponto de entrada calcula as estimativas e os intervalos de credibilidade e, em seguida, determine se a discrepância é esperada ou se é um bug. A tarefa estará concluída quando o comportamento estiver reconciliado e a semântica esperada estiver claramente documentada ou coberta por um teste de regressão.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
Hello everyone!
I think I might have found an inconsistency between modelbased's (estimate_means) and brms's conditional_effects when it comes to computing the EMMs with re_formula. In particular, this regards the difference between conditional and marginal effects.
Brief intro:
I found this post by Andrew Heiss very clear when it comes to describing the marginal (re_formula = NULL, default in modelbased) vs conditional (re_formula = NA) effects.
Here's a general definition:
- Conditional effect =
re_formula = NA> random offsets set to 0 - Marginal effect =
re_formula = NULL: random offsets are incorporated into the estimate
This definition seems to be shared elsewhere. In several blogs, they say that re_formula = NULL should increase the uncertainty in the predictions due to the inclusion of the random effects, whereas with re_formula = NA, the predictions are based only on fixed effects and the estimated error of those parameters. Here, Paul Buerkner is saying this. In the brms manual, it says the same thing:
re_formula: A formula containing group-level effects to be considered in the conditional predictions. IfNULL, include all group-level effects; ifNA(default), include no group-level effects
However, in modelbased's EMMs, the Credible Intervals with NA are way larger than those with NULL. But shouldn't it be the other way around? If NULL includes all the uncertainty coming from the random part of the model, shouldn't the CIs be larger? I confess I'm a bit confused.
Another user reported a similar doubt, but the replies are still a bit confusing to me. I noticed everyone suggests using NA as default (brms too), but in modelbased NULL seems to be the default, so I wondered why.
I'm attaching here three figures derived by three models (reproducible code below):
- scale(Reaction) ~ Days + (1+Days|Subject), data=sleepstudy
- scale(height) ~ Occasion + (1+Occasion|Subject), data = Oxboys
What I noticed is that the estimates seem more similar with NA (still not identical, though), but they vary enormously with NULL. Compared to NA, modelbased's NULL decreases the variability, whereas brms' conditional_effects increases it. Brms's results seem more logical to me though; cause, again, it makes sense that if you add the variability coming from the RE, the uncertainty should be larger. However, to complicate things even more, the NULL estimates with brms give the impression that nothing is "significant", whereas in fact everything is!
- Finally, this one comes from a more complex model with one random intercept and three random slopes (1 + x + y + z | participant). Here the discrepancies seem even larger.
Here's a reproducible code for the first two figures. I hope it's helpful for you to get to the bottom of this XD
Also, I apologise if I made some mistakes in describing what I found. Maybe it's just me not getting it.
### Marginal / Conditional Effects in B-GLMMs (brms & modelbased) ###
library(pacman)
pacman::p_load(brms, modelbased, lme4)
#### Sleepstudy dataset (slope) ####
data("sleepstudy", package = "lme4")
m <- brm(scale(Reaction) ~ Days + (1+Days|Subject),
family = gaussian(),
data = sleepstudy,
prior = c(
set_prior("normal(0,1)", class = "Intercept"),
set_prior("normal(0,1)", class = "b"),
set_prior("exponential(1)", class = "sd"),
set_prior("exponential(1)", class = "sigma")
),
cores= 4,
control = list(adapt_delta = 0.95),
save_pars = save_pars(all = TRUE),
backend = "cmdstanr",
seed = 1234)
## Marginal effect ##
emm_marginal <- estimate_means(m, by="Days", centrality="mean", ci=.95, ci_method="hdi", re_formula=NULL)
## Conditional effect ##
emm_conditional <- estimate_means(m, by="Days", centrality="mean", ci=.95, ci_method="hdi", re_formula=NA)
ce1 <- conditional_effects(m, effects = "Days", re_formula = NULL)
ce2 <- conditional_effects(m, effects = "Days", re_formula = NA)
p1 <- plot(ce1, plot = FALSE)[[1]]
p2 <- plot(ce2, plot = FALSE)[[1]]
# Merge all graphs
plot(emm_marginal)+labs(title="NULL | modelbased", y="Reaction")+
plot(emm_conditional)+labs(title="NA | modelbased", y="Reaction")+
p1 + labs(title="NULL | brms conditional_effects", y="Reaction")+
p2 + labs(title="NA | brms conditional_effects", y="Reaction")&
coord_cartesian(ylim=c(-2.5,3))&
theme_bw()
#### Oxboys dataset (mean difference) ####
data("Oxboys", package = "nlme")
Oxboys <- subset(Oxboys, Occasion <= 4)
Oxboys$Occasion <- factor(Oxboys$Occasion, ordered = FALSE)
m3 <- brm(scale(height) ~ Occasion + (1+Occasion|Subject),
family = gaussian(),
data = Oxboys,
prior = NULL,
cores= 4,
control = list(adapt_delta = 0.95),
save_pars = save_pars(all = TRUE),
backend = "cmdstanr",
seed = 1234)
## Marginal effect ##
emm_marginal3 <- estimate_means(m3, by="Occasion", centrality="mean", ci=.95, ci_method="hdi", re_formula=NULL)
## Conditional effect ##
emm_conditional3 <- estimate_means(m3, by="Occasion", centrality="mean", ci=.95, ci_method="hdi", re_formula=NA)
ce13 <- conditional_effects(m3, effects = "Occasion", re_formula = NULL)
ce23 <- conditional_effects(m3, effects = "Occasion", re_formula = NA)
p13 <- plot(ce13, plot = FALSE)[[1]]
p23 <- plot(ce23, plot = FALSE)[[1]]
# Merge all graphs
plot(emm_marginal3)+labs(title="NULL | modelbased", y="Height (z-score)")+
plot(emm_conditional3)+labs(title="NA | modelbased", y="Height (z-score)")+
p13 + labs(title="NULL | brms conditional_effects", y="Height (z-score)")+
p23 + labs(title="NA | brms conditional_effects", y="Height (z-score)")&
coord_cartesian(ylim=c(-3,3.5))&
theme_bw()
- Linguagem predominante
- R
- Estrelas
- 264
- Forks
- 24
- Merge médio
- 17h 36min
- PRs com merge (30d)
- 21
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Sem modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de easystats/modelbased
-
Enhancement :boom:
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 25/100
easystats/modelbased#591 · 3 comentários ·
Mantenedores costumam responder em até 1 dia
-
Enhancement :boom: Feature idea :fire:
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 30/100
easystats/modelbased#510 · 19 comentários · 1 reação ·
Mantenedores costumam responder em até 1 dia
-
What's your opinion :hear_no_evil:
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 35/100
easystats/modelbased#467 · 2 comentários ·
Mantenedores costumam responder em até 1 dia
-
What's your opinion :hear_no_evil:
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 20/100
easystats/modelbased#422 ·
Mantenedores costumam responder em até 1 dia
-
What's your opinion :hear_no_evil:
Dificuldade 5/5 Mais de uma semana Facilidade para iniciantes 20/100
easystats/modelbased#421 · 6 comentários ·
Mantenedores costumam responder em até 1 dia
Todas as issues de easystats/modelbased
Issues semelhantes
-
bug
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 84/100
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
bcgov/aqwarnings_shiny#128 ·
-
bug upkeep
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 76/100
Mantenedores costumam responder em até 1 dia
-
phoible.feature() failsAberta
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 66/100
ropensci/lingtypology#94 ·
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
datacarpentry/semester-biology#1316 ·