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),
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!