library(ggplot2)
# 1. Load the cleaned survey data (saved by your Discussion 12 code)
d <- read.csv("clean_data.csv", colClasses = c(addons = "character"))
nrow(d) # should be 11
## [1] 11
# 2. Helper variables
# number of add-ons (Q4 holds codes like "1,3,5"; blank = none)
d$n_addons <- ifelse(is.na(d$addons) | d$addons == "", 0,
lengths(strsplit(d$addons, ",")))
# add-on code 1 = tech support
d$tech_support <- as.numeric(grepl("(^|,)1(,|$)", d$addons))
# contract code 4 = "not sure", so it is left out of the month-to-month test
d$month_to_month <- ifelse(d$contract_type == 4, NA,
as.numeric(d$contract_type == 1))
# plan choice: 3 = Plan C (two-year contract)
d$plan_C <- as.numeric(d$plan_choice == 3)
# 3. Labels for the answer codes
d$contract_label <- factor(d$contract_type, levels = 1:4,
labels = c("Month-to-month", "One year", "Two year", "Not sure"))
d$tenure_label <- factor(d$tenure, levels = 2:4,
labels = c("1-2 yrs", "3-5 yrs", "5+ yrs"))
# 4. Cancel-intent score (Q5): how many chose each score, and the average
table(d$churn_intent)
##
## 1 2 3 4
## 6 2 2 1
mean(d$churn_intent) # about 1.82
## [1] 1.818182
# 5. Average cancel intent by contract and by tenure (slide 4)
summ <- function(g, y) {
g <- droplevels(g)
data.frame(group = levels(g),
n = as.vector(table(g)),
mean_cancel_intent = round(as.vector(tapply(y, g, mean)), 2))
}
contract_tbl <- summ(d$contract_label, d$churn_intent)
tenure_tbl <- summ(d$tenure_label, d$churn_intent)
contract_tbl
## group n mean_cancel_intent
## 1 Month-to-month 4 1.00
## 2 One year 3 2.33
## 3 Two year 1 1.00
## 4 Not sure 3 2.67
tenure_tbl
## group n mean_cancel_intent
## 1 1-2 yrs 2 1.00
## 2 3-5 yrs 3 1.67
## 3 5+ yrs 6 2.17
bar_plot <- function(tbl, title) {
ggplot(tbl, aes(x = factor(group, levels = group), y = mean_cancel_intent)) +
geom_col(fill = "#1E7F73", width = 0.6) +
geom_text(aes(label = paste0(mean_cancel_intent, "\n(n = ", n, ")")),
vjust = -0.3) +
ylim(0, 5) +
labs(title = title, x = NULL, y = "Average cancel intent (1-5)") +
theme_minimal()
}
bar_plot(contract_tbl, "Cancel intent by contract type")

bar_plot(tenure_tbl, "Cancel intent by time as a customer")

# 6. Correlation of each survey answer with cancel intent (slide 5)
cor_row <- function(label, x) {
ct <- cor.test(x, d$churn_intent) # drops missing pairs automatically
data.frame(answer = label,
n = unname(ct$parameter) + 2,
r = round(unname(ct$estimate), 2),
p = round(ct$p.value, 3))
}
cors <- rbind(
cor_row("Time as customer (Q3)", d$tenure),
cor_row("Support satisfaction (Q8)", d$support_sat),
cor_row("Chose two-year plan C (Q6)", d$plan_C),
cor_row("Has tech support (Q4)", d$tech_support),
cor_row("Number of add-ons (Q4)", d$n_addons),
cor_row("Commitment (Q10)", d$commitment),
cor_row("Price fairness (Q7)", d$price_fair),
cor_row("Switching hassle (Q9)", d$switch_hassle),
cor_row("Month-to-month (Q2)", d$month_to_month)
)
cors
## answer n r p
## 1 Time as customer (Q3) 11 0.43 0.190
## 2 Support satisfaction (Q8) 11 0.35 0.284
## 3 Chose two-year plan C (Q6) 11 0.34 0.308
## 4 Has tech support (Q4) 11 0.32 0.342
## 5 Number of add-ons (Q4) 11 0.24 0.478
## 6 Commitment (Q10) 11 -0.06 0.861
## 7 Price fairness (Q7) 11 -0.21 0.537
## 8 Switching hassle (Q9) 11 -0.31 0.356
## 9 Month-to-month (Q2) 8 -0.71 0.050
cors$answer <- factor(cors$answer, levels = cors$answer[order(cors$r)])
ggplot(cors, aes(x = r, y = answer, fill = r > 0)) +
geom_col() +
geom_text(aes(x = r + ifelse(r > 0, 0.07, -0.07), label = sprintf("%+.2f", r))) +
scale_fill_manual(values = c("TRUE" = "#1E7F73", "FALSE" = "#F0A25F"),
guide = "none") +
scale_x_continuous(limits = c(-0.9, 0.9)) +
labs(title = "Which answers move with cancel intent",
x = "Correlation (r) with cancel intent", y = NULL) +
theme_minimal()
