I'm running a logistic regression with one of the predictors being a random effect.
I want to do a leave-one-out cross validation on the model, but I want to subset the predictor connected to the observation that I'm currently leaving out and predicting.
This is what my code looks like now:
predictions <- numeric(length(data_df$Detected))
random_effect_levels <- unique(data_df$Site)
# Perform LOOCV
for (i in 1:nrow(data_df)) {
data_subset <- data_df[-i, ]
model_subset <- glmer(Detected ~ Height_22 + (1 | Site), data = data_subset, family = binomial(link = "logit"))
prediction <- predict(model_subset, newdata = data_df[i, ], type = "response")
predictions[i] <- prediction
data_df$predictions <- predictions
}
I tried my luck with ChatGPT, but I did not get the result that I wanted. The code included the line:
data_subset <- data_df[data_df$Site != site, ]
When I include the code above, I get the error:
Error in levelfun(r, n, allow.new.levels = allow.new.levels) :
new levels detected in newdata: B19251
Hope someone have a better understanding of this problem than me, or has another way of solving the issue.