Skip to content

Commit

Permalink
fixed a bug in GLMM RE bootstrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
jr-leary7 committed Oct 19, 2024
1 parent 30e5d92 commit dddb52c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 19 additions & 0 deletions R/bootstrapRandomEffects.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,29 @@ bootstrapRandomEffects <- function(glmm.mod = NULL,
parallel::stopCluster(cl)
}
})
# clean up potential errors with warning
error_count <- 0
ranef_boot <- purrr::map(ranef_boot, \(x) {
if (inherits(x, "simpleError")) {
error_df <- data.frame(iter = NA_real_,
subject = NA_character_,
term = NA_character_,
effect = NA_real_)
error_count <- error_count + 1
return(error_df)
} else {
return(x)
}
})
if (error_count > 0) {
warning(paste0(error_count, " bootstrap summaries failed, likely due to computationally singular effects matrices. Take caution."))
}
# summarize bootstrap resample
lower_bound <- alpha / 2
upper_bound <- 1 - alpha / 2
ranef_sumy <- purrr::reduce(ranef_boot, rbind) %>%
as.data.frame() %>%
na.omit() %>%
dplyr::with_groups(c(subject, term),
dplyr::summarise,
QL = stats::quantile(effect, probs = lower_bound),
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test_scLANE.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,22 @@ withr::with_output_sink(tempfile(), {
# run GEE Wald test
wald_test <- waldTestGEE(marge_mod_GEE_offset, mod.0 = null_mod_GEE)
# run GLMM model -- no offset
glmm_mod <- fitGLMM(X_pred = pt_test,
glmm_mod <- fitGLMM(pt_test,
Y = counts_test[, 4],
id.vec = sim_data$subject,
adaptive = TRUE,
M.glm = 3,
return.basis = TRUE,
return.GCV = TRUE)
# run GLMM model -- with offset
glmm_mod_offset <- fitGLMM(X_pred = pt_test,
glmm_mod_offset <- fitGLMM(pt_test,
Y = counts_test[, 4],
Y.offset = cell_offset,
id.vec = sim_data$subject,
adaptive = TRUE,
M.glm = 3,
return.basis = TRUE,
return.GCV = TRUE)
return.GCV = FALSE)
# bootstrap GLMM random effects
re_sumy <- bootstrapRandomEffects(glmm_mod_offset,
id.vec = sim_data$subject,
Expand Down Expand Up @@ -404,7 +404,7 @@ test_that("fitGLMM() output", {
expect_equal(glmm_mod$model_type, "GLMM")
expect_equal(glmm_mod_offset$model_type, "GLMM")
expect_s3_class(re_sumy, "data.frame")
expect_equal(nrow(re_sumy), 6)
expect_equal(nrow(re_sumy), 4)
expect_equal(ncol(re_sumy), 4)
})

Expand Down

0 comments on commit dddb52c

Please sign in to comment.