Skip to content

Commit

Permalink
Check the LHS of formula for any interaction terms. Closes #21
Browse files Browse the repository at this point in the history
  • Loading branch information
DavisVaughan committed Feb 20, 2019
1 parent f4c49ed commit 4e8a688
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
50 changes: 50 additions & 0 deletions R/mold-formula.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,10 @@ mold_formula_outcomes <- function(formula, data) {
original_levels <- get_levels(original_data)

formula <- get_outcomes_formula(formula)

# used on the `~ LHS` formula
validate_no_interactions(formula)

framed <- model_frame(formula, data)

outcomes <- flatten_embedded_columns(framed$data)
Expand Down Expand Up @@ -491,6 +495,52 @@ detect_factors_in_interactions <- function(.terms, .factor_names) {
bad_cols
}

validate_no_interactions <- function(.formula) {

bad_terms <- detect_interactions(.formula)

no_interactions <- length(bad_terms) == 0L
if (no_interactions) {
return(invisible(.formula))
}

bad_terms <- glue::glue_collapse(glue::single_quote(bad_terms), ", ")

glubort(
"Interaction terms cannot be specified on the LHS of `formula`. ",
"The following interaction terms were found: {bad_terms}."
)
}

# Returns processed names of any interaction terms
# like 'Species:Sepal.Width', or character(0)
detect_interactions <- function(.formula) {

.terms <- terms(.formula)

terms_matrix <- attr(.terms, "factors")

only_intercept_or_offsets <- length(terms_matrix) == 0L
if (only_intercept_or_offsets) {
return(character(0))
}

terms_nms <- colnames(terms_matrix)

# All interactions (*, ^, %in%) will be expanded to `:`
has_interactions <- grepl(":", terms_nms)

has_any_interactions <- any(has_interactions)

if (!has_any_interactions) {
return(character(0))
}

bad_terms <- terms_nms[has_interactions]

bad_terms
}

extract_original_factor_names <- function(.data_classes) {

no_data_classes <- length(.data_classes) == 0L
Expand Down
29 changes: 29 additions & 0 deletions tests/testthat/test-mold.R
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,35 @@ test_that("`indicators = FALSE` runs numeric interactions", {

})

test_that("LHS of the formula cannot contain interactions", {

expect_error(
mold(Sepal.Length:Sepal.Width ~ Sepal.Width, iris),
"The following interaction terms were found: 'Sepal.Length:Sepal.Width'"
)

expect_error(
mold(Sepal.Length*Sepal.Width ~ Sepal.Width, iris),
"The following interaction terms were found: 'Sepal.Length:Sepal.Width'"
)

expect_error(
mold(Sepal.Length %in% Sepal.Width ~ Sepal.Width, iris),
"The following interaction terms were found: 'Sepal.Length:Sepal.Width'"
)

expect_error(
mold((Sepal.Length + Sepal.Width)^2 ~ Sepal.Width, iris),
"The following interaction terms were found: 'Sepal.Length:Sepal.Width'"
)

expect_error(
mold(Sepal.Length:Sepal.Width + Species:Sepal.Length ~ Sepal.Width, iris),
"The following interaction terms were found: 'Sepal.Length:Sepal.Width', 'Sepal.Length:Species'"
)

})

# ------------------------------------------------------------------------------
context("test-mold-xy")

Expand Down

0 comments on commit 4e8a688

Please sign in to comment.