Skip to content

Commit

Permalink
fix formula
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 17, 2024
1 parent bde796c commit bd0b0f9
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions R/find_formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,26 @@ find_formula.default <- function(x, verbose = TRUE, ...) {
#' @export
find_formula.asym <- function(x, verbose = TRUE, ...) {
modified_f <- safe_deparse(stats::formula(x))
while (grepl("minus__", modified_f)) {
modified_f <- gsub("(.*)minus__(\\S+)(.*)", "\\1\\3", modified_f)
modified_f <- gsub("+ ", "", modified_f, fixed = TRUE)
modified_f <- gsub("* ", "*", modified_f, fixed = TRUE)
}
# modified_f <- gsub("(.*)lag_(.*)_(.*)", "\\1lag(\\2)\\3", modified_f)
f <- .safe(list(conditional = stats::as.formula(modified_f)))
# limitation: we can't preserve "*" and ":"
modified_f <- gsub("*", "+", modified_f, fixed = TRUE)
modified_f <- gsub(":", "+", modified_f, fixed = TRUE)
# explanation:
# - gsub("\\+\\s*minus__[^\\+]+", "", input_string):
# This regular expression matches and removes any term that starts with
# + minus__ followed by any characters that are not a +.
# - gsub("\\s*\\+\\s*$", "", output_string):
# This removes any trailing plus sign and whitespace that might be left
# at the end of the string.
output_string <- gsub("\\+\\s*minus__[^\\+]+", "", modified_f)
output_string <- gsub("\\s*(\\+|\\*)\\s*$", "", output_string) # Remove trailing plus sign if any
# explanation:
# - gsub("lag_([a-zA-Z]+)_", "lag(\\1)", input_string):
# This regular expression matches the pattern "lag_", followed by one or
# more letters (captured in a group), followed by "_". It replaces this
# pattern with "lag(", the captured group, and ")".
output_string <- gsub("lag_([a-zA-Z]+)_", "lag(\\1)", output_string)
output_string <- gsub("plus__", "", output_string, fixed = TRUE)
f <- .safe(list(conditional = stats::as.formula(output_string)))
.find_formula_return(f, verbose = verbose)
}

Expand Down

0 comments on commit bd0b0f9

Please sign in to comment.