-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChurn Prediction - Cleaned and Finalized Code.R
168 lines (123 loc) · 5.58 KB
/
Churn Prediction - Cleaned and Finalized Code.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
##Churn Prediction Project - Final Outline
install.packages("C50")
install.packages("ggplot2")
install.packages("tidyverse")
install.packages("ModelMetrics")
library(C50)
library(ggplot2)
library(tidyverse)
library(ModelMetrics)
data(churn)
remove(churnTest)
prop.table(table(churnTrain$churn))
churnTrain$churn <- factor(churnTrain$churn,
levels(churnTrain$churn)[c(2,1)])
#Step 1 - Checking for Redundant Variables
#day, evening, and night calls are not redundant or compensatory
cor(churnTrain$total_day_calls, churnTrain$total_eve_calls)
ggplot(data = churnTrain,
aes(x = total_day_calls, y = total_eve_calls)) +
geom_point()
cor(churnTrain$total_day_calls, churnTrain$total_night_calls)
ggplot(data = churnTrain,
aes(x = total_day_calls, y = total_night_calls)) +
geom_point()
cor(churnTrain$total_eve_calls, churnTrain$total_night_calls)
ggplot(data = churnTrain,
aes(x = total_eve_calls, y = total_night_calls)) +
geom_point()
#day, evening, and night minutes are not redundant or compensatory
cor(churnTrain$total_day_minutes, churnTrain$total_eve_minutes)
ggplot(data = churnTrain,
aes(x = total_day_minutes, y = total_eve_minutes)) +
geom_point()
cor(churnTrain$total_day_minutes, churnTrain$total_night_minutes)
ggplot(data = churnTrain,
aes(x = total_day_minutes, y = total_night_minutes)) +
geom_point()
cor(churnTrain$total_eve_minutes, churnTrain$total_night_minutes)
ggplot(data = churnTrain,
aes(x = total_eve_minutes, y = total_night_minutes)) +
geom_point()
#checking correlation between calls and call length for each time of day
cor(churnTrain$total_day_calls, churnTrain$total_day_minutes)
ggplot(data = churnTrain,
aes(x = total_day_calls, y = total_day_minutes)) +
geom_point()
cor(churnTrain$total_eve_calls, churnTrain$total_eve_minutes)
ggplot(data = churnTrain,
aes(x = total_eve_calls, y = total_eve_minutes)) +
geom_point()
cor(churnTrain$total_night_calls, churnTrain$total_night_minutes)
ggplot(data = churnTrain,
aes(x = total_night_calls, y = total_night_minutes)) +
geom_point()
#checking the number of total non-users
sum(churnTrain$total_day_calls == 0)
sum(churnTrain$total_eve_calls == 0)
sum(churnTrain$total_night_calls == 0)
#charge and minutes are redundant - the telecom company charges a set rate per
# minute of phone conversation
cor(churnTrain$total_day_charge, churnTrain$total_day_minutes)
ggplot(data = churnTrain,
aes(x = total_day_charge, y = total_day_minutes)) +
geom_point()
cor(churnTrain$total_eve_charge, churnTrain$total_eve_minutes)
ggplot(data = churnTrain,
aes(x = total_eve_charge, y = total_eve_minutes)) +
geom_point()
cor(churnTrain$total_night_charge, churnTrain$total_night_minutes)
ggplot(data = churnTrain,
aes(x = total_night_charge, y = total_night_minutes)) +
geom_point()
day_price <- churnTrain$total_day_charge/churnTrain$total_day_minutes
eve_price <- churnTrain$total_eve_charge/churnTrain$total_eve_minutes
night_price <- churnTrain$total_night_charge/churnTrain$total_night_minutes
#Step 2 - Data Tranformations: create variables that might be useful, in this
# case the ratio of customer service calls over account length
churnTrain$problem_ratio <- churnTrain$number_customer_service_calls/churnTrain$account_length
churnTrain$day_min_ratio <- churnTrain$total_day_minutes/churnTrain$account_length
churnTrain$eve_min_ratio <- churnTrain$total_eve_minutes/churnTrain$account_length
churnTrain$night_min_ratio <- churnTrain$total_night_minutes/churnTrain$account_length
#Step 3 - Explore continuous predictors with t-tests
t.test(account_length~churn, data = churnTrain)
t.test(number_customer_service_calls~churn, data = churnTrain)
t.test(problem_ratio~churn, data = churnTrain)
#Step 4 - Exploring factor predictors with logistic regression
state.model <- glm(churn~state, family = binomial(link = "logit"),
data = churnTrain)
summary(state.model)
international.model <- glm(churn~international_plan,
family = binomial(link = "logit"),
data = churnTrain)
summary(international.model)
voice_mail_model <- glm(churn~voice_mail_plan, family = binomial(link = "logit"),
data = churnTrain)
summary(voice_mail_model)
#Step 5 - ratios vs. totals
ratios.model <- glm(churn~day_min_ratio+eve_min_ratio+night_min_ratio,
family = binomial(link="logit"), data = churnTrain)
summary(ratios.model)
totals.model <- glm(churn~total_day_minutes+total_eve_minutes+total_night_minutes,
family = binomial(link="logit"), data = churnTrain)
summary(totals.model)
#Step 6 - putting the final model together
final_churn_model <- glm(churn~number_customer_service_calls+international_plan+
voice_mail_plan+total_day_minutes+total_eve_minutes+
total_night_minutes, family = binomial(link="logit"),
data = churnTrain)
summary(final_churn_model)
#Step 7 - interpret the model
#Step 8 - in-sample predictions
churn_prob <- predict(final_churn_model, newdata = churnTrain,
type = "response")
churnTrain$churn_prob <- churn_prob
churnTrain %>%
group_by(churn) %>%
summarise(med = median(churn_prob))
churn_guess <- ifelse(churn_prob > .267, 2, 1)
(sum(as.numeric(churnTrain$churn)==churn_guess))/length(churnTrain$churn)
auc(actual = churnTrain$churn, predicted = churn_guess)
#Step 9 - out-of-sample predictions
churn_predicts <- predict(final_churn_model, newdata = Customers_To_Predict,
type = "response")