如何包含与二次项的交互?
机器算法验证
回归
相互作用
二次型
2022-04-05 00:46:51
1个回答
这是相同的公式(意味着模型是等价的),只是 R 符号不同。
这是一个随机数据的例子:
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)))
所有这三个产生这些相同的结果,其中x1与两者x2和平方版本交互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
其它你可能感兴趣的问题