rlogistic-regressionweighted

How to prevent error in DHARMa package when including weights?


I am running a logistic regression in R and am including inverse weights to my data to ameliorate issues arising from class imbalance (the minority class has about 67x fewer observations):

# calculate class frequency
class_freq <- table(df[[outcome_name]])
# calculate weights
weights <- max(class_freq) / class_freq
# add weights to data frame (floor to avoid warning when running 'glm()'
df$weights <- floor(weights[df[[outcome_name]]])

I then run the model and attempt to check the fit using the DHARMa package:

model <- glm(formula = Y ~ X + cov1 + cov2 + cov3, 
             family = "binomial", data = df, weights = weights)
library(DHARMa)
# calculate scaled residuals
simulationOutput <- DHARMa::simulateResiduals(fittedModel = model)

However, I am getting this error:

Error in out * x$`(weights)` : non-conformable arrays

Any advice would be appreciated.


Solution

  • I think the issue was that I was not using weights appropriately (see this post: https://stats.stackexchange.com/questions/386675/what-are-weights-in-a-binary-glm-and-how-to-calculate-them); I was applying them as if my outcome was a binomial as opposed to Bernoulli variable where the variance (which is what weights specify) must be 1.