-
Notifications
You must be signed in to change notification settings - Fork 0
/
Linear model and Automatic Backward Elimination.R
64 lines (54 loc) · 1.98 KB
/
Linear model and Automatic Backward Elimination.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Importing dataset
dataset = read.csv('50_Startups.csv')
# Encoding categorical data
dataset$State = factor(dataset$State,
levels = c('New York', 'California', 'Florida'),
labels = c(1, 2, 3))
# Splitting dataset into training set an test set
library(caTools)
set.seed(123)
split = sample.split(dataset$Profit, SplitRatio = 0.8)
training_set = subset(dataset, split == TRUE)
test_set = subset(dataset, split == FALSE)
# regressor = lm(formula = Profit ~ R.D.Spend + Administration + Marketing.Spend + State,
# data = training_set)
# Applying linear model
regressor = lm(formula = Profit ~ .,
data = trainig_set)
summary(regressor)
# Predicting test set results
y_pred = predict(regressor, newdata = test_set)
# Creating automated backward elimination function
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))
}
# Applying backward eliminated function
SL = 0.05
dataset = dataset[, c(1,2,3,4,5)]
backwardElimination(training_set, SL)
# Applying linear model to selected features
regressor_new = lm(formula = Profit ~ R.D.Spend,
data = trainig_set)
summary(regressor_new)
# Predicting test set results
y_pred = predict(regressor_new, newdata = test_set)
# Visualizing test set results
library(ggplot2)
ggplot() +
geom_point(aes(x = test_set$R.D.Spend, y = test_set$Profit),
colour = 'red') +
geom_line(aes(x = training_set$R.D.Spend, y = predict(regressor_new, newdata = training_set)),
colour = 'blue') +
ggtitle('R.D.Spend vs Profit (Test set)') +
xlab('R.D.Spend') +
ylab('Profit')