Load library

library(caret)
library(tidyverse)


Load data

read_csv("churn.csv")
## # A tibble: 5,000 × 18
##    churn accountlength internationalplan voicemailplan numbervmailmessages
##    <chr>         <dbl> <chr>             <chr>                       <dbl>
##  1 No              128 no                yes                            25
##  2 No              107 no                yes                            26
##  3 No              137 no                no                              0
##  4 No               84 yes               no                              0
##  5 No               75 yes               no                              0
##  6 No              118 yes               no                              0
##  7 No              121 no                yes                            24
##  8 No              147 yes               no                              0
##  9 No              117 no                no                              0
## 10 No              141 yes               yes                            37
## # ℹ 4,990 more rows
## # ℹ 13 more variables: totaldayminutes <dbl>, totaldaycalls <dbl>,
## #   totaldaycharge <dbl>, totaleveminutes <dbl>, totalevecalls <dbl>,
## #   totalevecharge <dbl>, totalnightminutes <dbl>, totalnightcalls <dbl>,
## #   totalnightcharge <dbl>, totalintlminutes <dbl>, totalintlcalls <dbl>,
## #   totalintlcharge <dbl>, numbercustomerservicecalls <dbl>
churn <- read_csv("churn.csv")


Review data

glimpse(churn)
## Rows: 5,000
## Columns: 18
## $ churn                      <chr> "No", "No", "No", "No", "No", "No", "No", "…
## $ accountlength              <dbl> 128, 107, 137, 84, 75, 118, 121, 147, 117, …
## $ internationalplan          <chr> "no", "no", "no", "yes", "yes", "yes", "no"…
## $ voicemailplan              <chr> "yes", "yes", "no", "no", "no", "no", "yes"…
## $ numbervmailmessages        <dbl> 25, 26, 0, 0, 0, 0, 24, 0, 0, 37, 0, 0, 0, …
## $ totaldayminutes            <dbl> 265.1, 161.6, 243.4, 299.4, 166.7, 223.4, 2…
## $ totaldaycalls              <dbl> 110, 123, 114, 71, 113, 98, 88, 79, 97, 84,…
## $ totaldaycharge             <dbl> 45.07, 27.47, 41.38, 50.90, 28.34, 37.98, 3…
## $ totaleveminutes            <dbl> 197.4, 195.5, 121.2, 61.9, 148.3, 220.6, 34…
## $ totalevecalls              <dbl> 99, 103, 110, 88, 122, 101, 108, 94, 80, 11…
## $ totalevecharge             <dbl> 16.78, 16.62, 10.30, 5.26, 12.61, 18.75, 29…
## $ totalnightminutes          <dbl> 244.7, 254.4, 162.6, 196.9, 186.9, 203.9, 2…
## $ totalnightcalls            <dbl> 91, 103, 104, 89, 121, 118, 118, 96, 90, 97…
## $ totalnightcharge           <dbl> 11.01, 11.45, 7.32, 8.86, 8.41, 9.18, 9.57,…
## $ totalintlminutes           <dbl> 10.0, 13.7, 12.2, 6.6, 10.1, 6.3, 7.5, 7.1,…
## $ totalintlcalls             <dbl> 3, 3, 5, 7, 3, 6, 7, 6, 4, 5, 6, 5, 2, 5, 6…
## $ totalintlcharge            <dbl> 2.70, 3.70, 3.29, 1.78, 2.73, 1.70, 2.03, 1…
## $ numbercustomerservicecalls <dbl> 1, 1, 0, 2, 3, 0, 3, 0, 1, 0, 4, 0, 1, 3, 4…


Prepare data

Change column churn, internationalplan, voicemailplan to factor

df <- churn %>%
    select(churn, internationalplan, voicemailplan, numbervmailmessages, totaldaycalls, totaldaycharge) %>%
    mutate(churn = factor(churn),
           internationalplan  = factor(internationalplan),
           voicemailplan = factor(voicemailplan))

