Load the dataset
car_survey_data = read.csv("~/Desktop/MKTG Assignment 2/Cleaned_Car_Survey_Merged.csv")
str(car_survey_data)
## 'data.frame': 1049 obs. of 31 variables:
## $ Resp : chr "Res1" "Res10" "Res100" "Res1000" ...
## $ Att_1 : num 6 6 6 6 6 3 2 7 2 6 ...
## $ Att_2 : int 6 6 7 6 6 1 2 7 1 6 ...
## $ Enj_1 : num 6 4 7 7 7 4 1 7 2 6 ...
## $ Enj_2 : num 6 4 3 6 6 3 2 6 1 5 ...
## $ Perform_1 : num 5 4 5 6 6 5 2 5 2 5 ...
## $ Perform_2 : num 6 4 6 6 6 6 2 6 2 5 ...
## $ Perform_3 : num 3 1 6 6 6 6 1 5 2 5 ...
## $ WOM_1 : num 3 5 3 6 4 2 6 6 7 3 ...
## $ WOM_2 : num 3 6 5 6 4 6 7 6 7 3 ...
## $ Futu_Pur_1 : num 3 6 6 6 4 6 6 6 7 6 ...
## $ Futu_Pur_2 : num 3 6 6 6 6 6 5 7 7 6 ...
## $ Valu_Percp_1: num 5 6 7 4 5 5 4 6 4 5 ...
## $ Valu_Percp_2: num 2 6 6 6 6 4 4 5 6 6 ...
## $ Pur_Proces_1: num 6 6 5 6 6 5 4 5 6 6 ...
## $ Pur_Proces_2: num 4 6 5 3 7 5 5 5 7 5 ...
## $ Residence : num 2 1 2 2 1 1 1 2 1 2 ...
## $ Pay_Meth : int 2 2 1 3 3 3 3 3 3 3 ...
## $ Insur_Type : chr "Collision" "Collision" "Collision" "Liability" ...
## $ Gender : chr "Male" "Male" "Female" "Female" ...
## $ Age : int 18 21 32 24 24 25 26 26 27 27 ...
## $ Education : int 2 2 1 2 2 2 2 2 2 2 ...
## $ X : logi NA NA NA NA NA NA ...
## $ Region : chr "European" "European" "American" "Asian" ...
## $ Model : chr "Ford Expedition" "Ford Expedition" "Toyota Rav4" "Toyota Corolla" ...
## $ MPG : int 15 15 24 26 26 26 26 26 26 26 ...
## $ Cyl : int 8 8 4 4 4 4 4 4 4 4 ...
## $ acc1 : num 5.5 5.5 8.2 8 8 8 8 8 8 8 ...
## $ C_cost. : num 16 16 10 7 7 7 7 7 7 7 ...
## $ H_Cost : num 14 14 8 6 6 6 6 6 6 6 ...
## $ Post.Satis : int 4 5 4 6 5 6 5 6 7 6 ...
head(car_survey_data, n=10)
## Resp Att_1 Att_2 Enj_1 Enj_2 Perform_1 Perform_2 Perform_3 WOM_1 WOM_2
## 1 Res1 6 6 6 6 5 6 3 3 3
## 2 Res10 6 6 4 4 4 4 1 5 6
## 3 Res100 6 7 7 3 5 6 6 3 5
## 4 Res1000 6 6 7 6 6 6 6 6 6
## 5 Res1001 6 6 7 6 6 6 6 4 4
## 6 Res1002 3 1 4 3 5 6 6 2 6
## 7 Res1003 2 2 1 2 2 2 1 6 7
## 8 Res1004 7 7 7 6 5 6 5 6 6
## 9 Res1005 2 1 2 1 2 2 2 7 7
## 10 Res1006 6 6 6 5 5 5 5 3 3
## Futu_Pur_1 Futu_Pur_2 Valu_Percp_1 Valu_Percp_2 Pur_Proces_1 Pur_Proces_2
## 1 3 3 5 2 6 4
## 2 6 6 6 6 6 6
## 3 6 6 7 6 5 5
## 4 6 6 4 6 6 3
## 5 4 6 5 6 6 7
## 6 6 6 5 4 5 5
## 7 6 5 4 4 4 5
## 8 6 7 6 5 5 5
## 9 7 7 4 6 6 7
## 10 6 6 5 6 6 5
## Residence Pay_Meth Insur_Type Gender Age Education X Region
## 1 2 2 Collision Male 18 2 NA European
## 2 1 2 Collision Male 21 2 NA European
## 3 2 1 Collision Female 32 1 NA American
## 4 2 3 Liability Female 24 2 NA Asian
## 5 1 3 Liability Female 24 2 NA Asian
## 6 1 3 Liability Female 25 2 NA Asian
## 7 1 3 Liability Female 26 2 NA Asian
## 8 2 3 Liability Female 26 2 NA Asian
## 9 1 3 Liability Female 27 2 NA Asian
## 10 2 3 Liability Female 27 2 NA Asian
## Model MPG Cyl acc1 C_cost. H_Cost Post.Satis
## 1 Ford Expedition 15 8 5.5 16 14 4
## 2 Ford Expedition 15 8 5.5 16 14 5
## 3 Toyota Rav4 24 4 8.2 10 8 4
## 4 Toyota Corolla 26 4 8.0 7 6 6
## 5 Toyota Corolla 26 4 8.0 7 6 5
## 6 Toyota Corolla 26 4 8.0 7 6 6
## 7 Toyota Corolla 26 4 8.0 7 6 5
## 8 Toyota Corolla 26 4 8.0 7 6 6
## 9 Toyota Corolla 26 4 8.0 7 6 7
## 10 Toyota Corolla 26 4 8.0 7 6 6
Group by Car Make
library(stringr)
#Separate Model col into two, delimit using space
car_survey_data[c('Make', 'Model_v1')] = str_split_fixed(car_survey_data$Model, " ", 2)
#Check values of new column
table(car_survey_data$Make)
##
## Buick Chevrolet Chrysler Dodge Fiat Ford Honda Kia
## 31 64 169 41 18 202 159 34
## Lincoln Toyota
## 39 292
# Create a frequency table of car makes
make_table <- table(car_survey_data$Make)
# Convert the table to a dataframe for plotting
make_df <- as.data.frame(make_table)
colnames(make_df) <- c("Make", "Count")
#Group by Brand (Parent)
car_survey_data <- car_survey_data %>% #call dataframe and create new column with new groupings
mutate(Parent = case_when(
Make == "Buick" ~ "General Motors",
Make == "Chevrolet" ~ "General Motors",
Make == "Chrysler" ~ "Chrysler",
Make == "Dodge" ~ "Chrysler",
Make == "Fiat" ~ "Chrysler",
Make == "Ford" ~ "Ford",
Make == "Honda" ~ "Honda",
Make == "Kia" ~ "Kia",
Make == "Lincoln" ~ "Ford",
Make == "Toyota" ~ "Toyota",
TRUE ~ "Check"
))
# Check if grouping is correct
count(car_survey_data, car_survey_data$Make, car_survey_data$Parent, name = "Freq")
## car_survey_data$Make car_survey_data$Parent Freq
## 1 Buick General Motors 31
## 2 Chevrolet General Motors 64
## 3 Chrysler Chrysler 169
## 4 Dodge Chrysler 41
## 5 Fiat Chrysler 18
## 6 Ford Ford 202
## 7 Honda Honda 159
## 8 Kia Kia 34
## 9 Lincoln Ford 39
## 10 Toyota Toyota 292
# Count the number of occurrences for each Parent
parent_count <- car_survey_data %>%
group_by(Parent) %>%
summarise(Count = n())
Group by Brand (Parent)
car_survey_data <- car_survey_data %>%
mutate(Parent = case_when(
Make == "Buick" ~ "General Motors",
Make == "Chevrolet" ~ "General Motors",
Make == "Chrysler" ~ "Chrysler",
Make == "Dodge" ~ "Chrysler",
Make == "Fiat" ~ "Chrysler",
Make == "Ford" ~ "Ford",
Make == "Honda" ~ "Honda",
Make == "Kia" ~ "Kia",
Make == "Lincoln" ~ "Ford",
Make == "Toyota" ~ "Toyota",
TRUE ~ "Check"
))
# Check if grouping is correct
count(car_survey_data, car_survey_data$Make, car_survey_data$Parent, name = "Freq")
## car_survey_data$Make car_survey_data$Parent Freq
## 1 Buick General Motors 31
## 2 Chevrolet General Motors 64
## 3 Chrysler Chrysler 169
## 4 Dodge Chrysler 41
## 5 Fiat Chrysler 18
## 6 Ford Ford 202
## 7 Honda Honda 159
## 8 Kia Kia 34
## 9 Lincoln Ford 39
## 10 Toyota Toyota 292
# Count the number of occurrences for each Parent by Region
parent_region_count <- car_survey_data %>%
group_by(Region, Parent) %>%
summarise(Count = n())
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
Create a bar graph that shows the frequency of Toyota cars bought
per age group
ggplot(toyota_age_group_count, aes(x = Age_Group, y = Count, fill = Age_Group)) +
geom_bar(stat = "identity") +
geom_text(aes(label = Count), vjust = -0.5) +
labs(title = "Frequency of Toyota Cars Bought per Age Group", x = "Age Group", y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Count the number of Toyota cars bought per age group and region
toyota_age_region_count <- toyota_data %>%
group_by(Age_Group, Region) %>%
summarise(Count = n())
## `summarise()` has grouped output by 'Age_Group'. You can override using the
## `.groups` argument.
Create a bar graph that shows the frequency of Toyota cars bought
per age group and region
ggplot(toyota_age_region_count, aes(x = Age_Group, y = Count, fill = Region)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = Count), vjust = -0.5, position = position_dodge(width = 0.9)) +
labs(title = "Frequency of Toyota Cars Bought per Age Group and Region", x = "Age Group", y = "Count") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Different payment methods by region
# Define a mapping of numeric values to payment method labels
payment_method_labels <- c(
"1" = "Lease",
"2" = "Finance",
"3" = "Cash"
)
# Convert numeric payment method values to labels
toyota_data <- toyota_data %>%
mutate(Payment_Method_Label = recode(Pay_Meth, !!!payment_method_labels))
# Count the number of different payment methods by region
payment_method_count <- toyota_data %>%
group_by(Region, Payment_Method_Label) %>%
summarise(Count = n())
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
Create a bar graph that shows the frequency of different payment
methods by region
ggplot(payment_method_count, aes(x = Region, y = Count, fill = Payment_Method_Label)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = Count), vjust = -0.5, position = position_dodge(width = 0.9)) +
labs(title = "Method of Payments by Region for Toyota Cars", x = "Region", y = "Count", fill = "Payment Method") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Different types of insurance by region
# Display unique values in Insur_Type to identify invalid entries
unique(toyota_data$Insur_Type)
## [1] "Collision" "Liability" "Comprehensive" ""
# Define valid insurance types (example: replace these with actual valid types)
valid_insurance_types <- c("Liability", "Collision", "Comprehensive", "Uninsured Motorist")
# Filter out rows with invalid insurance types
toyota_data <- toyota_data %>%
filter(Insur_Type %in% valid_insurance_types)
# Count the number of different types of insurance by region
insurance_type_count <- toyota_data %>%
group_by(Region, Insur_Type) %>%
summarise(Count = n())
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
Create a bar graph that shows the frequency of different types of
insurance by region
ggplot(insurance_type_count, aes(x = Region, y = Count, fill = Insur_Type)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = Count), vjust = -0.5, position = position_dodge(width = 0.9)) +
labs(title = "Types of Insurance by Region for Toyota Cars", x = "Region", y = "Count", fill = "Insurance Type") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Different education levels by region
# Define a mapping of numeric values to education labels
education_labels <- c(
"1.0" = "High School/College Degree",
"2.0" = "Undergraduate—Bachelor’s Degree",
"3.0" = "Graduate—Master’s Degree"
)
# Convert numeric education values to labels
car_survey_data <- car_survey_data %>%
mutate(Education_Label = recode(Education, !!!education_labels))
# Count the number of different education levels by region
education_count <- car_survey_data %>%
group_by(Region, Education_Label) %>%
summarise(Count = n())
## `summarise()` has grouped output by 'Region'. You can override using the
## `.groups` argument.
Create a stacked bar graph that shows the frequency of different
education levels by region
ggplot(education_count, aes(x = Region, y = Count, fill = Education_Label)) +
geom_bar(stat = "identity", position = "stack") +
geom_text(aes(label = Count), position = position_stack(vjust = 0.5)) +
labs(title = "Frequency of Different Education Levels by Region", x = "Region", y = "Count", fill = "Education Level") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Correlation Analysis
# Load necessary libraries
library(ggplot2)
library(dplyr)
library(stringr)
library(reshape2)
library(GGally)
## Registered S3 method overwritten by 'GGally':
## method from
## +.gg ggplot2
# Create a Brand column to distinguish Toyota from other brands
car_survey_data <- car_survey_data %>%
mutate(Brand = ifelse(Make == "Toyota", "Toyota", "Other"))
# Merge Futu_Pur_1 and Futu_Pur_2 into a new column
car_survey_data <- car_survey_data %>%
mutate(Future_Purchase = rowMeans(select(., Futu_Pur_1, Futu_Pur_2), na.rm = TRUE))
# Merge WOM_1 and WOM_2 into a new column
car_survey_data <- car_survey_data %>%
mutate(WOM = rowMeans(select(., WOM_1, WOM_2), na.rm = TRUE))
# Merge Enj_1 and Enj_2 into a new column called Enjoyment
car_survey_data <- car_survey_data %>%
mutate(Enjoyment = rowMeans(select(., Enj_1, Enj_2), na.rm = TRUE))
# Filter data for Toyota brand
toyota_data <- car_survey_data %>% filter(Brand == "Toyota")
# Select relevant columns for correlation analysis
toyota_corr_data <- toyota_data %>% select(Future_Purchase, WOM, Enjoyment)
# Calculate correlation matrix for Toyota
toyota_corr_matrix <- cor(toyota_corr_data, use = "complete.obs")
# Melt the correlation matrix for plotting
toyota_corr_melt <- melt(toyota_corr_matrix)
# Filter data for other brands
other_brands_data <- car_survey_data %>% filter(Brand == "Other")
# Select relevant columns for correlation analysis
other_brands_corr_data <- other_brands_data %>% select(Future_Purchase, WOM, Enjoyment)
# Calculate correlation matrix for other brands
other_brands_corr_matrix <- cor(other_brands_corr_data, use = "complete.obs")
# Melt the correlation matrix for plotting
other_brands_corr_melt <- melt(other_brands_corr_matrix)
# Calculate correlation by region for Toyota
correlation_by_region_toyota <- toyota_data %>%
group_by(Region) %>%
summarise(
Future_Purchase_WOM = cor(Future_Purchase, WOM, use = "complete.obs"),
Future_Purchase_Enjoyment = cor(Future_Purchase, Enjoyment, use = "complete.obs"),
WOM_Enjoyment = cor(WOM, Enjoyment, use = "complete.obs")
)
# Melt the data for plotting
correlation_by_region_toyota_melt <- melt(correlation_by_region_toyota, id.vars = "Region")
Plot the correlation comparison by region for Toyota
ggplot(correlation_by_region_toyota_melt, aes(x = Region, y = value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Correlation Comparison by Region for Toyota",
x = "Region",
y = "Correlation Coefficient",
fill = "Variable Pair") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))

Plot the correlation comparison by region for other brands
# Calculate correlation by region for other brands
correlation_by_region_other <- other_brands_data %>%
group_by(Region) %>%
summarise(
Future_Purchase_WOM = cor(Future_Purchase, WOM, use = "complete.obs"),
Future_Purchase_Enjoyment = cor(Future_Purchase, Enjoyment, use = "complete.obs"),
WOM_Enjoyment = cor(WOM, Enjoyment, use = "complete.obs")
)
# Melt the data for plotting
correlation_by_region_other_melt <- melt(correlation_by_region_other, id.vars = "Region")
ggplot(correlation_by_region_other_melt, aes(x = Region, y = value, fill = variable)) +
geom_bar(stat = "identity", position = "dodge") +
labs(title = "Correlation Comparison by Region for Other Brands",
x = "Region",
y = "Correlation Coefficient",
fill = "Variable Pair") +
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1))
