Delete a variable wrapped in a function from the model formula in R

I have a model with converted variables, for example:

data = data.frame(y = runif(100,0,10), x1 = runif(100,0,10), x2 = runif(100, 0, 10)) mod = lm(y ~ scale(x1) + scale(x2), data) 

I would like to remove one integer variable from the formula, for example:

 mod = lm(y ~ scale(x1), # x2 is gone! data) 

But I would like to do this using the user-provided character string of the variable that needs to be deleted (in other words, I wrap it in a function and it is impossible to edit the formula manually, as I have here).

If the variable was not translated, this would be simple with gsub :

 remove.var = "x2" update(mod, formula. = as.formula(gsub(remove.var, "", format(formula(mod))))) 

but as such it returns a completely predictable error:

  > Error in as.matrix(x) : argument "x" is missing, with no default 

because scale() is still in the formula!

Is there a way to do this with regexpr , or in some way that I don't see, is this completely obvious? I would like it to be scalable for other types of transforms, for example: log , log10 , etc.

As another level of complexity, suppose that the variable to be deleted also appeared in the interaction:

  mod = lm(y ~ scale(x1) * scale(x2), data) 

In this case, it would be necessary to remove the interaction * (errant + s, I found, in order).

Any help is greatly appreciated. Thanks!

+5
source share
2 answers

A term object is a formula with additional attributes:

 update(mod, formula=drop.terms(mod$terms, 2, keep.response=TRUE) ) Call: lm(formula = y ~ scale(x1), data = data) Coefficients: (Intercept) scale(x1) 5.0121 0.1236 

If you need to calculate this position from a string argument, you can grep the term.labels attribute:

 > grep( "x2", attr( mod$terms, "term.labels") ) [1] 2 

Note that this is also possible with the interaction formula:

 update(mod, formula=drop.terms(mod$terms, grep( "x2", attr( mod$terms, "term.labels") ), keep.response=TRUE) ) #---------- Call: lm(formula = y ~ scale(x1), data = data) Coefficients: (Intercept) scale(x1) 5.0121 0.1236 
+3
source

I'm sure there is another way to do this with the caret package, but I think this does what you want:

 vars<- c("tom", "dick", "harry") remove<- "tom" vars<- setdiff(vars, remove) formula <- as.formula( paste( "y ~", paste("scale(", vars, ")", collapse = "+"))) 
0
source

Source: https://habr.com/ru/post/1206750/


All Articles