多个线性 Reg 多项式项应用于交互作用

Multiple Linear Reg-Applying Polynomial Terms to Interaction Effects

我一直在为此绞尽脑汁,想不出最好的方法是什么。

到目前为止,我进行了初步的 MLR

reg=lm(register~.,data=train)

从那里,我使用

检查交互效果
testinter=glm(register~(.-atemp)*(.-atemp),data=train)

确定显着交互作用后,我将所有交互作用都包含在我的模型中。

reg=lm(register~season:month+season:temp+year:month+
     year:weekday+year:temp+month:temp+holiday:windspeed+weekday:
     weathersit+weekday:hum+weathersit:hum+
     hum+weathersit+season+year+temp, data=train)

但是,在查看了一些变量之后,需要将 hum 和 temp 转换为多项式项。嗡嗡声^2 和温度^3。

我的问题是如何将这些包含在交互效果中? 到目前为止,这是我的尝试,我用 poly(hum,2,raw=T) 切换了 "hum" 但我不确定它是否正确。

reg=lm(register~season:month+season:poly(temp,3,raw=T)+year:month+
     year:weekday+year:poly(temp,3,raw=T)+month:poly(temp,3,raw=T)+holiday:windspeed+weekday:
     weathersit+weekday:poly(hum,2,raw=T)+weathersit:poly(hum,2,raw=T)+
     poly(hum,2,raw=T)+weathersit+season+year+poly(temp,3,raw=T), data=train)

当您需要在 lmglm 方程中进行转换时,您应该使用 I()

这是一个使用 iris 数据集的示例:

data(iris)
reg <- lm(Sepal.Length~Sepal.Width:I(Petal.Length^2), data=iris)
summary(reg)
Call:
lm(formula = Sepal.Length ~ Sepal.Width:I(Petal.Length^2), data = iris)

Residuals:
    Min      1Q  Median      3Q     Max 
-0.9405 -0.2357  0.0029  0.2224  0.8241 

Coefficients:
                               Estimate Std. Error t value Pr(>|t|)    
(Intercept)                   4.8649126  0.0478357  101.70   <2e-16 ***
Sepal.Width:I(Petal.Length^2) 0.0192699  0.0007492   25.72   <2e-16 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.3552 on 148 degrees of freedom
Multiple R-squared:  0.8172,  Adjusted R-squared:  0.816 
F-statistic: 661.6 on 1 and 148 DF,  p-value: < 2.2e-16