-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLogistic_Regression.R
More file actions
74 lines (52 loc) · 2.09 KB
/
Copy pathLogistic_Regression.R
File metadata and controls
74 lines (52 loc) · 2.09 KB
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
65
66
67
68
69
70
71
72
73
rm(list=ls(all=TRUE))
#Set the work directory where CustomerData_Classification.csv is #placed
#setwd("")
data<-read.csv("CustomerData_Classification.csv",header=T)
str(data)
summary(data)
# Preprocessing
# remove CustomerID column
data = data[,-1]
# convert City attribute as factor
data$City = as.factor(as.character(data$City))
# Convert taget attribute as factor
data$Churned<-as.factor(as.character(data$Churned))
dataForModel = data
#Split the data into train and test data sets
rows=seq(1,nrow(dataForModel),1)
set.seed(123)
trainRows=sample(rows,(70*nrow(dataForModel))/100)
train = dataForModel[trainRows,]
test = dataForModel[-trainRows,]
# Build Logistic regression and interpret the results
LogReg <- glm(Churned ~ ., data=train, family=binomial)
summary(LogReg)
# train results
prob<-predict(LogReg, type="response")
pred_class <- ifelse(prob> 0.5, 1, 0)
table(train$Churned,pred_class)
# Test results
fitted.results <- predict(LogReg,test,type='response')
fitted.class <- ifelse(fitted.results > 0.5,1,0)
table(test$Churned,fitted.class)
#As a last step, we are going to plot the ROC curve and calculate the AUC
#(area under the curve) which are typical performance measurements
#for a binary classifier.
#The ROC (Receiver Operating Characteristic curve) is a curve generated by plotting the true positive rate (TPR = sensitivity) against
# the false positive rate (FPR= specificity) at various threshold settings while the AUC is
# the area under the ROC curve. As a rule of thumb, a model with good
#predictive ability should have an AUC closer to 1 (1 is ideal) than to 0.5.
library(ROCR)
p <- predict(LogReg,test, type="response")
pr <- prediction(p, test$Churned)
prf <- performance(pr, measure = "tpr", x.measure = "fpr")
plot(prf,colorize = TRUE, print.cutoffs.at=seq(0,1,by=0.1), text.adj=c(-0.2,1.7))
abline(a=0, b= 1)
auc <- performance(pr, measure = "auc")
auc <- auc@y.values[[1]]
auc # very low
# Error Metric
conf.mat = table(test$Churned,fitted.class)
accuracy = sum(diag(conf.mat))/sum(conf.mat)
precision = conf.mat[2,2]/sum(conf.mat[,2])
recall = conf.mat[2,2]/sum(conf.mat[2,])