R update function, how to drop all the variables that are related to a pre-specified variable

StackOverflow https://stackoverflow.com/questions/23381616

  •  12-07-2023
  •  | 
  •  

Question

I have a R formula object:

 formula1 <- y ~ x1 + x2 + x3 + x1:x2

I want to update this formula object by dropping all x1-related variables. That is x1 AND x1:x2.

If I use an update function,

 update(formula1,.~.-x1)

I get:

 y ~ x2 + x3 + x1:x2

This is not what I want. In practice, I do not know how many interaction effects of x1 are defined in the formula1 object.

Is there simple way to get

 y ~ x2 + x3
Was it helpful?

Solution

Here's a function to remove a term including all interactions in which the term is present:

remove_terms <- function(form, term) {
  fterms <- terms(form)
  fac <- attr(fterms, "factors")
  idx <- which(as.logical(fac[term, ]))
  new_fterms <- drop.terms(fterms, dropx = idx, keep.response = TRUE)
  return(formula(new_fterms))
}

Apply the function:

formula1 <- y ~ x1 + x2 + x3 + x1:x2

# the term that should be removed
to_remove <- "x1"

remove_terms(formula1, to_remove)
# y ~ x2 + x3
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top