Advertisement
STANAANDREY

auto Backward Elim

May 19th, 2024
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 0.51 KB | None | 0 0
  1. backwardElimination <- function(x, sl) {
  2.     numVars = length(x)
  3.     for (i in c(1:numVars)){
  4.       regressor = lm(formula = Profit ~ ., data = x)
  5.       maxVar = max(coef(summary(regressor))[c(2:numVars), "Pr(>|t|)"])
  6.       if (maxVar > sl){
  7.         j = which(coef(summary(regressor))[c(2:numVars), "Pr(>|t|)"] == maxVar)
  8.         x = x[, -j]
  9.       }
  10.       numVars = numVars - 1
  11.     }
  12.     return(summary(regressor))
  13.   }
  14.  
  15.   SL = 0.05
  16.   dataset = dataset[, c(1,2,3,4,5)]
  17.   backwardElimination(training_set, SL)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement