florianhartig / DHARMa

Diagnostics for HierArchical Regession Models
http://florianhartig.github.io/DHARMa/
200 stars 21 forks source link

recalculateResiduals() throws error when many other packages are loaded #393

Closed RaduUrian closed 7 months ago

RaduUrian commented 7 months ago

Hi Prof. Hartig,

I was assessing the performance of a glmer binomial model (1/0 Bernoulli) by following the DHARMa vignettes for binomial models you provided. However, I ran into an error when trying to group the simulated residuals by the random effect variable in my model using recalculateResiduals(). The error code is:

Error in get(as.character(FUN), mode = "function", envir = envir) : object 'aggregateBy' of mode 'function' was not found

The exact code from the recalculateResiduals() vignettes also produces the same error:

library(lme4)

testData = createData(sampleSize = 100, overdispersion = 0.5, family = poisson())
fittedModel <- glmer(observedResponse ~ Environment1 + (1|group), 
                     family = "poisson", data = testData)

simulationOutput <- simulateResiduals(fittedModel = fittedModel, plot = F) 
simulationOutput2 = recalculateResiduals(simulationOutput, group = testData$group)
plot(simulationOutput2, quantreg = FALSE)

Similar error with the slightly modified code you suggested in issue #383:

library(DHARMa)
library(lme4)
testData = createData(sampleSize = 5000,numGroups = 1000, randomEffectVariance = 2)
fittedModel <- glmer(observedResponse ~ Environment1 + (1|group), family = "poisson", data = testData)

simulationOutput <- simulateResiduals(fittedModel = fittedModel, plot = F)

simulationOutput = recalculateResiduals(simulationOutput , group = testData$group)
plot(simulationOutput)

Is this a known error or something that you can reproduce using the vignette code? Or is it a client-side issue on my part?

DHARMa 0.4.6 Session info:

─ Session info ─
 setting  value
 version  R version 4.3.2 (2023-10-31 ucrt)
 os       Windows 10 x64 (build 19045)
 system   x86_64, mingw32
 ui       RStudio
 language (EN)
 collate  English_Canada.utf8
 ctype    English_Canada.utf8
 tz       America/Toronto
 date     2023-11-16
 rstudio  2023.06.1+524 Mountain Hydrangea (desktop)
 pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)

*I'm unfamiliar with posting issues on GitHub, so please let me know if this is not up to the acceptable standard. **Update first code chunk based on comments to accurately reflect the vignette codes.

florianhartig commented 7 months ago

Hello,

not all all, your issues is very detailed and well formulated.

The problem is that I can't reproduce the error. The first code seems to be missing a line of code where simulationOutput is created but the second chunk runs for me without problems. I also don't see any errors in the automatic checks of the package. The error complains about the aggregateBy = sum option set in the function, but I don't understand why. A potential option is that you created a variable or function called sum somewhere in your R session that overwrites the R default sum function.

Can you complete restart R and Studio, start with a clean and empty session and confirm that the following code:

library(DHARMa)
library(lme4)
testData = createData(sampleSize = 5000,numGroups = 1000, randomEffectVariance = 2)
fittedModel <- glmer(observedResponse ~ Environment1 + (1|group), family = "poisson", data = testData)

simulationOutput <- simulateResiduals(fittedModel = fittedModel, plot = F)

simulationOutput = recalculateResiduals(simulationOutput , group = testData$group)
plot(simulationOutput)

indeed produces an error?

RaduUrian commented 7 months ago

Thank you for your prompt answer. You're correct, as the chunk you provided works without an error in a new session with a cleared environment. I can still reproduce the error on my old session even with a clean environment, so there must be something in my session info or in interactions with the (many) loaded packages that causes the error. Thank you for your help, this has really clarified my issue and hopefully I can find a quick solution. Thank you for the amazing package btw!

florianhartig commented 7 months ago

If a loaded package overwrites sum, it might help to provide aggregateBy = base::sum to recalculateResiduals.

florianhartig commented 7 months ago

OK, I assume that this is solved, if not please reopen.