R 逐步回归 - 更改 entry/removal 标准和模型变量显着性

R regression stepwise - Change entry/removal criteria, and model variable significance

我一直在使用 lm 回归函数并使用逐步回归。不幸的是,stepwise 似乎没有太多的灵活性。 Entry/Removal 无法调整条件和重要性。

使用mtcars,我运行这些代码

FitAll <- lm(mpg ~ . ,data=mtcars) # Fit reg model with all variables
FitStart <- lm(mpg~1,data=mtcars) # Fit reg model with just intercept
step(FitStart, direction = "both"  , scope=formula(FitAll)) # stepwise, "both"=forward&backward

它告诉我逐步停止模型中的 3 个变量,wt + cyl + hp。当我 运行 使用这些建立回归模型时,我发现一些变量在 5% 时并不显着。

fit <- lm(formula = mpg ~ wt + cyl + hp, data = mtcars)
summary(fit)

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) 38.75179    1.78686  21.687  < 2e-16 ***
wt          -3.16697    0.74058  -4.276 0.000199 ***
cyl         -0.94162    0.55092  -1.709 0.098480 .  
hp          -0.01804    0.01188  -1.519 0.140015    

有没有办法使用这个 lm 和 step 函数来指定进入和退出标准。此外,有时我想增加对所有显着 1% 的变量的严格性。有没有办法用这种方法指定 entry/exit 标准和显着性水平?有没有更好的包可以使用? 非常感谢任何帮助。谢谢

您可能想尝试 StepReg 包,它似乎提供了您想要的选项

# install.packages("StepReg")

library(StepReg)

stepwise(mtcars, 
         y = "mpg", 
         selection = "bidirection", 
         select = "SL", 
         sle = .01, 
         sls = .01)
#> $process
#>   Step EffectEntered EffectRemoved EffectNumber    Select
#> 1    0     intercept                          1  1.000000
#> 2    1            wt                          2 -9.859915
#> 3    2           cyl                          3 -2.914801
#> 
#> $variate
#> [1] "intercept" "wt"        "cyl"