Skip to content

brms compatibility#523

Merged
florianhartig merged 25 commits into
masterfrom
v0.5.0_brmsCompatibility
May 12, 2026
Merged

brms compatibility#523
florianhartig merged 25 commits into
masterfrom
v0.5.0_brmsCompatibility

Conversation

@cosminawerneke

Copy link
Copy Markdown
Collaborator

Compatibility for simple brms models (i.e. models which could also be fit with glmmTMB; no multinomial, multi-response or structural equation models)

cosminawerneke and others added 10 commits March 9, 2026 17:16
…il in spatialAutocorrelation plot so that margins are returned to previous settings
- adding getPearsonResiduals.brmsfit and improving testDispersion to detect absence of Pearson residuals in brms models and return NA for tests with refit=T AND type="PearsonChisq"
- adding info in help file of getData to alert users that it will search data frames in the environment.
this functions doesn't interfere with others, but to keep it standardized I created the compatibility for brms because brms::fixef() returns a matrix (mean, se, q2.5, q97.5) and the standard for other packages is to return a numeric vector. When refit=T, the simulateResiduals was creating a weird matrix in simulationOutput$refittedFixedEffects. Now it is ok.
using the function getPearsonResiduals for the refitted Pearsons to 1) use the function we created for that to avoid errors/warnings from residuals from model's package. This way we guarantee NAs in brms models, for example.
Reason: if I have  getPearsonResiduals.brmsfit and don't export it - R CMD CHECK will complain with a note on S3 methods. If I export it, it will appear in the help of getPearsonResiduals, but it doesn't seem elegant/nice to have a function there that just throws an error. So, I decided to implement the error for brmsfit models in getPearsonResiduals.default.
…r, testData is removed and created anew for phylolm); adjusted vignette for bayesians for brms compatibility
"brms and Stan models are unique because they compile C++ code during the test phase. Even if BH is a dependency of your dependency, the compiler sometimes fails to link it unless it's explicitly present in the environment's library."
adding rstan to suggests instead of Enhances
adding BH and StanHeaders to suggestions to see if the brms testthat works
adding RcppEigen in extra-packages
deleting the packages I added to `suggests`, to test if it will still work in GH actions
@melina-leite melina-leite force-pushed the v0.5.0_brmsCompatibility branch from acaa3a5 to 4745ac2 Compare March 18, 2026 16:37
@melina-leite

Copy link
Copy Markdown
Collaborator

About the GH Action fails and the solution

With brms, in order to make the testthat run, we had to add the bold code in R-CMD-check.yaml:

uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: |
any::rcmdcheck
any::BH
any::StanHeaders
any::RcppEigen

However, there was a suggestion (from AI) to also add the same packages to the 'Suggests' section of the DESCRIPTION FILE. In the end, I kept the latest commit without this suggestion. I'm not sure the consequences of this to CRAN submission.

@melina-leite melina-leite force-pushed the v0.5.0_brmsCompatibility branch from 802e7c5 to b8f35e1 Compare March 18, 2026 17:21
Comment thread DHARMa/R/tests.R Outdated
message("Note that the Chi2 test on Pearson residuals is biased for MIXED models towards underdispersion. Tests with alternative = two.sided or less are therefore not reliable. If you have random effects in your model, we recommend to test only with alternative = 'greater', i.e. test for overdispersion, or else use the DHARMa default tests which are unbiased. See help for details.")}


#rp <- getPearsonResiduals(model)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @cosminawerneke - I wonder if we really need this error message here or if it would not be sufficient to throw the error in getPearsonResiduals, and then people can see the traceback?

In the way you coded it it is of course slightly more informative / cleaner, but also higher maintenance.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @florianhartig,
I added this here (for type="Pearson") as it was also in the (type = "DHARMa" & refit=T). Cause the error wasn't clear when having no Pearson residuals in this case.

