7/2/25, 9:05 PM Midterm Exam 2
Midterm Exam 2
Part 2: Data Analysis
You will use two datasets for this exam.
For Questions 1-4, use the dataset “Customer churn”. See description below.
For Question 5-7, use the dataset “ShipAccidents”. See description below.
Customer Churn Dataset
This dataset is part of a data science project focused on customer churn prediction for a subscription-based
service. Customer churn, the rate at which customers cancel their subscriptions, is a vital metric for businesses
offering subscription services. Predictive analytics techniques are employed to anticipate which customers are
likely to churn, enabling companies to take proactive measures for customer retention.
SubscriptionType: Type of subscription plan chosen by the customer (e.g., Basic, Premium, Deluxe)
PaymentMethod: Method used for payment (e.g., Credit Card, Electronic Check, PayPal)
PaperlessBilling: Whether the customer uses paperless billing (Yes/No)
ContentType: Type of content accessed by the customer (e.g., Movies, TV Shows, Documentaries)
MultiDeviceAccess: Whether the customer has access on multiple devices (Yes/No)
DeviceRegistered: Device registered by the customer (e.g., Smartphone, Smart TV, Laptop)
GenrePreference: Genre preference of the customer (e.g., Action, Drama, Comedy)
Gender: Gender of the customer (Male/Female)
ParentalControl: Whether parental control is enabled (Yes/No)
SubtitlesEnabled: Whether subtitles are enabled (Yes/No)
AccountAge: Age of the customer’s subscription account (in months)
MonthlyCharges: Monthly subscription charges
TotalCharges: Total charges incurred by the customer
ViewingHoursPerWeek: Average number of viewing hours per week
SupportTicketsPerMonth: Number of customer support tickets raised per month
AverageViewingDuration: Average duration of each viewing session
ContentDownloadsPerMonth: Number of content downloads per month
UserRating: Customer satisfaction rating (1 to 5)
WatchlistSize: Size of the customer’s content watchlist
Churn (response variable): 1 if the customer has cancelled the subscription, 0 if not.
Read the data and answer the questions below:
file:///C:/Users/shani/Downloads/Spring2024_midterm_2_R_solutions.html 1/21
,7/2/25, 9:05 PM Midterm Exam 2
NOTE: The categorical variables have already been converted into factors in the code below. The dataset has
been divided into train and test datasets.
# Loading of the data
churn= read.csv("Customer churn.csv", header=TRUE, sep=",")
churn$SubscriptionType=as.factor(churn$SubscriptionType)
churn$PaymentMethod=as.factor(churn$PaymentMethod)
churn$PaperlessBilling=as.factor(churn$PaperlessBilling)
churn$ContentType=as.factor(churn$ContentType)
churn$MultiDeviceAccess=as.factor(churn$MultiDeviceAccess)
churn$DeviceRegistered=as.factor(churn$DeviceRegistered)
churn$GenrePreference=as.factor(churn$GenrePreference)
churn$Gender=as.factor(churn$Gender)
churn$ParentalControl=as.factor(churn$ParentalControl)
churn$SubtitlesEnabled=as.factor(churn$SubtitlesEnabled)
churn$Churn=as.factor(churn$Churn)
set.seed(123) # Setting seed for reproducibility
nrows <- nrow(churn)
train_indices <- sample(1:nrows, 0.8 * nrows) # 80% of data for training
# Split the data into train and test sets
train_churn <- churn[train_indices, ]
test_churn <- churn[-train_indices, ]
#resetting the indices
rownames(train_churn) <- NULL
rownames(test_churn) <- NULL
head(train_churn)
file:///C:/Users/shani/Downloads/Spring2024_midterm_2_R_solutions.html 2/21
, 7/2/25, 9:05 PM Midterm Exam 2
## AccountAge MonthlyCharges TotalCharges SubscriptionType PaymentMethod
## 1 79 12.940000 1021.9544 Standard Credit card
## 2 52 7.400856 384.8445 Basic Bank transfer
## 3 57 13.907165 792.7084 Basic Bank transfer
## 4 69 9.990000 689.2958 Basic Mailed check
## 5 52 13.011418 676.5937 Standard Credit card
## 6 14 18.886117 264.4056 Basic Mailed check
## PaperlessBilling ContentType MultiDeviceAccess DeviceRegistered
## 1 No Both Yes Tablet
## 2 Yes Both Yes Computer
## 3 No Movies No TV
## 4 No TV Shows No Mobile
## 5 Yes TV Shows No Tablet
## 6 No Both Yes TV
## ViewingHoursPerWeek AverageViewingDuration ContentDownloadsPerMonth
## 1 35.281760 57.02602 16
## 2 23.196542 30.24825 26
## 3 28.992753 36.39316 11
## 4 13.294368 23.04048 28
## 5 27.749768 152.69022 47
## 6 3.718285 71.87396 2
## GenrePreference UserRating SupportTicketsPerMonth Gender WatchlistSize
## 1 Drama 4.785032 5 Female 18
## 2 Drama 1.374711 4 Male 24
## 3 Drama 4.080789 6 Male 8
## 4 Drama 3.972756 9 Female 23
## 5 Drama 4.305154 6 Female 19
## 6 Drama 1.355945 3 Female 7
## ParentalControl SubtitlesEnabled Churn
## 1 No Yes 0
## 2 Yes Yes 0
## 3 No No 1
## 4 Yes No 1
## 5 Yes Yes 0
## 6 No No 1
Use “train_churn” for the following questions, unless stated otherwise.
Q1 EXPLORATORY DATA ANALYSIS (12 points)
A. (6points) Create the boxplots of the response variable Churn against the following predictors:
i. AverageViewingDuration; ii) ContentDownloadsPerMonth; and iii) MonthlyCharges.
Interpret each plot.
## Your code here...
boxplot(AverageViewingDuration~Churn, data=train_churn, ylab="AverageViewingDuration", xlab ="Ch
urn")
file:///C:/Users/shani/Downloads/Spring2024_midterm_2_R_solutions.html 3/21
Midterm Exam 2
Part 2: Data Analysis
You will use two datasets for this exam.
For Questions 1-4, use the dataset “Customer churn”. See description below.
For Question 5-7, use the dataset “ShipAccidents”. See description below.
Customer Churn Dataset
This dataset is part of a data science project focused on customer churn prediction for a subscription-based
service. Customer churn, the rate at which customers cancel their subscriptions, is a vital metric for businesses
offering subscription services. Predictive analytics techniques are employed to anticipate which customers are
likely to churn, enabling companies to take proactive measures for customer retention.
SubscriptionType: Type of subscription plan chosen by the customer (e.g., Basic, Premium, Deluxe)
PaymentMethod: Method used for payment (e.g., Credit Card, Electronic Check, PayPal)
PaperlessBilling: Whether the customer uses paperless billing (Yes/No)
ContentType: Type of content accessed by the customer (e.g., Movies, TV Shows, Documentaries)
MultiDeviceAccess: Whether the customer has access on multiple devices (Yes/No)
DeviceRegistered: Device registered by the customer (e.g., Smartphone, Smart TV, Laptop)
GenrePreference: Genre preference of the customer (e.g., Action, Drama, Comedy)
Gender: Gender of the customer (Male/Female)
ParentalControl: Whether parental control is enabled (Yes/No)
SubtitlesEnabled: Whether subtitles are enabled (Yes/No)
AccountAge: Age of the customer’s subscription account (in months)
MonthlyCharges: Monthly subscription charges
TotalCharges: Total charges incurred by the customer
ViewingHoursPerWeek: Average number of viewing hours per week
SupportTicketsPerMonth: Number of customer support tickets raised per month
AverageViewingDuration: Average duration of each viewing session
ContentDownloadsPerMonth: Number of content downloads per month
UserRating: Customer satisfaction rating (1 to 5)
WatchlistSize: Size of the customer’s content watchlist
Churn (response variable): 1 if the customer has cancelled the subscription, 0 if not.
Read the data and answer the questions below:
file:///C:/Users/shani/Downloads/Spring2024_midterm_2_R_solutions.html 1/21
,7/2/25, 9:05 PM Midterm Exam 2
NOTE: The categorical variables have already been converted into factors in the code below. The dataset has
been divided into train and test datasets.
# Loading of the data
churn= read.csv("Customer churn.csv", header=TRUE, sep=",")
churn$SubscriptionType=as.factor(churn$SubscriptionType)
churn$PaymentMethod=as.factor(churn$PaymentMethod)
churn$PaperlessBilling=as.factor(churn$PaperlessBilling)
churn$ContentType=as.factor(churn$ContentType)
churn$MultiDeviceAccess=as.factor(churn$MultiDeviceAccess)
churn$DeviceRegistered=as.factor(churn$DeviceRegistered)
churn$GenrePreference=as.factor(churn$GenrePreference)
churn$Gender=as.factor(churn$Gender)
churn$ParentalControl=as.factor(churn$ParentalControl)
churn$SubtitlesEnabled=as.factor(churn$SubtitlesEnabled)
churn$Churn=as.factor(churn$Churn)
set.seed(123) # Setting seed for reproducibility
nrows <- nrow(churn)
train_indices <- sample(1:nrows, 0.8 * nrows) # 80% of data for training
# Split the data into train and test sets
train_churn <- churn[train_indices, ]
test_churn <- churn[-train_indices, ]
#resetting the indices
rownames(train_churn) <- NULL
rownames(test_churn) <- NULL
head(train_churn)
file:///C:/Users/shani/Downloads/Spring2024_midterm_2_R_solutions.html 2/21
, 7/2/25, 9:05 PM Midterm Exam 2
## AccountAge MonthlyCharges TotalCharges SubscriptionType PaymentMethod
## 1 79 12.940000 1021.9544 Standard Credit card
## 2 52 7.400856 384.8445 Basic Bank transfer
## 3 57 13.907165 792.7084 Basic Bank transfer
## 4 69 9.990000 689.2958 Basic Mailed check
## 5 52 13.011418 676.5937 Standard Credit card
## 6 14 18.886117 264.4056 Basic Mailed check
## PaperlessBilling ContentType MultiDeviceAccess DeviceRegistered
## 1 No Both Yes Tablet
## 2 Yes Both Yes Computer
## 3 No Movies No TV
## 4 No TV Shows No Mobile
## 5 Yes TV Shows No Tablet
## 6 No Both Yes TV
## ViewingHoursPerWeek AverageViewingDuration ContentDownloadsPerMonth
## 1 35.281760 57.02602 16
## 2 23.196542 30.24825 26
## 3 28.992753 36.39316 11
## 4 13.294368 23.04048 28
## 5 27.749768 152.69022 47
## 6 3.718285 71.87396 2
## GenrePreference UserRating SupportTicketsPerMonth Gender WatchlistSize
## 1 Drama 4.785032 5 Female 18
## 2 Drama 1.374711 4 Male 24
## 3 Drama 4.080789 6 Male 8
## 4 Drama 3.972756 9 Female 23
## 5 Drama 4.305154 6 Female 19
## 6 Drama 1.355945 3 Female 7
## ParentalControl SubtitlesEnabled Churn
## 1 No Yes 0
## 2 Yes Yes 0
## 3 No No 1
## 4 Yes No 1
## 5 Yes Yes 0
## 6 No No 1
Use “train_churn” for the following questions, unless stated otherwise.
Q1 EXPLORATORY DATA ANALYSIS (12 points)
A. (6points) Create the boxplots of the response variable Churn against the following predictors:
i. AverageViewingDuration; ii) ContentDownloadsPerMonth; and iii) MonthlyCharges.
Interpret each plot.
## Your code here...
boxplot(AverageViewingDuration~Churn, data=train_churn, ylab="AverageViewingDuration", xlab ="Ch
urn")
file:///C:/Users/shani/Downloads/Spring2024_midterm_2_R_solutions.html 3/21