4.7 Making prescriptions

In this section, we will use our predictive model to get probabilities for a set of potential customers. we will then use those probabilities to create segments to target.

First, get the list of prospects.

prospects <- read.csv("http://bit.ly/2VSzAVT",
                      stringsAsFactors = FALSE)

We will have to perform the same adjustments that we did on the training data.

prospects2 <- prospects %>% 
  mutate(CallStart = lubridate::hms(CallStart),
         CallEnd  = lubridate::hms(CallEnd),
         CallStartHour = hour(CallStart),
         CallStartMin = minute(CallStart),
         CallStartSec = second(CallStart),
         CallEndHour = hour(CallEnd),
         CallEndMin = minute(CallEnd),
         CallEndSec = second(CallEnd),
         CallDuration = period_to_seconds(CallEnd) - period_to_seconds(CallStart)) %>% 
  select(-CallStart, -CallEnd) %>% 
  mutate_if(is.character, as.factor) %>% 
  mutate(Communication = ifelse(is.na(Communication), 
                                "Not Available", 
                                Communication),
         Outcome = ifelse(is.na(Outcome), "None", Outcome),
         DaysPassed = ifelse(DaysPassed == -1, 1000, DaysPassed))

Impute missing values as before.

set.seed(8934)

miceMod2 <- mice::mice(subset(prospects2, select = -CarInsurance), 
                      method = "rf")

Generate the completed data, create dummy variables, and convert CarInsurance to a factor.

prospects3 <- mice::complete(miceMod2)


prospects3$CarInsurance <- prospects2$CarInsurance

prospects4 <- predict(dummyVars(CarInsurance ~ .,
                         data = prospects3,
                         fullRank = TRUE),
                      newdata = prospects3) %>% 
  data.frame()

prospects4$CarInsurance <- prospects2$CarInsurance

prospects4 <- prospects4 %>% 
  mutate(CarInsurance = as.factor(ifelse(CarInsurance == 0, "No", "Yes")))