3

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.

David Z
  • 6,641
  • 11
  • 50
  • 101

1 Answers1

5

Rather than assigning loose variables in the global environment, it would be better to assemble the formulae in a list. This can be done with lapply(), and leveraging the useful trick that allows you to create a formula from a character string:

lapply(names(df)[-(1:3)],function(var) formula(paste0('y~x1+x2+x1*',var),env=globalenv()));
## [[1]]
## y ~ x1 + x2 + x1 * x3
##
## [[2]]
## y ~ x1 + x2 + x1 * x4
bgoldst
  • 34,190
  • 6
  • 38
  • 64
  • Interesting answer, does your solution apply to [this similar question](https://stackoverflow.com/questions/58756403/combinations-of-variables-that-produce-the-smallest-quantities-in-an-r-function)? – rnorouzian Nov 08 '19 at 01:30