I have a long list of variables that I want to throw into a logistic model using the caret
package. However, I want to interact all the variables in the list with one specific variable in the list. For example, I have variables v1, v2, ..., v100
and I want to interact all of these variables with v5
. I'm not sure if there's a handy way of doing this efficiently and quickly without having to type out each pair. Any help with be great. Thanks!
Asked
Active
Viewed 32 times
0

user122514
- 397
- 5
- 13
-
1Are there other variables in the data.frame that you want to igore? Because `.` can be used as a shortcut for "all the variables". Something like `lm(disp ~ mpg * ., data=mtcars)` will fit a model with all the variables in mtcars alone and then as an interaction with mpg. – MrFlick Feb 10 '20 at 20:34
-
Hi @MrFlick, I suppose the only variable I want to "ignore" is the variable I want to interact with (i.e., interacting `var5` with itself, though I don't think this would be problematic). – user122514 Feb 10 '20 at 20:42
-
Then you can remove just that term: `lm(disp ~ mpg * . - mpg, data=mtcars)` – MrFlick Feb 10 '20 at 20:42
-
@MrFlick, after a second look, I do want to exclude a few other variables. Following your example, how would I remove, for example, `v4, v5, v6`? Thanks so much! – user122514 Feb 10 '20 at 20:44
-
I would filter them from the object you pass to `data=`, then you don't have to worry about it. Otherwise you would have to explicitly remove them and their interactions. If you provided a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) this would me much easier rather than talking hypothetically. – MrFlick Feb 10 '20 at 20:46
-
Thanks, based on your answer, I was able to remove all the ones I don't need. Thanks again! – user122514 Feb 10 '20 at 20:55