Skip to content

Commit

Permalink
fix issue with poly() in re terms #1221
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Nov 8, 2024
1 parent f7106d4 commit bfc6398
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 7 deletions.
12 changes: 8 additions & 4 deletions R/brmsframe.R
Original file line number Diff line number Diff line change
Expand Up @@ -247,12 +247,16 @@ get_levels.list <- function(x, ...) {
out <- vector("list", length(x))
for (i in seq_along(out)) {
levels <- get_levels(x[[i]], ...)
if (!length(levels)) {
next
}
if (is.list(levels)) {
stopifnot(!is.null(names(levels)))
out[[i]] <- as.list(levels)
} else if (!is.null(levels)) {
stopifnot(isTRUE(nzchar(names(x)[i])))
out[[i]] <- setNames(list(levels), names(x)[[i]])
} else if (is.vector(levels)) {
name_i <- names(x)[i]
stopifnot(isTRUE(nzchar(name_i)))
out[[i]] <- setNames(list(levels), name_i)
}
}
out <- unlist(out, recursive = FALSE)
Expand All @@ -267,7 +271,7 @@ get_levels.brmsterms <- function(x, data = NULL, ...) {
return(out)
}
if (!is.null(data)) {
ls <- list(frame_re(x, data), frame_me(x, data))
ls <- list(frame_re_levels_only(x, data), frame_me(x, data))
out <- get_levels(ls)
}
out
Expand Down
19 changes: 19 additions & 0 deletions R/formula-re.R
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,25 @@ frame_re <- function(bterms, data, old_levels = NULL) {
out
}

# like frame_re but only returns its levels attribute
# this avoids issue #1221 and likely some other edge cases
frame_re_levels_only <- function(bterms, data) {
out <- empty_reframe()
data <- combine_groups(data, get_group_vars(bterms))
re <- get_re(bterms)
re <- re[!duplicated(re$group), ]
levels <- named_list(re$group)
for (i in seq_along(levels)) {
# combine levels of all grouping factors within one grouping term
levels[[i]] <- unique(ulapply(
re$gcall[[i]]$groups,
function(g) extract_levels(get(g, data))
))
}
set_levels(out) <- levels
out
}

empty_reframe <- function() {
out <- data.frame(
id = numeric(0), group = character(0), gn = numeric(0),
Expand Down
5 changes: 2 additions & 3 deletions R/formula-sp.R
Original file line number Diff line number Diff line change
Expand Up @@ -297,8 +297,7 @@ frame_me <- function(x, data, old_levels = NULL) {
term = uni_me, xname = "", grname = "",
stringsAsFactors = FALSE
)
unique_grnames <- unique(out$grname)
levels <- named_list(unique_grnames)
levels <- list()
for (i in seq_rows(out)) {
tmp <- eval2(out$term[i])
out$xname[i] <- tmp$term
Expand All @@ -313,7 +312,7 @@ frame_me <- function(x, data, old_levels = NULL) {
out$cor <- isTRUE(x$mecor)
if (!is.null(old_levels)) {
# for newdata numeration has to depend on the original levels
set_levels(out) <- old_levels[[unique_grnames]]
set_levels(out) <- old_levels
set_levels(out, "used") <- levels
} else {
set_levels(out) <- levels
Expand Down

0 comments on commit bfc6398

Please sign in to comment.