Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes for #38 regarding nested effects #41

Merged
merged 1 commit into from
Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Imports:
Suggests:
covr,
gee,
ggplot2,
knitr,
nlme,
rmarkdown,
Expand Down
12 changes: 9 additions & 3 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
42 changes: 12 additions & 30 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions multilevelmod.Rproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ LineEndingConversion: Posix

BuildType: Package
PackageUseDevtools: Yes
PackageCleanBeforeInstall: Yes
PackageInstallArgs: --no-multiarch --with-keep.source
PackageRoxygenize: rd,collate,namespace
26 changes: 26 additions & 0 deletions tests/testthat/test-linear-reg-lme4.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
)



})