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)^2and 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^2orz^2) just refers to the term itself. So,y^2becomesyandz^2becomesz. Now, the formula isx ~ y + z + 2*y*z. - Step 4: The coefficient
2in front of the interaction termy*zdoesn’t change the nature of the interaction, so it is dropped. Now, the formula isx ~ y + z + y*z. - Step 5: Since
y*zis 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.