-4

I would like to find a command in R, in order to calculate all possible models for a given set of variables. For example, for three variables x1, x2 and x3 there are 8 possible models:

m1: x1+x2+x3

m2: x1*x2+x3

m3: x1*x3+x2

m4: x2*x3+x1

m5: x1*x2+x1*x3

m6: x1*x2+x2*x3

m7: x1*x3+x2*x3

m8: x1*x2*x3

If I have 5 variables there are 6894 different models (including all possible interaction) but I would like to confirm with R.

PKumar
  • 10,971
  • 6
  • 37
  • 52
George
  • 5
  • 3

1 Answers1

1

The answer is here

options(na.action = "na.fail") # avoid getting an error

library(MuMIn)

fullmodel <- lm(y ~ x1 * x2 * x3)

dredge(fullmodel, fixed = ~ x1 + x2 + x3)
Davide Passaretti
  • 2,741
  • 1
  • 21
  • 32
  • If I would lke to take constant the main effect of x1, x2 and x3? – George Jan 07 '18 at 07:34
  • @George I edited my answer accordingly. – Davide Passaretti Jan 07 '18 at 07:37
  • Basically, I need to see the possible combinations, keeping constant the main effects of x1, x2, x3. I guess the result would be something like this: [1, 2, 3] [1,2,3 ,12], [1,2,3,13] [1,2,3,23] [1,2,3,12,13] [1,2,3, 12, 23] [1,2,3,13 23] without interactions 123 – George Jan 07 '18 at 07:37
  • @George if you want to omit the 3-way interaction, just do not consider it as a possible output model. It is way more complicated to try to tell the R function to omit it. – Davide Passaretti Jan 07 '18 at 07:50