2

I know, that when I'm using lm() or glm() function to fit the regression model in R, it's possible to write interactions up to n-th degree like this:

fit <- glm(formula=outVar ~ (inVar1 + inVar2 + inVar3)^n,
           data=d)

But is it possible to do similar thing with the power of variables, so I don't have to specify I(inVar1^2), I(inVar1^3) and to exclude interactions between different powers of the same variable?


EDIT

I'd like to do something like this:

formula=outVar ~ (poly(inVar1 + inVar2 + inVar3, 2))^2

So I'd get the formula

outVar ~ inVar1 + inVar2 + inVar3 + I(inVar1^2) + I(inVar2^2) + I(inVar3^2) + inVar1:inVar2 + inVar1:inVar3 + inVar2:inVar3 + I(inVar1^2):I(inVar2^2) + I(inVar1^2):I(inVar3^2) + I(inVar1^2):I(inVar3^2) + inVar1:I(inVar2^2) + inVar1:I(inVar3^2)...
Eenoku
  • 2,741
  • 4
  • 32
  • 64
  • @李哲源ZheyuanLi Could you, please, provide an example with conflicting interactions (e.g. `I(inVar1^2):I(inVar1^3)`) excluded? – Eenoku Mar 21 '17 at 09:11
  • 2
    @Eenoku, it could be helpful to write the expected long form out in the question (i.e. all terms you want to include), then people can suggest shorter ways of writing it. – Axeman Mar 21 '17 at 09:12
  • This may be helpful http://stackoverflow.com/documentation/r/1061/formula/29295/create-linear-quadratic-and-second-order-interaction-terms#t=201703210915463399866 – Sathish Mar 21 '17 at 09:16
  • @Axeman Example added into the question – Eenoku Mar 21 '17 at 09:30
  • `as.formula(paste('y ~', paste0('poly(', variable_names, ', n)')))` might be the easiest –  Mar 21 '17 at 09:30

0 Answers0