florianhartig / DHARMa

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

How are scaled residuals calculated when grouping is used? #394

Closed akhileshtayade closed 10 months ago

akhileshtayade commented 10 months ago

Hello!

I have been unable to understand how DHARMa generates scaled residuals when recalculateResiduals is used with argument group.

Here is a minimal, reproducible example (output not shown) to describe what I am having trouble with:

# DHARMa version 0.4.6
library(DHARMa)

# generate data
testData = createData(sampleSize = 250)
fittedModel <- glmer(observedResponse ~ Environment1 + (1|group) , 
                     family = "poisson", data = testData)

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

# there are 10 groups in the data
unique(testData$group)

# residuals are grouped according to the groups in the data
groupedSimulationOutput <- recalculateResiduals(simulationOutput, group = testData$group)

# the first 25 rows belong to the first group
testData[testData$group == 1, ]

# confirms that recalculateResiduals just sums the predicted responses 
# from simulationOutput for each group by default
sum(simulationOutput$fittedPredictedResponse[1:25]) == groupedSimulationOutput$fittedPredictedResponse[1]

However, I did not understand how the scaledResiduals are calculated for the groupedSimulationOutput: how are groupedSimulationOutput$scaledResiduals calculated from simulationOutput$scaledResiduals? I hope I am not missing something obvious.

florianhartig commented 10 months ago

Hello akhileshtayade,

basically, the observed data (simulated and real observed data) is summarised per group according to the function that is provided (default = sum) in , and then the quantile residual is calculated as for a single data point (which is described in the vignette).

Does that help?

Best F

akhileshtayade commented 10 months ago

Yes, that helped! Thank you very much for a super quick response!

I also checked this commit and the code of the function recalculateResiduals which also helped in resolving the doubt. Thank you again!