glimpse(df)
## Rows: 5,000
## Columns: 6
## $ churn               <fct> No, No, No, No, No, No, No, No, No, No, Yes, No, N…
## $ internationalplan   <fct> no, no, no, yes, yes, yes, no, yes, no, yes, no, n…
## $ voicemailplan       <fct> yes, yes, no, no, no, no, yes, no, no, yes, no, no…
## $ numbervmailmessages <dbl> 25, 26, 0, 0, 0, 0, 24, 0, 0, 37, 0, 0, 0, 0, 0, 0…
## $ totaldaycalls       <dbl> 110, 123, 114, 71, 113, 98, 88, 79, 97, 84, 137, 1…
## $ totaldaycharge      <dbl> 45.07, 27.47, 41.38, 50.90, 28.34, 37.98, 37.09, 2…


Create function split_data(data, size)

split_data() รับ input 2 ตัว

data = data set

size = percent of train data

split_data <- function(data, size) {
  set.seed(42)
  n <- nrow(data)
  numrow <- sample(n, size = size*n)
  train_data <- data[numrow, ]
  test_data <- data[-numrow, ]
  return(list(train_data, test_data))
}


Split data

split <- split_data(df, 0.8)

train_data <- split[[1]] # train data 
test_data <- split[[2]] # test data 


Train data

glimpse(train_data)
## Rows: 4,000
## Columns: 6
## $ churn               <fct> No, No, No, No, No, No, No, No, No, No, No, No, No…
## $ internationalplan   <fct> no, no, no, no, no, no, no, no, no, no, yes, no, n…
## $ voicemailplan       <fct> yes, no, no, yes, no, yes, yes, yes, no, no, no, n…
## $ numbervmailmessages <dbl> 46, 0, 0, 29, 0, 34, 28, 43, 0, 0, 0, 0, 0, 24, 0,…
## $ totaldaycalls       <dbl> 124, 91, 83, 121, 105, 104, 88, 94, 105, 60, 94, 6…
## $ totaldaycharge      <dbl> 28.61, 42.45, 41.89, 47.58, 28.75, 24.89, 39.64, 2…


Test data

glimpse(test_data)
## Rows: 1,000
## Columns: 6
## $ churn               <fct> No, Yes, No, Yes, No, No, No, No, Yes, No, No, Yes…
## $ internationalplan   <fct> yes, no, no, no, no, no, no, no, yes, no, no, no, …
## $ voicemailplan       <fct> no, no, no, no, no, no, yes, no, yes, no, no, no, …
## $ numbervmailmessages <dbl> 0, 0, 0, 0, 0, 0, 30, 0, 41, 0, 0, 0, 0, 0, 0, 0, …
## $ totaldaycalls       <dbl> 113, 137, 117, 89, 103, 73, 128, 94, 85, 57, 90, 1…
## $ totaldaycharge      <dbl> 28.34, 21.95, 26.37, 10.61, 18.77, 22.83, 24.87, 2…


Train model

y = f(x1, x2, x3, x4, x5)

y = churn

x1 = internationalplan

x2 = voicemailplan

x3 = numbervmailmessages

x4 = totaldaycalls

x5 = totaldaycharge


glm_model <- train(churn ~ internationalplan + voicemailplan + numbervmailmessages + totaldaycalls + totaldaycharge, 
                   data = train_data, 
                   method = "glm" )

glm_model
## Generalized Linear Model 
## 
## 4000 samples
##    5 predictor
##    2 classes: 'No', 'Yes' 
## 
## No pre-processing
## Resampling: Bootstrapped (25 reps) 
## Summary of sample sizes: 4000, 4000, 4000, 4000, 4000, 4000, ... 
## Resampling results:
## 
##   Accuracy   Kappa    
##   0.8564185  0.1202425


Score and evaluate

p <- predict(glm_model, newdata = test_data)


Accuracy Test data

accuracy <- mean(p == test_data$churn)
accuracy
## [1] 0.856