24 September 2023
Understanding R Formulas
by markolenik
Why is the formula x ~ (y + z)^2
equivalent to x ~ y*z
?
Here are the exact transformation steps:
- Step 1: Start with the formula
x ~ (y + z)^2
and take the square, we gety^2 + z^2 + 2*y*z
. - Step 2: In R, the square of a term in a formula (like
y^2
orz^2
) just refers to the term itself. So,y^2
becomesy
andz^2
becomesz
. Now, the formula isx ~ y + z + 2*y*z
. - Step 4: The coefficient
2
in front of the interaction termy*z
doesn’t change the nature of the interaction, so it is dropped. Now, the formula isx ~ y + z + y*z
. - Step 5: Since
y*z
is a shorthand fory+z+y:z
, we getx ~ 2y + 2z+ y:z
. Again we can get rid of the coefficients, which simplifies tox ~ y + z + y:z
, i.e.x ~ y*z
.