-
Notifications
You must be signed in to change notification settings - Fork 1
/
Code and Technical Analysis.Rmd
1432 lines (971 loc) · 37.7 KB
/
Code and Technical Analysis.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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
# This is the YAML header/metadata for the document
title: "Customer Churn Analysis code & Technical Analysis"
author: "Pranav Chandaliya, Vaishnavi Nagarajaiah, Sunisha Harish, Kunal Inglunkar, Pooja Chandrashekara"
output:
html_document:
code_folding: hide
number_sections: true
toc: yes
toc_depth: 3
toc_float: yes
pdf_document:
toc: yes
toc_depth: '3'
---
```{r init, include=F}
# The package "ezids" (EZ Intro to Data Science) includes a lot of the helper functions we developed for the course.
# Some of the frequently used functions are loadPkg(), xkabledply(), xkablesummary(), uzscale(), etc.
# Once installed, load the library.
library(ezids)
```
```{r setup, include=FALSE}
# some of common options (and the defaults) are:
# include=T, eval=T, echo=T, results='hide'/'asis'/'markup',..., collapse=F, warning=T, message=T, error=T, cache=T, fig.width=6, fig.height=4, fig.dim=c(6,4) #inches, fig.align='left'/'center','right',
# knitr::opts_chunk$set(warning = F, results = "markup", message = F)
knitr::opts_chunk$set(warning = F, results = "hide", message = F)
# knitr::opts_chunk$set(include = F)
# knitr::opts_chunk$set(echo = TRUE)
options(scientific=T, digits = 3)
# options(scipen=9, digits = 3)
# ‘scipen’: integer. A penalty to be applied when deciding to print numeric values in fixed or exponential notation. Positive values bias towards fixed and negative towards scientific notation: fixed notation will be preferred unless it is more than ‘scipen’ digits wider.
# use scipen=999 to prevent scientific notation at all times
```
Loading our dataset into a dataframe.
```{r,results="show"}
Telecom_Data <- data.frame(read.csv("Telecom Data.csv"))
ncol(Telecom_Data)
nrow(Telecom_Data)
```
There are total 58 Columns and 51,047 Rows.
Let us print the structure of our data.
```{r,results=T}
str(Telecom_Data)
```
Here we are converting a few columns to factor data type.
```{r,results="show"}
#Telecom_Data$Churn <- factor(Telecom_Data$Churn)
Telecom_Data$CreditRating <- factor(Telecom_Data$CreditRating)
Telecom_Data$Occupation <- factor(Telecom_Data$Occupation)
```
Getting the summary of our data.
```{r,results="show"}
#xkablesummary(Telecom_Data)
```
Let's check for null values
```{r,results="show"}
library(dplyr)
library(tidyr)
## Checking the null values in the dataset
#summary(Telecom_Data)
#is.null(Telecom_Data)
null_values<-sapply(Telecom_Data, function(x) sum(is.na(x)))
null_values
```
Few columns have null values but the count is less,
Lets create new variables which will help with our analysis.
```{r, results= "show"}
##Creation of new variables for our analysis
Telecom_Data$perc_recurrent_charge <- (Telecom_Data$TotalRecurringCharge /Telecom_Data$MonthlyRevenue) * 100
Telecom_Data$perc_overage_minute <- (Telecom_Data$OverageMinutes / Telecom_Data$MonthlyMinutes) * 100
str(Telecom_Data)
```
Let us check the count for churn.
```{r,results="show"}
## Getting Churn counts
churn_counts<- dplyr::count(Telecom_Data,Churn , sort = TRUE)
```
We are plotting a bar chart here to see the churn distribution.
```{r,results="show"}
library("ggplot2")
ggplot(data = churn_counts, aes(x = "", y = n, fill = Churn)) +
geom_bar(stat = "identity") +
coord_polar("y")
```
Trying out plotly for pie chart for more interactive graphs.
```{r,results="show"}
library(plotly)
colors <- c('rgb(211,94,96)', 'rgb(128,133,133)', 'rgb(144,103,167)', 'rgb(171,104,87)', 'rgb(114,147,203)')
fig <- plot_ly(type='pie', labels=churn_counts$Churn, values=churn_counts$n,
textinfo='label+percent',
insidetextorientation='radial',marker = list(colors = colors,
line = list(color = '#FFFFFF', width = 1)))
fig
```
Creating a subset for churned and retained customers for deep dive analysis and checking the summary of the divided data to analyze the trend.
```{r,results="show"}
library(dplyr)
Telecom_Data_yes = filter(Telecom_Data, Churn == "Yes")
Telecom_Data_no = filter(Telecom_Data, Churn == "No")
summary(Telecom_Data_yes)
summary(Telecom_Data_no)
```
Showing the classification of our data into major categories.
```{r,results="show"}
feat_typ_counts <- data.frame(read.csv("Feat_type_counts.csv"))
#install.packages("plotrix")
library(plotrix)
library("ggplot2")
#pie(feat_typ_counts$Counts, feat_typ_counts$Variable.Type)
piepercent<- round(100 * feat_typ_counts$Counts / sum(feat_typ_counts$Counts), 1)
feat_typ_counts$fraction <- feat_typ_counts$Counts / sum(feat_typ_counts$Counts)
# Compute the cumulative percentages (top of each rectangle)
feat_typ_counts$ymax <- cumsum(feat_typ_counts$fraction)
# Compute the bottom of each rectangle
feat_typ_counts$ymin <- c(0, head(feat_typ_counts$ymax, n=-1))
# Compute label position
feat_typ_counts$labelPosition <- (feat_typ_counts$ymax + feat_typ_counts$ymin) / 2
# Compute a good label
feat_typ_counts$label <- paste0(feat_typ_counts$Variable.Type, "\n Count: ", feat_typ_counts$Counts)
ggplot(feat_typ_counts, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=Variable.Type)) +
geom_rect() +
geom_label( x=3.5, aes(y=labelPosition, label=label), size=2) +
scale_fill_brewer(palette=4) +
coord_polar(theta="y") +
xlim(c(2, 4)) +
theme_void() +
theme(legend.position = "none")
```
##Plotting a Box plot of Monthly Minutes
```{r,results="show"}
boxplot(Telecom_Data$MonthlyMinutes,
main = "Monthly Minutes of Customers",
xlab = "Monthly Min",
ylab = "Frequency",
col = "orange",
border = "brown",
horizontal = TRUE,
notch = TRUE
)
```
##Current Headset use in days
```{r,results="show"}
plot_ly(Telecom_Data, y= Telecom_Data$CurrentEquipmentDays, color = Telecom_Data$Churn, type = "box") %>%
layout(boxmode = "group",
xaxis = list(title=''),
yaxis = list(title='Frequency'))
```
##Boxplot of Total Recurring Charge
```{r,results="show"}
plot_ly(Telecom_Data, y= Telecom_Data$TotalRecurringCharge, color = Telecom_Data$Churn, type = "box") %>%
layout(boxmode = "group",
xaxis = list(title=''),
yaxis = list(title='Frequency'))
```
##Box plot of Month in Service
```{r,results="show"}
plot_ly(Telecom_Data, y= Telecom_Data$MonthsInService, color = Telecom_Data$Churn, type = "box") %>%
layout(boxmode = "group",
xaxis = list(title=''),
yaxis = list(title='Frequency'))
```
##Box plot of the Percent change in recurrent charge
```{r,results="show"}
plot_ly(Telecom_Data, y= Telecom_Data$perc_recurrent_charge, color = Telecom_Data$Churn, type = "box") %>%
layout(boxmode = "group",
xaxis = list(title=''),
yaxis = list(title='Frequency'))
```
##Box plot of Percent change in Minutes
```{r,results="show"}
plot_ly(Telecom_Data, y= Telecom_Data$PercChangeMinutes, color = Telecom_Data$Churn, type = "box") %>%
layout(boxmode = "group",
xaxis = list(title=''),
yaxis = list(title='Frequency'))
```
##Box plot of Percent change in Revenues
```{r,results="show"}
plot_ly(Telecom_Data, y= Telecom_Data$PercChangeRevenues, color = Telecom_Data$Churn, type = "box") %>%
layout(boxmode = "group",
xaxis = list(title=''),
yaxis = list(title='Frequency'))
```
##Distribution of the Montly Revenue
```{r,results="show"}
library(ggplot2)
library(plotly)
set.seed(1)
gg <- ggplot(Telecom_Data,aes(x = MonthlyRevenue, color = 'density')) +
geom_histogram(aes(y = ..density..), bins = 7, fill = '#67B7D1', alpha = 0.5) +
geom_density(color = '#67B7D1') +
geom_rug(color = '#67B7D1') +
ylab("") +
xlab("") + theme(legend.title=element_blank()) +
scale_color_manual(values = c('density' = '#67B7D1'))
ggplotly(gg)%>%
layout(plot_bgcolor='#e5ecf6',
xaxis = list(
title='Time',
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(
title='Monthly Revenue',
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'))
```
##Distribution of Monthly Minutes
```{r,results="show"}
library(ggplot2)
library(plotly)
set.seed(1)
gg <- ggplot(Telecom_Data,aes(x = MonthlyMinutes, color = 'density')) +
geom_histogram(aes(y = ..density..), bins = 7, fill = '#67B7D1', alpha = 0.5) +
geom_density(color = '#67B7D1') +
geom_rug(color = '#67B7D1') +
ylab("") +
xlab("") + theme(legend.title=element_blank()) +
scale_color_manual(values = c('density' = '#67B7D1'))
ggplotly(gg)%>%
layout(plot_bgcolor='#e5ecf6',
xaxis = list(
title='Monthly Minutes ',
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(
title='Frequency',
zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'))
```
```{r,results="show"}
qqnorm(Telecom_Data$MonthlyMinutes) # QQplot
qqline(Telecom_Data$MonthlyMinutes, col = "red")
#install.packages("car")
#library("car")
#qqPlot(Telecom_Data$MonthlyMinutes)
```
```{r,results="show"}
library("plotly")
#plot_ly(Telecom_Data, y= Telecom_Data$AgeHH1, color = Telecom_Data$Churn, type = "box")
#layout(boxmode = "group",
# xaxis = list(title=''),
# yaxis = list(title='Frequency'))
```
Checking how Non US travel affects the churn rate
```{r,results="show"}
churn_count<-nrow(Telecom_Data$Churn)
ggplot(Telecom_Data,aes(x = NonUSTravel,fill=Churn )) +
geom_bar( position = "stack")+ggtitle("How Travel affects churn")
```
Do the number of dropped call have affect on churn ?
```{r}
ggplot(Telecom_Data, aes(x=DroppedCalls, fill=Churn)) + geom_histogram(position='identity',alpha=0.6)
```
Income group of the customers
```{r}
ggplot(Telecom_Data,aes(x=IncomeGroup, fill=Churn))+geom_histogram(position='identity',alpha=0.6)
```
How many customer are opting out of mailing list
```{r}
ggplot(Telecom_Data,aes(x=OptOutMailings,fill=Churn))+geom_bar(position='identity',alpha=0.6)
```
Histogram for representing age of customers in Telecom Data
```{r, echo=T, results=T}
library(plotly)
ggplot(Telecom_Data, aes(x=AgeHH1))+ geom_histogram(color="aquamarine4",fill = "aquamarine3",alpha=0.6, bins=30) + labs(x="Age of Customers", y="Frequency",
title="Histogram of Customer Age") + theme_classic()
```
Filtering age of primary users
```{r, echo=T, results=T}
library(dplyr)
AgeFiltered = filter(Telecom_Data, AgeHH1== 0)
nrow(AgeFiltered)
(13917/nrow(Telecom_Data))*100
```
Boxplot representing customer age group in Telecom Data using ggplot
```{r, echo=T, results=T}
library(ggplot2)
ggplot(Telecom_Data, aes(y=AgeHH1)) + geom_boxplot( colour="maroon", fill="aquamarine3",alpha=0.6) + ggtitle("Boxplot of Customer Age group`") + labs(x="Age of Customers", y=" Frequency") + theme_classic()
```
Boxplot representing Customer age group in Telecom Data using plotly
```{r, echo=T, results=T}
library(plotly)
plot_ly(Telecom_Data, y= Telecom_Data$AgeHH1,type = "box", color = Telecom_Data$Churn) %>%
layout(boxmode = "group",
xaxis = list(title=''),
yaxis = list(title='Frequency'))
```
## Does credit rating have an impact on churn rate?
Boxplot for Credit Rating using ggplot
```{r, echo=T, results=T}
library(ggplot2)
ggplot(Telecom_Data, aes(y=CreditRating)) + geom_boxplot( colour="orange", fill="black") + ggtitle("Credit Rating using `ggplot`")
```
Creating a subset for Churned and Retained customers data
```{r, echo=T, results=T}
Churned <- subset(Telecom_Data, Churn=="Yes")
Retained <- subset(Telecom_Data, Churn=="No")
```
Frequency barplot for Credit Rating of Churned customers using ggplot
```{r, echo=T, results=T}
library(ggplot2)
ggplot(Churned, aes(x = CreditRating)) + geom_bar(col="black", fill="red", alpha=0.4) + ggtitle("Credit Rating for Churned Telecom Data") + labs(x="Credit Rating (x-axis)", y=" Count (y-axis)") + ylim(0,15000) + theme_classic()
```
Frequency barplot for Credit Rating of Retained customers using ggplot
```{r, echo=T, results=T}
library(ggplot2)
ggplot(Retained, aes(x = CreditRating)) + geom_bar(col="black", fill="aquamarine3", alpha=0.6) + ggtitle("Credit Rating for Retained Telecom Data") + labs(x="Credit Rating (x-axis)", y=" Count (y-axis)")+ ylim(0,15000) + theme_classic()
```
## At what duration is the churn rate high for the customers?
```{r Histogram,results=T}
# Histogram for relationship between months in service and Churn
ggplot(Churned, aes(x=MonthsInService, fill=Churn)) + geom_histogram(position='identity',alpha=0.6,color='aquamarine4',fill='aquamarine3')+xlab("Service period for churned customers (In Months) ")+ylab("Frequency") + theme_classic()+ggtitle("Service Months Distribution for Churned customers")
Mean_MonthsInService=mean(Churned$MonthsInService)
print(paste("Mean of service months of the customer:",Mean_MonthsInService))
Median_MonthsInService=median(Churned$MonthsInService)
print(paste("Median of service months of the customer:",Median_MonthsInService))
```
## In what Prizm codes are the Churn Rates high?
```{r PrizmCode,results=T}
# Barplot for Prizm Code effect on Churn
ggplot(Telecom_Data, aes(x=PrizmCode, fill = Churn)) +geom_bar(position = "dodge2")+ggtitle("Churn distribution for Prizm code")
#install.packages("plotly")
library(plotly)
colors <- c('rgb(128,133,133)', 'rgb(144,103,167)', 'rgb(171,104,87)', 'rgb(114,147,203)')
fig <- plot_ly(type='pie', labels=Churned$PrizmCode, values=Churned$n,
textinfo='label+percent',
insidetextorientation='radial',marker = list(colors = "Set1"),
line = list(color = '#FFFFFF', width = 1))
fig
fig_1 <- plot_ly(type='pie', labels=Retained$PrizmCode, values=Retained$n,
textinfo='label+percent',
insidetextorientation='radial',marker = list(colors = "Set1"),
line = list(color = '#FFFFFF', width = 1))
fig_1
```
## Is occupation independent of churn ?
```{r Occupation,results=T}
#Frequency distribution of Occupation
ggplot(Telecom_Data,aes(x=Occupation)) + geom_bar(fill = "aquamarine3") + ggtitle("Frequency distribution of occupation")
#Creating a contingency table for Occupation and Churn
Occupation_Churn<-table(Telecom_Data$Occupation,Telecom_Data$Churn)
str(Occupation_Churn)
#Performing Chi Square Test to check if occupation is independent of churn
chisq_test=chisq.test(Occupation_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
p value is greater than 0.05. Hence we will be accepting the null hypothesis, H0.
Therefore we can say that occupation is independent of churn.
## Does the monthly revenue average differ for different occupations?
H0: Average monthly revenue is similar across different occupations
H1: Average monthly revenue is different across different occupations
```{r}
## Anova Results
one.way <- aov(MonthlyMinutes ~ Occupation, data = Telecom_Data)
#summary(one.way)
xkabledply(one.way, title = "ANOVA result summary")
```
P values < 0.05, Which means null hypothesis is rejected
Monthly revenue average differ for occupations
**Bivariate analysis of variables**
```{r Bivariate-Analysis, echo=T, results=T}
Telecom_Data <- read.csv("Telecom Data.csv")
```
Correlation Analysis of Monthly Revenue and Overage Minutes for churned customers.
```{r Correlation Analysis, echo=T, results=T}
churndata <- subset(Telecom_Data, Telecom_Data$Churn == "Yes")
sum(is.na(churndata$MonthlyRevenue))
sum(is.na(churndata$OverageMinutes))
```
We can see there are 70 NA values in monthly revenue and overageminutes columns when churn is yes
Lets remove those NA values
```{r, echo=T, results=T}
churndata2 <- na.omit(churndata)
sum(is.na(churndata2$MonthlyRevenue))
sum(is.na(churndata2$OverageMinutes))
```
We have removed all the null values.
```{r,echo=T,results=T}
cor.test(churndata2$MonthlyRevenue, churndata2$OverageMinutes, method='spearman')
```
The output is 0.5911 which is approximately 0.6.
As the sign is positive, we can say that monthly revenue and overage minutes vary postively when there is churn. That is, the as the overage minutes increases, monthly revenue also increases.
```{r, echo=T, results=T}
#Plot with statistical results
library(ggplot2)
ggplot(data = churndata2) +
geom_smooth(mapping = aes(x = OverageMinutes, y = MonthlyRevenue, color="Brown")) + theme_classic()+
xlab("Overage minutes used by the customer") + ylab("Monthly revenue of the Telecom company ")
```
From the graph we can see that as Overage Minutes increased, Monthly revenue also increased.
```{r, echo=T, results=T}
#install.packages("ggpubr")
library(ggpubr)
ggscatter(data = churndata2, x = "OverageMinutes", y = "MonthlyRevenue",
conf.int = TRUE, color="brown", xlab="Overage Minutes used by customer", ylab="Monthly Revenue of the Telecom company", title="Scatter plot of Overage Minutes vs Monthly Revenue for churned customers")
```
The scatter graph in the aforementioned section illustrates the positive correlation between customer overage minutes consumed and the telecom sector's monthly income for customers who churn.
Now lets plot when there is no churn, that is when there is retention of customers.
```{r, echo=T, results=T}
retentiondata <- subset(Telecom_Data, Telecom_Data$Churn == "No")
sum(is.na(retentiondata$MonthlyRevenue))
sum(is.na(retentiondata$OverageMinutes))
```
We can see there are 86 NA values in monthly revenue and overageminutes columns when there is no churn.
Lets remove those NA values
```{r, echo=T, results=T}
retentiondata2 <- na.omit(retentiondata)
sum(is.na(retentiondata2$MonthlyRevenue))
sum(is.na(retentiondata2$OverageMinutes))
```
We have removed all the null values.
Now lets do correlation
```{r, echo=T, results=T}
cor.test(retentiondata2$MonthlyRevenue, retentiondata2$OverageMinutes, method='spearman')
```
Even when there is retention the output is 0.56, we can say the that Monthly revenue varies postively as overage minutes varies.
```{r, echo=T, results=T}
#Plot with statistical results
library(ggplot2)
ggplot(data = retentiondata2)+
geom_smooth(mapping = aes(x = OverageMinutes, y = MonthlyRevenue), color="dark green" ) + theme_classic() + xlab("Overage minutes used by the customer") + ylab("Monthly revenue of the Telecom company ")
```
From the graph we can see that as Overage Minutes increases, Monthly revenue also increases.
```{r, echo=T, results=T}
#install.packages("ggpubr")
library(ggpubr)
ggscatter(data = retentiondata2, x = "OverageMinutes", y = "MonthlyRevenue",
conf.int = TRUE, color="dark green", xlab="Overage Minutes used by customer", ylab="Monthly Revenue of the Telecom company", title="Scatter plot of Overage Minutes vs Monthly Revenue for retention customers")
```
The graph shows that even for customers who are still with the company, monthly revenue rises as Overage Minutes rise.
Also, we can observe that outliers are more for churned customers compared to non-churn customers
```{r, echo=T, results=T}
datacorr <- Telecom_Data[ , c("MonthlyRevenue","MonthlyMinutes", "TotalRecurringCharge","CustomerCareCalls","ThreewayCalls","ReceivedCalls","OutboundCalls","MonthsInService","HandsetPrice","CreditRating")]
sum(is.na(datacorr))
```
There are 468 NA values in the subsetted dataset.
```{r, echo=T, results=T}
datacorr2 <- na.omit(datacorr)
nrow(datacorr2)
sum(is.na(datacorr2))
```
After removing the NA values, we are left out with 50891 rows.
Next, we have used few numerical factors, including months of service, outbound calls, received calls, three-way calls, customer care calls, total recurring calls, and monthly minutes, and we've conducted correlation statistical studies to look at how they relate to one another using correlation matrix.
```{r, echo=T, results=T}
# load package
#install.packages("ggstatsplot")
#install.packages("ggcorrplot")
library(ggstatsplot)
library(ggcorrplot)
library(corrplot)
# correlogram
ggstatsplot::ggcorrmat(
data = datacorr2,
type = "nonparametric", # parametric for Pearson, nonparametric for Spearman's correlation
colors = c("darkred", "white", "steelblue"),
title = "Correlation matrix"
)
```
The above correlation graph leads us to the following conclusions:
1. Monthly minutes consumed by the client and monthly revenue of the telecom operator are highly associated.
2. There is a significant probability that customers will receive calls from customer service.
3. The number of months a consumer stays with a service won't significantly alter its monthly revenue.
4. The customer service calls made by the telecom sector have no impact on the number of months the clients have left on their subscriptions.
## Data Preprocessing for Modeling
In the EDA, We observed Incorrect entries in agehh1 and agehh2 which had zero values that need to be imputed, We observed the distribution of it and decided to go with median imputation as age has skewness.
```{r, echo=T, results=T}
library(ggplot2)
ggplot(Telecom_Data, aes(x = AgeHH1)) +
geom_histogram(aes(y = ..density..),
colour = 1, fill = "cyan")
ggplot(Telecom_Data, aes(x = AgeHH2)) +
geom_histogram(aes(y = ..density..),
colour = 1, fill = "cyan")
Telecom_Data$AgeHH1<-replace(Telecom_Data$AgeHH1,Telecom_Data$AgeHH1 <1 ,NA)
Telecom_Data$AgeHH2<-replace(Telecom_Data$AgeHH2,Telecom_Data$AgeHH2 <1 ,NA)
Telecom_Data$AgeHH1[is.na(Telecom_Data$AgeHH1)]<- median(Telecom_Data$AgeHH1,na.rm = TRUE)
Telecom_Data$AgeHH2[is.na(Telecom_Data$AgeHH2)]<- median(Telecom_Data$AgeHH2,na.rm = TRUE)
library(ggplot2)
ggplot(Telecom_Data, aes(x = AgeHH1)) +
geom_histogram(aes(y = ..density..),
colour = 1, fill = "cyan")
ggplot(Telecom_Data, aes(x = AgeHH2)) +
geom_histogram(aes(y = ..density..),
colour = 1, fill = "cyan")
```
## checking inactive customers and removing them
```{r, echo=T, results=T}
library(plotly)
fig <- plot_ly(y = Telecom_Data$MonthlyMinutes, type = "box", quartilemethod="linear") # or "inclusive", or "linear" by default
fig
## checking inactive customers
nrow(subset(Telecom_Data, MonthlyRevenue <= 0))
## checking inactive customers
nrow(subset(Telecom_Data, MonthlyMinutes <= 0))
## Removing inactive customers (outliers)
Telecom_Data<-subset(Telecom_Data, MonthlyRevenue > 0)
Telecom_Data <-subset(Telecom_Data, MonthlyMinutes > 0)
library(plotly)
fig <- plot_ly(y = Telecom_Data$MonthlyMinutes, type = "box", quartilemethod="exclusive") # or "inclusive", or "linear" by default
fig
nrow(Telecom_Data)
```
## Checking Null Values :
```{r, echo=T, results=T}
library(tidyverse)
map(Telecom_Data, ~sum(is.na(.)))
```
### Feature selection
#### Using Chi square test to select categorical variables
#Creating a contingency table for IncomeGroup and Churn
```{r,results=T}
IncomeGroup_Churn<-table(Telecom_Data$IncomeGroup,Telecom_Data$Churn)
str(IncomeGroup_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(IncomeGroup_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for Service Area and Churn
```{r,results=T}
Service_Area_Churn<-table(Telecom_Data$ServiceArea,Telecom_Data$Churn)
str(Service_Area_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(Service_Area_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for ChildrenInHH and Churn
```{r,results=T}
ChildrenInHH_Churn<-table(Telecom_Data$ChildrenInHH,Telecom_Data$Churn)
str(ChildrenInHH_Churn)
```
```{r,results=T}
chisq_test=chisq.test(ChildrenInHH_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for HandsetWebCapable and Churn
```{r ,results=T}
HandsetWebCapable_Churn<-table(Telecom_Data$HandsetWebCapable,Telecom_Data$Churn)
str(HandsetWebCapable_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(HandsetWebCapable_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for TruckOwner and Churn
```{r ,results=T}
TruckOwner_Churn<-table(Telecom_Data$TruckOwner,Telecom_Data$Churn)
str(TruckOwner_Churn)
```
```{r,results=T}
chisq_test=chisq.test(TruckOwner_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for RVOwner and Churn
```{r ,results=T}
RVOwner_Churn<-table(Telecom_Data$RVOwner,Telecom_Data$Churn)
str(RVOwner_Churn)
```
```{r RVOwner,results=T}
chisq_test=chisq.test(RVOwner_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for Homeownership and Churn
```{r ,results=T}
Homeownership_Churn<-table(Telecom_Data$Homeownership,Telecom_Data$Churn)
str(Homeownership_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(Homeownership_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for BuysViaMailOrder and Churn
```{r ,results=T}
BuysViaMailOrder_Churn<-table(Telecom_Data$BuysViaMailOrder,Telecom_Data$Churn)
str(BuysViaMailOrder_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(BuysViaMailOrder_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for RespondsToMailOffers and Churn
```{r ,results=T}
RespondsToMailOffers_Churn<-table(Telecom_Data$RespondsToMailOffers,Telecom_Data$Churn)
str(RespondsToMailOffers_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(RespondsToMailOffers_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for OptOutMailings and Churn
```{r ,results=T}
OptOutMailings_Churn<-table(Telecom_Data$OptOutMailings,Telecom_Data$Churn)
str(OptOutMailings_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(OptOutMailings_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for NonUSTravel and Churn
```{r ,results=T}
NonUSTravel_Churn<-table(Telecom_Data$NonUSTravel,Telecom_Data$Churn)
str(NonUSTravel_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(NonUSTravel_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for OwnsComputer and Churn
```{r ,results=T}
OwnsComputer_Churn<-table(Telecom_Data$OwnsComputer,Telecom_Data$Churn)
str(OwnsComputer_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(OwnsComputer_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for HasCreditCard and Churn
```{r ,results=T}
HasCreditCard_Churn<-table(Telecom_Data$HasCreditCard,Telecom_Data$Churn)
str(HasCreditCard_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(HasCreditCard_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for NewCellphoneUser and Churn
```{r ,results=T}
NewCellphoneUser_Churn<-table(Telecom_Data$NewCellphoneUser,Telecom_Data$Churn)
str(HasCreditCard_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(NewCellphoneUser_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for NotNewCellphoneUser and Churn
```{r ,results=T}
NotNewCellphoneUser_Churn<-table(Telecom_Data$NotNewCellphoneUser,Telecom_Data$Churn)
str(NotNewCellphoneUser_Churn)
```
```{r ,results=T}
chisq_test=chisq.test(NotNewCellphoneUser_Churn)
chisq_test
p_value=chisq_test$p.value
print(paste("The p value is:",p_value))
```
#Creating a contingency table for OwnsMotorcycle and Churn
```{r ,results=T}
OwnsMotorcycle_Churn<-table(Telecom_Data$OwnsMotorcycle,Telecom_Data$Churn)
str(OwnsMotorcycle_Churn)