From acce03c59236f6a787afc14acb408f49f20ec83d Mon Sep 17 00:00:00 2001 From: topepo Date: Thu, 16 Jun 2022 17:31:00 -0400 Subject: [PATCH] changes for #38 --- DESCRIPTION | 1 + R/misc.R | 12 ++++++-- inst/WORDLIST | 42 ++++++++------------------- multilevelmod.Rproj | 1 + tests/testthat/test-linear-reg-lme4.R | 26 +++++++++++++++++ 5 files changed, 49 insertions(+), 33 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index dc2bfe6..9ed0586 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -29,6 +29,7 @@ Imports: Suggests: covr, gee, + ggplot2, knitr, nlme, rmarkdown, diff --git a/R/misc.R b/R/misc.R index e9c0686..35eba7c 100644 --- a/R/misc.R +++ b/R/misc.R @@ -8,9 +8,15 @@ reformat_lme_pred_data <- function(x, object) { random_effects <- names(lme4::ranef(object$fit)) - for (i in random_effects) { - lvl <- levels(object$fit@frame[[i]]) - x[[i]] <- factor(lvl[1], levels = lvl) + random_effects <- paste(random_effects, collapse = "+") + random_effects_f <- as.formula(paste("~", random_effects)) + random_effect_cols <- all.vars(random_effects_f) + vals <- dplyr::select(object$fit@frame, dplyr::all_of(random_effect_cols)) + vals <- vals[1,,drop = FALSE] + + for (i in random_effect_cols) { + lvl <- levels(vals[[i]]) + x[[i]] <- factor(vals[[i]][1], levels = lvl) } x } diff --git a/inst/WORDLIST b/inst/WORDLIST index e2b2286..390114b 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -1,47 +1,29 @@ Bech -Biometrics -Biometrika -CED CMD -CRC -Cano Codecov -Correa -DN -DeBruine -Galecki -Hodgson Imipramine -Inger -LM Liang Lifecycle -McElreath -Minto -PeerJ +ORCID Pinheiro Reisby -Sorensen -Springer -Thorson -Vasishth -Welch -XA Zeger al -arXiv +biomet +doi et -id’ +funder +glmer +gls +lme +lmer mixedlevelmod -modelling +nlme pharmacokinetic +poisson reprex +stan tibble +tidybayes tidymodels warmup -th -β -β̂ -η -η̂ -ORCID diff --git a/multilevelmod.Rproj b/multilevelmod.Rproj index 766b3b2..f87a09e 100755 --- a/multilevelmod.Rproj +++ b/multilevelmod.Rproj @@ -18,5 +18,6 @@ LineEndingConversion: Posix BuildType: Package PackageUseDevtools: Yes +PackageCleanBeforeInstall: Yes PackageInstallArgs: --no-multiarch --with-keep.source PackageRoxygenize: rd,collate,namespace diff --git a/tests/testthat/test-linear-reg-lme4.R b/tests/testthat/test-linear-reg-lme4.R index 343cc8e..b32b800 100644 --- a/tests/testthat/test-linear-reg-lme4.R +++ b/tests/testthat/test-linear-reg-lme4.R @@ -46,3 +46,29 @@ test_that('mode specific package dependencies', { list(c("lme4", "multilevelmod")) ) }) + + +test_that('random interactions and/or nesting', { + skip_if_not_installed("lme4") + skip_if_not_installed("ggplot2") + skip_on_cran() + + data(mpg, package = "ggplot2") + + fit <- + linear_reg() %>% + set_engine("lmer") %>% + fit(cty ~ year + (1|manufacturer/model), data = mpg) + + expect_error(predict(fit, new_data = mpg), regexp = NA) + # Now predict with new levels for the random effect columns, esp + # when there is nesting/interactions + expect_error( + predict(fit, new_data = tibble::tibble(year = 2000, model = "dan", manufacturer = "max")), + regexp = NA + ) + + + +}) +