-
Notifications
You must be signed in to change notification settings - Fork 0
/
University Enrollment.Rmd
373 lines (256 loc) · 9.06 KB
/
University Enrollment.Rmd
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#Load libraries
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(tidymodels)
library(janitor)
library(skimr)
library(kableExtra)
library(GGally)
library(kableExtra)
library(vip) # -- tidymodels variable importance
library(fastshap) # -- shapley values for variable importance
library(MASS)
```
#Import dataset
```{r, message=FALSE, warning=FALSE}
unidata <-read_csv("C:/Users/16179/Downloads/my_R_Project/churn_training.csv") %>%
clean_names()
head(unidata)
```
#Data summary/overview
```{r, message=FALSE, warning=FALSE}
unidata%>%
skim()
```
#Explore enrollment balance
```{r, message=FALSE, warning=FALSE}
unidata%>%
ggplot(aes(x=churn))+
geom_histogram(stat="count")+
labs("Enrollment Count: Yes and No")
pct_exam <- unidata%>%
group_by(churn)%>%
summarize(n=n())%>%
ungroup()%>%
mutate(pct = n/sum(n))
pct_exam
```
#Explore Boxplots of all numeric and barchart for categorical
```{r, message=FALSE, warning=FALSE}
unidata$churn <- as.factor(unidata$churn)
unidata$phone_model <- as.factor(unidata$phone_model)
unidata$partner <- as.factor(unidata$partner)
unidata$phone_service <- as.factor(unidata$phone_service)
unidata$phone_area_code <- as.factor(unidata$phone_area_code)
unidata$streaming_plan <- as.factor(unidata$streaming_plan)
unidata$multiple_lines <- as.factor(unidata$multiple_lines)
unidata$device_protection <- as.factor(unidata$device_protection)
unidata$billing_state <- as.factor(unidata$billing_state)
unidata$paperless_billing <- as.factor(unidata$paperless_billing)
unidata$payment_method <- as.factor(unidata$payment_method)
unidata$network_speed <- as.factor(unidata$network_speed)
unidata$mobile_hotspot <- as.factor(unidata$mobile_hotspot)
unidata$wifi_calling_text <- as.factor(unidata$wifi_calling_text)
unidata$online_backup <- as.factor(unidata$online_backup)
unidata$gender <- as.factor(unidata$gender)
unidata$contract_code <- as.factor(unidata$contract_code)
unidata$currency_code <- as.factor(unidata$currency_code)
unidata$maling_code <- as.factor(unidata$maling_code)
unidata$email_domain<- as.factor(unidata$email_domain)
```
#commercial household income,campus visit count,self-initiated contact count,total contact count, excurricular interest, mail qualify score, attendence to campus event
#set seed for datasets
```{r, warning=FALSE}
set.seed(999)
uni_split <- initial_split(unidata, prop = 0.7)
uni_train <- training(uni_split)
uni_test <- testing(uni_split)
sprintf("Train PCT : %1.2f%%", nrow(uni_train)/ nrow(unidata) * 100)
sprintf("Test PCT : %1.2f%%", nrow(uni_test)/ nrow(unidata) * 100)
```
#2. Recipe
#Drop all rejected variables from model and some variables with more than 20% missing values
```{r, warning=FALSE}
uni_recipe <- recipe(churn~ email_domain+ monthly_minutes + total_billed+ customer_service_calls+ mobile_hotspot+ late_payments+
multiple_lines+ currency_code+ phone_model+ streaming_minutes + prev_balance+ payment_method+ phone_area_code + paperless_billing+ number_phones+ streaming_plan,
data= uni_train)%>%
step_impute_median(all_numeric_predictors()) %>%
step_unknown(all_nominal_predictors()) %>%
step_scale(all_numeric_predictors()) %>%
step_dummy(all_nominal_predictors())%>%
prep()
uni_recipe
```
#apply recipe to two data sets
```{r, warning=FALSE}
bake_train <- bake(uni_recipe, new_data = uni_train)
skim(bake_train)
bake_test <- bake(uni_recipe, new_data = uni_test)
skim(bake_test)
```
```{r, warning=FALSE}
logistic_uni <-logistic_reg(mode = "classification") %>%
set_engine("glm") %>%
fit(churn ~ ., data = bake_train)
tidy(logistic_uni) %>%
mutate_at(c("estimate", "std.error", "statistic", "p.value"),round, 4)
```
```{r, warning=FALSE}
# -- training
predict(logistic_uni, bake_train, type = "prob") %>%
bind_cols(.,predict(logistic_uni, bake_train)) %>%
bind_cols(.,bake_train) -> scored_train_uni
head(scored_train_uni)
# -- testing
predict(logistic_uni, bake_test, type = "prob") %>%
bind_cols(.,predict(logistic_uni, bake_test)) %>%
bind_cols(.,bake_test) -> scored_test_uni
head(scored_test_uni)
```
```{r, warning=FALSE}
options(yardstick.event_first=FALSE)
scored_train_uni %>%
metrics(churn, .pred_1, estimate = .pred_class) %>%
mutate(part="training") %>%
bind_rows(scored_test_uni %>%
metrics(churn, .pred_1, estimate = .pred_class) %>%
mutate(part="testing")
)
# -- Variable Importance top 10 features
logistic_uni %>%
vip(num_features = 50)
# -- ROC Charts
scored_train_uni %>%
mutate(model = "train") %>%
bind_rows(scored_test_uni %>%
mutate(model="test")) %>%
group_by(model) %>%
roc_curve(churn, .pred_1) %>%
autoplot()
# -- Confustion Matricies
scored_train_uni %>%
conf_mat(churn, .pred_class) %>%
autoplot( type = "heatmap") +
labs(title="Train Confusion Matrix")
scored_test_uni %>%
conf_mat(churn, .pred_class) %>%
autoplot( type = "heatmap") +
labs(title="Test Confusion Matrix")
```
```{r, warning=FALSE}
steplog <- glm(churn ~ ., data = bake_train, family=binomial(link="logit"))
step <- stepAIC(steplog, direction="both")
summary(step)
```
```{r, warning=FALSE}
## -- Use tidymodel framework to fit and evaulate reduced model
uni_steprecipe <- recipe(enroll ~ campus_visit+premiere+stuemail+instate+interest+init_span+int1rat+int2rat+hscrat, data = uni_train) %>%
prep()
uni_steprecipe
# -- apply new recipe
bake_steptrain <- bake(uni_steprecipe, new_data = uni_train)
bake_steptest <- bake(uni_steprecipe, new_data = uni_test)
logistic_step1 <-logistic_reg(mode = "classification") %>%
set_engine("glm") %>%
fit(enroll ~ ., data = bake_steptrain)
## -- check out your parameter estimates ...
tidy(logistic_step1) %>%
mutate_at(c("estimate", "std.error", "statistic", "p.value"),round, 4)
```
```{r, warning=FALSE}
# -- training predictions from stepwise model
predict(logistic_step1, bake_steptrain, type = "prob") %>%
bind_cols(.,predict(logistic_step1, bake_steptrain)) %>%
bind_cols(.,bake_steptrain) -> scored_train_step1
head(scored_train_step1)
# -- testing predictions from stepwise model
predict(logistic_step1, bake_steptest , type = "prob") %>%
bind_cols(.,predict(logistic_step1, bake_steptest )) %>%
bind_cols(.,bake_steptest ) -> scored_test_step1
head(scored_test_step1)
```
```{r, warning=FALSE}
# -- Evaluate Stepwise Model
# -- AUC: Train and Test
options(yardstick.event_first=FALSE)
scored_train_step1 %>%
metrics(enroll, .pred_1, estimate = .pred_class) %>%
mutate(part="training") %>%
bind_rows( scored_test_step1 %>%
metrics(enroll, .pred_1, estimate = .pred_class) %>%
mutate(part="testing")
)
# -- ROC Charts
scored_train_step1 %>%
mutate(model = "train") %>%
bind_rows(scored_test_step1 %>%
mutate(model="test")) %>%
group_by(model) %>%
roc_curve(enroll, .pred_1) %>%
autoplot()
```
```{r}
# -- Confustion Matricies
scored_train_step1 %>%
conf_mat(enroll, .pred_class) %>%
autoplot( type = "heatmap") +
labs(title="Train Confusion Matrix")
scored_test_step1 %>%
conf_mat(enroll, .pred_class) %>%
autoplot( type = "heatmap") +
labs(title="Test Confusion Matrix")
```
```{r, warning=FALSE}
uni_step_2_recipe <- recipe(enroll ~ campus_visit+premiere+interest+instate+init_span+int1rat+int2rat+hscrat, data = uni_train) %>%
prep()
uni_step_2_recipe
# -- apply new recipe
bake_steptrain_2 <- bake(uni_step_2_recipe, new_data = uni_train)
bake_steptest_2 <- bake(uni_step_2_recipe, new_data = uni_test)
logistic_step2 <-logistic_reg(mode = "classification") %>%
set_engine("glm") %>%
fit(enroll ~ ., data = bake_steptrain_2)
## -- check out your parameter estimates ...
tidy(logistic_step2) %>%
mutate_at(c("estimate", "std.error", "statistic", "p.value"),round, 4)
```
```{r, warning=FALSE}
predict(logistic_step2, bake_steptrain_2, type = "prob") %>%
bind_cols(.,predict(logistic_step2, bake_steptrain_2)) %>%
bind_cols(.,bake_steptrain_2) -> scored_train_step2
head(scored_train_step2)
predict(logistic_step2, bake_steptest_2 , type = "prob") %>%
bind_cols(.,predict(logistic_step2, bake_steptest_2 )) %>%
bind_cols(.,bake_steptest_2 ) -> scored_test_step2
head(scored_test_step2)
```
```{r, warning=FALSE}
# -- Evaluate Model
options(yardstick.event_first=FALSE)
scored_train_step2 %>%
metrics(enroll, .pred_1, estimate = .pred_class) %>%
mutate(part="training") %>%
bind_rows( scored_test_step2 %>%
metrics(enroll, .pred_1, estimate = .pred_class) %>%
mutate(part="testing")
)
# -- ROC Charts
scored_train_step2 %>%
mutate(model = "train") %>%
bind_rows(scored_test_step2 %>%
mutate(model="test")) %>%
group_by(model) %>%
roc_curve(enroll, .pred_1) %>%
autoplot()
```
```{r}
# -- Confusion Matrices
scored_train_step2 %>%
conf_mat(enroll, .pred_class) %>%
autoplot( type = "heatmap") +
labs(title="Train Confusion Matrix")
scored_test_step2 %>%
conf_mat(enroll, .pred_class) %>%
autoplot( type = "heatmap") +
labs(title="Test Confusion Matrix")
```