views:

70

answers:

1

In the statistics programming language R, the following formula (as used in lm() or glm())

z ~ (x+y)^2  

is equivalent to

z ~ x + y + x:y

Assuming, I only have continuous predictors, is there a concise way to obtain

z ~ I(x^2) + I(y^2) + I(x) + I(y) + I(x*y)

A formula that does the right thing for factor predictors is a plus.

One possible solution is

z ~ (poly(x,2) + poly(y,2))^2

I am looking for something more elegant.

+5  A: 

I don't know if it is more elegant or not, but the poly function can take multiple vectors:

z ~ poly(x, y, degree=2)

This will create all the combinations that you asked for, without additional ones. Do note that you need to specify degree=2, not just 2 when doing it this way.

Greg Snow