Solved – How to get the residuals for a glm with a binary response variable using R

binary datageneralized linear modelresiduals

I am running a glm with a binary response variable (0 or 1) in R, one of my significant predictors is continuous and the other is categorical. How would I get the residuals for this model, especially with the response variable being binary?

Best Answer

You can use the DHARMa package, which implements the idea of randomized quantile residuals by Dunn and Smyth (1996).

Essentially, the idea is to simulate new data from the fitted model, and compare to the observed data. Details see https://cran.r-project.org/web/packages/DHARMa/vignettes/DHARMa.html

Here an example with a missing quadratic effect in the glm, which shows up in the right plot.

library(DHARMa)

dat = createData(replicates = 1, sampleSize = 300, intercept = -3,
           fixedEffects = 1, quadraticFixedEffects = 20, 
           randomEffectVariance = 0, family = binomial())

fit = glm(observedResponse ~ Environment1 , data = dat, family = binomial)
res = simulateResiduals(fit)
plot(res)

enter image description here