Shaun
Shaun

Reputation: 949

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

I've been using the lm regression function and using a stepwise regression. Unfortunately the stepwise doesn't seem to allow much flexibility. Entry/Removal Criteria and Significance can't be adjusted.

Using the mtcars, i run these codes

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

It tells me the stepwise stops with 3 variables in the model, wt + cyl + hp. When I run a regression model with these, I find that some of the variables are not significant at 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    

Is there a way using this lm and step function to specify entry and exit criteria. Also, there are also times when I want to increase the strictness to all variables significant at 1%. Is there any way to specify entry/exit criteria and significance level with this method? Is there a better package to use? Any help much appreciated. Thanks

Upvotes: 1

Views: 971

Answers (1)

Chuck P
Chuck P

Reputation: 3923

You may want to try the package StepReg which appears to offer the options you want

# 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"

Upvotes: 1

Related Questions