bbolker / broom.mixed

tidy methods for mixed models in R
227 stars 23 forks source link

tidy.brmsfit fails on distributional models #138

Open chrishanretty opened 1 year ago

chrishanretty commented 1 year ago

brms allows users to model the residual standard deviation. tidy.brmsfit gives an error with such models. A MWE follows:

library(brms)
library(broom.mixed)
library(lme4)

### Let's repeat a model from lme4
fm1 <- brm(angle ~ recipe * temperature + (1|recipe) + (1|recipe:replicate),
           data = cake,
           cores = 4,
           chains = 4)

### WORKS
tfm1 <- tidy(fm1)

### Complicate by adding heteroskedasticity
fm2 <- brm(bf(angle ~ recipe * temperature + (1|recipe) + (1|recipe:replicate),
              sigma ~ recipe),
           data = cake,
           cores = 4,
           chains = 4)

### DOESN'T WORK
tfm2 <- tidy(fm2)

The problem arises because the regular expression which identifies random effects uses the term "sigma". Here, "sigma" is present, but not because it identifies a random effect, but because it's the outcome in an auxiliary regression.

I think the issue can be resolved by dropping in the following code (or something equivalent) in just after line 227 in the code:

res_list$ran_pars <- res_list$ran_pars |>
 subset(group != "Residual")