I'm not sure about moving it to getPearsonResiduals, because, as I see it, we would have to repeat it in every getPearsonResiduals.xx function (there aren't that many, but still repeating code).
Let me know if you have a more elegant way of doing it! :)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm ... but it still feels a bit weird - getPearsonResiduals is our own function, so I feel we should be able to call it and rely on it providing sensible output without having to catch errors.

Or, in other words, getPearsonResiduals should be implemented for all sensible model classes, and specific errors should be thrown. For the default function, we could catch a general error.

I would still change this. If this code is used somewhere else in the package, we should change it as well, it may been a remnant from the time where I didn't have a getPearsonResiduals function

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@florianhartig, changes made:

  1. I moved the error message to getPearsonResiduals.default and getPearsonResiduals.gam

  2. I had to suppress the error message in simulateResiduals() with refit=T to avoid repeating the error message nSim times. Then, I throw just ONE warning message at the end of simulateResiduals(refit=T).

Here is a working example with mgcv:

devtools::load_all()
data <- createData()

# DOESN'T HAVE PEARSON - shouldn't work
library(mgcv)
mod2 <- gam(observedResponse ~ Environment1, 
               data=data, family = ziP())
summary(mod2)

mgcv:::residuals.gam(mod2, type="scaled.pearson") # shouldn't work
# error message of mgcv is weird, it searches for res, but because the family doesn't have a formula for Pearson, it simply says res wasn't found. (could be more informative, but it is not)

getPearsonResiduals(mod2) # ok, not working: DHARMa message + mgcv error message


testDispersion(mod2, type="Pearson") # ok the same

resis <- simulateResiduals(mod2)

getPearsonResiduals(resis$fittedModel) # ok the same

testDispersion(resis, type="DHARMa") # dharma test works ok

resis2 <- simulateResiduals(mod2, refit=T) # only ONE error message in the end
testDispersion(resis2, type="DHARMa") # ok the same

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @florianhartig, I implemented the changes we discussed today (commit 70be55f. Please have a look at it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, I had to change a bit the testthat for some packages that don't have the Pearson (brms, GLMMadaptive, phylolm) to avoid the error message to stop the R CMD check.

Comment thread DHARMa/R/tests.R
@florianhartig

Copy link
Copy Markdown
Owner

Hi @cosminawerneke, looks fine for me except for the few questions I have made directly in the code.

One general question: I didn't see anything that tests if the brms model that is used is suitable for DHARMa - what will happen if a person uses e.g. a multinomial model etc. ?

@cosminawerneke

Copy link
Copy Markdown
Collaborator Author

Hi @florianhartig, as we agreed on in our last meeting about brms compatibility, DHARMa does not test if a brms model can reliably be checked or not. We just give a warning every time a brms model is detected (see compatibility.R line 45), and then it's up to the user to decide if their model is suitable to be used with DHARMa

@florianhartig

Copy link
Copy Markdown
Owner

@melina-leite, I would still clean up this error message, otherwise ready to be merged.

florianhartig and others added 2 commits May 5, 2026 08:21
- moving Pearson residuals error messagem from testDisperstion to getPearsonResiduals()
- suppressing error message of not getting pearson in simulateResiduals when refit=T
- some gitignore from new version rstudio "positai"

@melina-leite melina-leite left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all good

- adding the getPearsonResiduals.brms(), although it throws an error automatically because Pearson residuals are deprecated for brms. I added a note about it in the help file of the wrapper.
- deleted the message about not having Pearson residuals when doing refit=T in simulateResiduals().
- Cleaning unnecessary code in testDispersion.
given that the getPearsonResiduals.brms function will always give an error, I created a runEverything function excluding the Pearson residuals dispersion tests in testModelTypes. Used this for brms, phylolm/phyloglm, GLMMadaptive. Otherwise the tests wouldn't work
@florianhartig florianhartig merged commit 7325e28 into master May 12, 2026
6 checks passed
@melina-leite melina-leite deleted the v0.5.0_brmsCompatibility branch May 18, 2026 08:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants