R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(readr)
## Warning: package 'readr' was built under R version 4.2.3
projectDS <- read.csv("C:\\Users\\hussain7108\\Desktop\\churn (1).csv", stringsAsFactors = TRUE)
projectDS <- subset(projectDS, select = -c(Phone))
projectDS <- subset(projectDS, select = -c(State))

summary(projectDS)
##  Account.Length    Area.Code     Int.l.Plan VMail.Plan VMail.Message   
##  Min.   :  1.0   Min.   :408.0   no :3010   no :2411   Min.   : 0.000  
##  1st Qu.: 74.0   1st Qu.:408.0   yes: 323   yes: 922   1st Qu.: 0.000  
##  Median :101.0   Median :415.0                         Median : 0.000  
##  Mean   :101.1   Mean   :437.2                         Mean   : 8.099  
##  3rd Qu.:127.0   3rd Qu.:510.0                         3rd Qu.:20.000  
##  Max.   :243.0   Max.   :510.0                         Max.   :51.000  
##     Day.Mins       Day.Calls       Day.Charge       Eve.Mins    
##  Min.   :  0.0   Min.   :  0.0   Min.   : 0.00   Min.   :  0.0  
##  1st Qu.:143.7   1st Qu.: 87.0   1st Qu.:24.43   1st Qu.:166.6  
##  Median :179.4   Median :101.0   Median :30.50   Median :201.4  
##  Mean   :179.8   Mean   :100.4   Mean   :30.56   Mean   :201.0  
##  3rd Qu.:216.4   3rd Qu.:114.0   3rd Qu.:36.79   3rd Qu.:235.3  
##  Max.   :350.8   Max.   :165.0   Max.   :59.64   Max.   :363.7  
##    Eve.Calls       Eve.Charge      Night.Mins     Night.Calls   
##  Min.   :  0.0   Min.   : 0.00   Min.   : 23.2   Min.   : 33.0  
##  1st Qu.: 87.0   1st Qu.:14.16   1st Qu.:167.0   1st Qu.: 87.0  
##  Median :100.0   Median :17.12   Median :201.2   Median :100.0  
##  Mean   :100.1   Mean   :17.08   Mean   :200.9   Mean   :100.1  
##  3rd Qu.:114.0   3rd Qu.:20.00   3rd Qu.:235.3   3rd Qu.:113.0  
##  Max.   :170.0   Max.   :30.91   Max.   :395.0   Max.   :175.0  
##   Night.Charge      Intl.Mins       Intl.Calls      Intl.Charge   
##  Min.   : 1.040   Min.   : 0.00   Min.   : 0.000   Min.   :0.000  
##  1st Qu.: 7.520   1st Qu.: 8.50   1st Qu.: 3.000   1st Qu.:2.300  
##  Median : 9.050   Median :10.30   Median : 4.000   Median :2.780  
##  Mean   : 9.039   Mean   :10.24   Mean   : 4.479   Mean   :2.765  
##  3rd Qu.:10.590   3rd Qu.:12.10   3rd Qu.: 6.000   3rd Qu.:3.270  
##  Max.   :17.770   Max.   :20.00   Max.   :20.000   Max.   :5.400  
##  CustServ.Calls     Churn.    
##  Min.   :0.000   False.:2850  
##  1st Qu.:1.000   True. : 483  
##  Median :1.000                
##  Mean   :1.563                
##  3rd Qu.:2.000                
##  Max.   :9.000
# View the structure
str(projectDS)
## 'data.frame':    3333 obs. of  19 variables:
##  $ Account.Length: int  128 107 137 84 75 118 121 147 117 141 ...
##  $ Area.Code     : int  415 415 415 408 415 510 510 415 408 415 ...
##  $ Int.l.Plan    : Factor w/ 2 levels "no","yes": 1 1 1 2 2 2 1 2 1 2 ...
##  $ VMail.Plan    : Factor w/ 2 levels "no","yes": 2 2 1 1 1 1 2 1 1 2 ...
##  $ VMail.Message : int  25 26 0 0 0 0 24 0 0 37 ...
##  $ Day.Mins      : num  265 162 243 299 167 ...
##  $ Day.Calls     : int  110 123 114 71 113 98 88 79 97 84 ...
##  $ Day.Charge    : num  45.1 27.5 41.4 50.9 28.3 ...
##  $ Eve.Mins      : num  197.4 195.5 121.2 61.9 148.3 ...
##  $ Eve.Calls     : int  99 103 110 88 122 101 108 94 80 111 ...
##  $ Eve.Charge    : num  16.78 16.62 10.3 5.26 12.61 ...
##  $ Night.Mins    : num  245 254 163 197 187 ...
##  $ Night.Calls   : int  91 103 104 89 121 118 118 96 90 97 ...
##  $ Night.Charge  : num  11.01 11.45 7.32 8.86 8.41 ...
##  $ Intl.Mins     : num  10 13.7 12.2 6.6 10.1 6.3 7.5 7.1 8.7 11.2 ...
##  $ Intl.Calls    : int  3 3 5 7 3 6 7 6 4 5 ...
##  $ Intl.Charge   : num  2.7 3.7 3.29 1.78 2.73 1.7 2.03 1.92 2.35 3.02 ...
##  $ CustServ.Calls: int  1 1 0 2 3 0 3 0 1 0 ...
##  $ Churn.        : Factor w/ 2 levels "False.","True.": 1 1 1 1 1 1 1 1 1 1 ...
#display the dimensions of the dataset
dim(projectDS)
## [1] 3333   19
#list types for each attribute
sapply(projectDS, class)
## Account.Length      Area.Code     Int.l.Plan     VMail.Plan  VMail.Message 
##      "integer"      "integer"       "factor"       "factor"      "integer" 
##       Day.Mins      Day.Calls     Day.Charge       Eve.Mins      Eve.Calls 
##      "numeric"      "integer"      "numeric"      "numeric"      "integer" 
##     Eve.Charge     Night.Mins    Night.Calls   Night.Charge      Intl.Mins 
##      "numeric"      "numeric"      "integer"      "numeric"      "numeric" 
##     Intl.Calls    Intl.Charge CustServ.Calls         Churn. 
##      "integer"      "numeric"      "integer"       "factor"
summary(projectDS$Day.Calls) 
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0    87.0   101.0   100.4   114.0   165.0
summary(projectDS$Eve.Calls)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##     0.0    87.0   100.0   100.1   114.0   170.0
summary(projectDS$Night.Calls)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    33.0    87.0   100.0   100.1   113.0   175.0
summary(projectDS$CustServ.Calls)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   1.000   1.000   1.563   2.000   9.000
summary(projectDS$Intl.Charge)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   2.300   2.780   2.765   3.270   5.400
summary(projectDS$Day.Charge)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00   24.43   30.50   30.56   36.79   59.64
summary(projectDS$Eve.Charge)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##    0.00   14.16   17.12   17.08   20.00   30.91
summary(projectDS$Night.Charge)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   1.040   7.520   9.050   9.039  10.590  17.770
#histogram of Charges to understand in more details
hist(projectDS$CustServ.Calls)

hist(projectDS$Day.Calls)

hist(projectDS$Eve.Calls)

hist(projectDS$Night.Calls)

hist(projectDS$Day.Mins)

hist(projectDS$Eve.Mins)

hist(projectDS$Night.Mins)

hist(projectDS$Day.Charge)

hist(projectDS$Eve.Charge)

hist(projectDS$Night.Charge)

hist(projectDS$Intl.Charge)

#check proportion of factor variables
table(projectDS$CustServ.Calls)
## 
##    0    1    2    3    4    5    6    7    8    9 
##  697 1181  759  429  166   66   22    9    2    2
table(projectDS$Int.l.Plan)
## 
##   no  yes 
## 3010  323
table(projectDS$VMail.Plan)
## 
##   no  yes 
## 2411  922
table(projectDS$Churn.)
## 
## False.  True. 
##   2850    483
#Co-relation
cor(projectDS[c('CustServ.Calls','Day.Charge','Eve.Charge','Night.Charge','Intl.Charge')])
##                CustServ.Calls   Day.Charge   Eve.Charge Night.Charge
## CustServ.Calls    1.000000000 -0.013426969 -0.012987407 -0.009276954
## Day.Charge       -0.013426969  1.000000000  0.007036131  0.004300861
## Eve.Charge       -0.012987407  0.007036131  1.000000000 -0.012601142
## Night.Charge     -0.009276954  0.004300861 -0.012601142  1.000000000
## Intl.Charge      -0.009674732 -0.010094257 -0.011074499 -0.015186139
##                 Intl.Charge
## CustServ.Calls -0.009674732
## Day.Charge     -0.010094257
## Eve.Charge     -0.011074499
## Night.Charge   -0.015186139
## Intl.Charge     1.000000000
#Scatter plot
pairs(projectDS[c('CustServ.Calls','Day.Charge','Eve.Charge','Night.Charge','Intl.Charge')])
library(psych)
## Warning: package 'psych' was built under R version 4.2.3

  ins_model <- lm(CustServ.Calls ~ Day.Charge + Eve.Charge + Night.Charge + Intl.Charge + Int.l.Plan + VMail.Plan+Churn.,data=projectDS)
    ins_model         
## 
## Call:
## lm(formula = CustServ.Calls ~ Day.Charge + Eve.Charge + Night.Charge + 
##     Intl.Charge + Int.l.Plan + VMail.Plan + Churn., data = projectDS)
## 
## Coefficients:
##   (Intercept)     Day.Charge     Eve.Charge   Night.Charge    Intl.Charge  
##      2.128053      -0.008576      -0.010633      -0.012262      -0.042108  
## Int.l.Planyes  VMail.Planyes    Churn.True.  
##     -0.379534       0.026343       0.933609
   #understand the model results
   summary(ins_model)   
## 
## Call:
## lm(formula = CustServ.Calls ~ Day.Charge + Eve.Charge + Night.Charge + 
##     Intl.Charge + Int.l.Plan + VMail.Plan + Churn., data = projectDS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.4933 -0.6851 -0.3853  0.6121  7.1744 
## 
## Coefficients:
##                Estimate Std. Error t value Pr(>|t|)    
## (Intercept)    2.128053   0.170815  12.458  < 2e-16 ***
## Day.Charge    -0.008576   0.002448  -3.504 0.000465 ***
## Eve.Charge    -0.010633   0.005170  -2.057 0.039774 *  
## Night.Charge  -0.012262   0.009758  -1.257 0.208958    
## Intl.Charge   -0.042108   0.029509  -1.427 0.153683    
## Int.l.Planyes -0.379534   0.077728  -4.883 1.09e-06 ***
## VMail.Planyes  0.026343   0.049877   0.528 0.597431    
## Churn.True.    0.933609   0.067456  13.840  < 2e-16 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.28 on 3325 degrees of freedom
## Multiple R-squared:  0.05579,    Adjusted R-squared:  0.0538 
## F-statistic: 28.06 on 7 and 3325 DF,  p-value: < 2.2e-16
   #Lets train the model with some additional features
   projectDS$Day.Charge2<-projectDS$Day.Charge^2
   str(projectDS)
## 'data.frame':    3333 obs. of  20 variables:
##  $ Account.Length: int  128 107 137 84 75 118 121 147 117 141 ...
##  $ Area.Code     : int  415 415 415 408 415 510 510 415 408 415 ...
##  $ Int.l.Plan    : Factor w/ 2 levels "no","yes": 1 1 1 2 2 2 1 2 1 2 ...
##  $ VMail.Plan    : Factor w/ 2 levels "no","yes": 2 2 1 1 1 1 2 1 1 2 ...
##  $ VMail.Message : int  25 26 0 0 0 0 24 0 0 37 ...
##  $ Day.Mins      : num  265 162 243 299 167 ...
##  $ Day.Calls     : int  110 123 114 71 113 98 88 79 97 84 ...
##  $ Day.Charge    : num  45.1 27.5 41.4 50.9 28.3 ...
##  $ Eve.Mins      : num  197.4 195.5 121.2 61.9 148.3 ...
##  $ Eve.Calls     : int  99 103 110 88 122 101 108 94 80 111 ...
##  $ Eve.Charge    : num  16.78 16.62 10.3 5.26 12.61 ...
##  $ Night.Mins    : num  245 254 163 197 187 ...
##  $ Night.Calls   : int  91 103 104 89 121 118 118 96 90 97 ...
##  $ Night.Charge  : num  11.01 11.45 7.32 8.86 8.41 ...
##  $ Intl.Mins     : num  10 13.7 12.2 6.6 10.1 6.3 7.5 7.1 8.7 11.2 ...
##  $ Intl.Calls    : int  3 3 5 7 3 6 7 6 4 5 ...
##  $ Intl.Charge   : num  2.7 3.7 3.29 1.78 2.73 1.7 2.03 1.92 2.35 3.02 ...
##  $ CustServ.Calls: int  1 1 0 2 3 0 3 0 1 0 ...
##  $ Churn.        : Factor w/ 2 levels "False.","True.": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Day.Charge2   : num  2031 755 1712 2591 803 ...
   projectDS$Eve.Charge10<-ifelse(projectDS$Eve.Charge>=10,1,0)
   str(projectDS)   
## 'data.frame':    3333 obs. of  21 variables:
##  $ Account.Length: int  128 107 137 84 75 118 121 147 117 141 ...
##  $ Area.Code     : int  415 415 415 408 415 510 510 415 408 415 ...
##  $ Int.l.Plan    : Factor w/ 2 levels "no","yes": 1 1 1 2 2 2 1 2 1 2 ...
##  $ VMail.Plan    : Factor w/ 2 levels "no","yes": 2 2 1 1 1 1 2 1 1 2 ...
##  $ VMail.Message : int  25 26 0 0 0 0 24 0 0 37 ...
##  $ Day.Mins      : num  265 162 243 299 167 ...
##  $ Day.Calls     : int  110 123 114 71 113 98 88 79 97 84 ...
##  $ Day.Charge    : num  45.1 27.5 41.4 50.9 28.3 ...
##  $ Eve.Mins      : num  197.4 195.5 121.2 61.9 148.3 ...
##  $ Eve.Calls     : int  99 103 110 88 122 101 108 94 80 111 ...
##  $ Eve.Charge    : num  16.78 16.62 10.3 5.26 12.61 ...
##  $ Night.Mins    : num  245 254 163 197 187 ...
##  $ Night.Calls   : int  91 103 104 89 121 118 118 96 90 97 ...
##  $ Night.Charge  : num  11.01 11.45 7.32 8.86 8.41 ...
##  $ Intl.Mins     : num  10 13.7 12.2 6.6 10.1 6.3 7.5 7.1 8.7 11.2 ...
##  $ Intl.Calls    : int  3 3 5 7 3 6 7 6 4 5 ...
##  $ Intl.Charge   : num  2.7 3.7 3.29 1.78 2.73 1.7 2.03 1.92 2.35 3.02 ...
##  $ CustServ.Calls: int  1 1 0 2 3 0 3 0 1 0 ...
##  $ Churn.        : Factor w/ 2 levels "False.","True.": 1 1 1 1 1 1 1 1 1 1 ...
##  $ Day.Charge2   : num  2031 755 1712 2591 803 ...
##  $ Eve.Charge10  : num  1 1 1 0 1 1 1 0 1 1 ...
   ins_model2<-lm(CustServ.Calls~., data=projectDS)
   ins_model2   
## 
## Call:
## lm(formula = CustServ.Calls ~ ., data = projectDS)
## 
## Coefficients:
##    (Intercept)  Account.Length       Area.Code   Int.l.Planyes   VMail.Planyes  
##      1.1537396      -0.0001552       0.0009061      -0.4004997      -0.0320318  
##  VMail.Message        Day.Mins       Day.Calls      Day.Charge        Eve.Mins  
##      0.0021021       0.8419723      -0.0014328      -4.9117037       0.1042128  
##      Eve.Calls      Eve.Charge      Night.Mins     Night.Calls    Night.Charge  
##      0.0001999      -1.2427924      -0.1501389      -0.0008870       3.3226293  
##      Intl.Mins      Intl.Calls     Intl.Charge     Churn.True.     Day.Charge2  
##      1.1445911       0.0007060      -4.2806608       1.0165708      -0.0008247  
##   Eve.Charge10  
##      0.2490494
   summary(ins_model2)
## 
## Call:
## lm(formula = CustServ.Calls ~ ., data = projectDS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -2.6689 -0.7145 -0.3651  0.6455  7.2334 
## 
## Coefficients:
##                  Estimate Std. Error t value Pr(>|t|)    
## (Intercept)     1.1537396  0.3901394   2.957  0.00313 ** 
## Account.Length -0.0001552  0.0005566  -0.279  0.78034    
## Area.Code       0.0009061  0.0005236   1.730  0.08366 .  
## Int.l.Planyes  -0.4004997  0.0777976  -5.148 2.79e-07 ***
## VMail.Planyes  -0.0320318  0.1708426  -0.187  0.85129    
## VMail.Message   0.0021021  0.0055766   0.377  0.70623    
## Day.Mins        0.8419723  1.3140108   0.641  0.52172    
## Day.Calls      -0.0014328  0.0011047  -1.297  0.19471    
## Day.Charge     -4.9117037  7.7294627  -0.635  0.52518    
## Eve.Mins        0.1042128  0.6527897   0.160  0.87317    
## Eve.Calls       0.0001999  0.0011118   0.180  0.85730    
## Eve.Charge     -1.2427924  7.6798625  -0.162  0.87145    
## Night.Mins     -0.1501389  0.3496509  -0.429  0.66766    
## Night.Calls    -0.0008870  0.0011320  -0.784  0.43334    
## Night.Charge    3.3226293  7.7698449   0.428  0.66895    
## Intl.Mins       1.1445911  2.0826709   0.550  0.58265    
## Intl.Calls      0.0007060  0.0090274   0.078  0.93767    
## Intl.Charge    -4.2806608  7.7138763  -0.555  0.57898    
## Churn.True.     1.0165708  0.0699497  14.533  < 2e-16 ***
## Day.Charge2    -0.0008247  0.0001904  -4.331 1.53e-05 ***
## Eve.Charge10    0.2490494  0.1192826   2.088  0.03688 *  
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.276 on 3312 degrees of freedom
## Multiple R-squared:  0.06411,    Adjusted R-squared:  0.05846 
## F-statistic: 11.34 on 20 and 3312 DF,  p-value: < 2.2e-16
   ins_model3<-lm(CustServ.Calls~.+Churn.*Eve.Charge10,data=projectDS) 
   ins_model3 
