Suppose I have a data such like this:
n=100
df<-data.frame(y=rnorm(n,2,3),
x1=rbinom(n,1,0.3),
x2=rbinom(n,10, 0.5),
x3=rnorm(n, 50, 20),
x4=rnorm(n, 3, 2))
and I have a basic model y=x1+x2
:
mod0<-as.formula(y~x1+x2)
lm0<-lm(mod0, data=df)
what I want is to update the mod0 with an interaction term between x1 and each of x3 and x4 such that mod1<-y~x1+x2+x1*x3
and mod2<-y~x1+x2+x1*x4
. Since I have a bunch of variables need to loop through, I am wondering what it may be a best way for that.