Skip to content

Commit

Permalink
fix issue #1520
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-buerkner committed Jul 6, 2023
1 parent 40d0587 commit 865d4f8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
12 changes: 7 additions & 5 deletions R/brmsterms.R
Original file line number Diff line number Diff line change
Expand Up @@ -921,6 +921,7 @@ all_terms <- function(x) {

# generate a regular expression to extract special terms
# @param type one or more special term types to be extracted
# TODO: rule out expressions such as mi(y) + mi(x)
regex_sp <- function(type = "all") {
choices <- c("all", "sp", "sm", "gp", "cs", "mmc", "ac", all_sp_types())
type <- unique(match.arg(type, choices, several.ok = TRUE))
Expand Down Expand Up @@ -969,13 +970,14 @@ find_terms <- function(x, type, complete = TRUE, ranef = FALSE) {
if (complete) {
matches <- lapply(out, get_matches_expr, pattern = regex)
# each term may contain only one special function call
inv <- out[lengths(matches) > 1L]
if (!length(inv)) {
invalid <- out[lengths(matches) > 1L]
if (!length(invalid)) {
# each term must be exactly equal to the special function call
inv <- out[unlist(matches) != out]
invalid <- out[unlist(matches) != out]
}
if (length(inv)) {
stop2("The term '", inv[1], "' is invalid in brms syntax.")
# TODO: some terms can be part of I() calls (#1520); reflect this here?
if (length(invalid)) {
stop2("The term '", invalid[1], "' is invalid in brms syntax.")
}
}
out
Expand Down
14 changes: 7 additions & 7 deletions R/conditional_effects.R
Original file line number Diff line number Diff line change
Expand Up @@ -564,14 +564,14 @@ get_all_effects_type <- function(x, type) {
term_parts <- unlist(strsplit(terms[i], split = ":"))
vars <- vector("list", length(term_parts))
for (j in seq_along(term_parts)) {
if (grepl_expr(regex_type, term_parts[j])) {
# evaluate a special term to extract variables
tmp <- eval2(term_parts[j])
vars[[j]] <- setdiff(unique(c(tmp$term, tmp$by)), "NA")
} else {
# extract all variables from an ordinary term
vars[[j]] <- all_vars(term_parts[j])
matches <- get_matches_expr(regex_type, term_parts[j])
for (k in seq_along(matches)) {
# evaluate special terms to extract variables
tmp <- eval2(matches[[k]])
c(vars[[j]]) <- setdiff(unique(c(tmp$term, tmp$by)), "NA")
}
# extract all variables not part of any special term
c(vars[[j]]) <- setdiff(all_vars(term_parts[j]), all_vars(matches))
}
vars <- unique(unlist(vars))
out[[i]] <- str2formula(vars, collapse = "*")
Expand Down

0 comments on commit 865d4f8

Please sign in to comment.