Last active
June 4, 2019 01:46
-
-
Save ssajous/d3168c86c861e9a24d2a3df3b738c2ad to your computer and use it in GitHub Desktop.
Multiple Linear Regression in R - Automatic Backward Elimination
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
backwardElimination <- function(x, sl) { | |
numVars = length(x) | |
for (i in c(1:numVars)){ | |
regressor = lm(formula = Profit ~ ., data = x) | |
maxVar = max(coef(summary(regressor))[c(2:numVars), "Pr(>|t|)"]) | |
if (maxVar > sl){ | |
j = which(coef(summary(regressor))[c(2:numVars), "Pr(>|t|)"] == maxVar) | |
x = x[, -j] | |
} | |
numVars = numVars - 1 | |
} | |
return(summary(regressor)) | |
} | |
SL = 0.05 | |
dataset = dataset[, c(1,2,3,4,5)] | |
backwardElimination(training_set, SL) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment