I'm using mixed-effect models through glmer
function of lme4
library. I want to calculate the partial response functions but the function response.plot2
of biomod2
is not working of this class of models. I tries to do it by myself this way :
library(purrr)
library(lme4)
set.seed(1213)
Y_ <- purrr:: rbernoulli(150, p = 0.4)
Y <- ifelse(Y_=='TRUE', 1, 0)
years <- as.character(rdunif(150,b=5,a=1))
r1_ <- rnorm(150, 800, sd=50)
r2_ <- rnorm(150, 1000, sd=50)
my_data <- as.data.frame(cbind(Y, r1_, r2_, years))
colnames(my_data) <- c("Y", "r1", "r2", "years")
my_data$r1 <- as.numeric(as.character(my_data$r1))
my_data$r2 <- as.numeric(as.character(my_data$r2))
GLMM_MODEL_Model_ <- glmer('Y ~ r1*r2+ (1 | years)' ,
data = my_data, family=binomial(link="logit"),
control=glmerControl(optimizer="bobyqa",
optCtrl=list(maxfun=150000)))
my_preds_glmm <- c("r1", "r2")
DATA_PRATIAL_EFFECTS <- data.frame(matrix(0, nrow = nrow(my_data), ncol=1))
for(i in 1: length(my_preds_glmm)) {
Pr <- data.frame(matrix(0, nrow = nrow(my_data), ncol=2))
colnames(Pr) <- c(paste0("EFFC_",my_preds_glmm[i]) , paste0("VAR_",my_preds_glmm[i]))
Pr [, 2] <- my_data[, my_preds_glmm[i]]
DATA_PARTIAL <- data.frame(matrix(0, nrow = nrow(my_data), ncol=length(my_preds_glmm)))
colnames(DATA_PARTIAL) <- my_preds_glmm
DATA_PARTIAL[, i] <- my_data[, my_preds_glmm[i]]
Pr[, 1]<- predict(GLMM_MODEL_Model_, DATA_PARTIAL, type='response', re.form=NA)
DATA_PRATIAL_EFFECTS <- cbind(DATA_PRATIAL_EFFECTS, Pr)
}
DATA_PRATIAL_EFFECTS <- DATA_PRATIAL_EFFECTS[, -1]
I want to have some points of view regarding my approach.