library(faraway) data(state) state_data <- data.frame(state.x77) # === Backward elimination ===== # Biggest model considered lmod_back <- lm(Life.Exp ~ ., data = state_data) sumary(lmod_back) drop1(lmod_back, test = "F") # one step of backward elimination lmod_back <- update(lmod_back, . ~ . - Area) sumary(lmod_back) drop1(lmod_back, test = "F") # Your turn: continue.... lmod_back <- update(lmod_back, . ~ . - Illiteracy) sumary(lmod_back) drop1(lmod_back, test = "F") lmod_back <- update(lmod_back, . ~ . - Income) sumary(lmod_back) drop1(lmod_back, test = "F") # final model lmod <- lm(Life.Exp ~ Murder + HS.Grad + Frost, data = state_data) # === Forward selection ===== lmod_for <- lm(Life.Exp ~ 1, data = state_data) add1(lmod_for, ~ Population + Income + Illiteracy + Murder + HS.Grad + Frost + Area, test = "F") # one step of forwards selection lmod_for <- update(lmod_for, . ~ . + Murder) add1(lmod_for, ~ Population + Income + Illiteracy + Murder + HS.Grad + Frost + Area, test = "F") # Added: HSGrad, Frost, (Population at cutoff, not added) # final model lmod <- lm(Life.Exp ~ Murder + HS.Grad + Frost, data = state_data) # end up at the same model -- this doesn't always happen # == Why are add1 and drop1 preferred? == # # imagine there was a categorical predictor state_data$region <- state.region lmod_back <- lm(Life.Exp ~ ., data = state_data) # compare sumary(lmod_back) drop1(lmod_back, test = "F") # === Getting leaps to do the work ==== library(leaps) fs <- regsubsets(Life.Exp ~ ., data = state_data, method = "forward") summary(fs) plot(fs) be <- regsubsets(Life.Exp ~ ., data = state_data, method = "backward") summary(be)