## 
## Call:
## lm(formula = CustServ.Calls ~ . + Churn. * Eve.Charge10, data = projectDS)
## 
## Coefficients:
##              (Intercept)            Account.Length                 Area.Code  
##                1.0267627                -0.0001970                 0.0009092  
##            Int.l.Planyes             VMail.Planyes             VMail.Message  
##               -0.4032994                -0.0326586                 0.0020520  
##                 Day.Mins                 Day.Calls                Day.Charge  
##                0.6835935                -0.0013677                -3.9814694  
##                 Eve.Mins                 Eve.Calls                Eve.Charge  
##                0.1122904                 0.0003152                -1.3375981  
##               Night.Mins               Night.Calls              Night.Charge  
##               -0.1158951                -0.0008766                 2.5616278  
##                Intl.Mins                Intl.Calls               Intl.Charge  
##                1.4023086                 0.0010897                -5.2372801  
##              Churn.True.               Day.Charge2              Eve.Charge10  
##                2.2936697                -0.0007919                 0.3777842  
## Churn.True.:Eve.Charge10  
##               -1.3260432
   summary(ins_model3)   
## 
## Call:
## lm(formula = CustServ.Calls ~ . + Churn. * Eve.Charge10, data = projectDS)
## 
## Residuals:
##     Min      1Q  Median      3Q     Max 
## -3.2829 -0.7116 -0.3631  0.6397  7.2648 
## 
## Coefficients:
##                            Estimate Std. Error t value Pr(>|t|)    
## (Intercept)               1.0267627  0.3908469   2.627 0.008653 ** 
## Account.Length           -0.0001970  0.0005556  -0.355 0.722943    
## Area.Code                 0.0009092  0.0005226   1.740 0.081997 .  
## Int.l.Planyes            -0.4032994  0.0776486  -5.194 2.18e-07 ***
## VMail.Planyes            -0.0326586  0.1705076  -0.192 0.848116    
## VMail.Message             0.0020520  0.0055657   0.369 0.712380    
## Day.Mins                  0.6835935  1.3121148   0.521 0.602411    
## Day.Calls                -0.0013677  0.0011026  -1.240 0.214915    
## Day.Charge               -3.9814694  7.7182979  -0.516 0.605995    
## Eve.Mins                  0.1122904  0.6515128   0.172 0.863170    
## Eve.Calls                 0.0003152  0.0011101   0.284 0.776490    
## Eve.Charge               -1.3375981  7.6648405  -0.175 0.861475    
## Night.Mins               -0.1158951  0.3490848  -0.332 0.739912    
## Night.Calls              -0.0008766  0.0011298  -0.776 0.437849    
## Night.Charge              2.5616278  7.7572654   0.330 0.741252    
## Intl.Mins                 1.4023086  2.0797242   0.674 0.500183    
## Intl.Calls                0.0010897  0.0090102   0.121 0.903750    
## Intl.Charge              -5.2372801  7.7029804  -0.680 0.496613    
## Churn.True.               2.2936697  0.3480148   6.591 5.07e-11 ***
## Day.Charge2              -0.0007919  0.0001903  -4.162 3.23e-05 ***
## Eve.Charge10              0.3777842  0.1239101   3.049 0.002315 ** 
## Churn.True.:Eve.Charge10 -1.3260432  0.3540071  -3.746 0.000183 ***
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
## 
## Residual standard error: 1.274 on 3311 degrees of freedom
## Multiple R-squared:  0.06806,    Adjusted R-squared:  0.06215 
## F-statistic: 11.51 on 21 and 3311 DF,  p-value: < 2.2e-16