# How to include an interaction with a quadratic term? [closed]

I want to predict $$y$$ with $$x_{1}$$ and $$x_{2}$$ and I suppose that $$x_{2}$$ has a quadratic effect on $$y$$ and that there is an interaction. How to model that?

I've look in previous questions but there seem to be different suggestions.

1. Include all possible effects separately (see model 2):

$$y$$ ~ $$x_{1} + x_{2} + x_{2}^{2} + x_{1} : x_{2} + x_{1} : x_{2}^{2}$$

2. Keep all the parts of your polynomial variable together:

$$y$$ ~ $$x_{1} + x_{2} + x_{2}^{2} + x_{1} : (x_{2} + x_{2}^{2})$$

I use the notation of R where $$y$$ ~ $$x_{1} + x_{2} + x_{1} : x_{2}$$, for example, means that there are two main effects, namely $$x_{1}$$ and $$x_{2}$$, and an interaction between $$x_{1}$$ and $$x_{2}$$. In R there is no need to specify the intercept, but it is estimated by default, too.

## closed as off-topic by mkt, whuber♦May 22 at 18:30

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "This question appears to be off-topic because EITHER it is not about statistics, machine learning, data analysis, data mining, or data visualization, OR it focuses on programming, debugging, or performing routine operations within a statistical computing platform. If the latter, you could try the support links we maintain." – mkt, whuber
If this question can be reworded to fit the rules in the help center, please edit the question.

It's the same formula (meaning that the models are equivalent), just the R notation is different.

Here is an example with random data:

x1 <- rnorm(100)
x2 <- rnorm(100)
y <- x1 + x2 + x2**2 + x1*x2 + rnorm(100)

fit <- lm(y ~ x1 + x2 + I(x2^2) + x1:x2 + x1:I(x2^2))

fit <- lm(y ~ x1 + x2 + I(x2^2) + x1:(x2 + I(x2^2)))

fit <- lm(y ~ x1 + x2 + I(x2*x2) + x1:(x2 + I(x2*x2)))


All three of these produce these same results where x1 is interacted with both x2 and the squared version of x2:

Residuals:
Min       1Q   Median       3Q      Max
-2.12678 -0.64983  0.03115  0.59760  2.26080

Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept)   -0.11838    0.12757  -0.928    0.356
x1             0.95627    0.13901   6.879 6.61e-10 ***
x2             1.04394    0.09099  11.473  < 2e-16 ***
I(x2 * x2)     0.94417    0.06015  15.698  < 2e-16 ***
x1:x2          1.05098    0.12875   8.163 1.45e-12 ***
x1:I(x2 * x2)  0.05926    0.09656   0.614    0.541
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 1.003 on 94 degrees of freedom
Multiple R-squared:  0.8412,    Adjusted R-squared:  0.8328
F-statistic: 99.59 on 5 and 94 DF,  p-value: < 2.2e-16