# Libraries
library(gridExtra)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.4
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.0
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::combine() masks gridExtra::combine()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(lubridate)
library(BSDA)
## Loading required package: lattice
##
## Attaching package: 'BSDA'
##
## The following object is masked from 'package:datasets':
##
## Orange
library(MASS)
##
## Attaching package: 'MASS'
##
## The following object is masked from 'package:dplyr':
##
## select
library(rstatix)
##
## Attaching package: 'rstatix'
##
## The following object is masked from 'package:MASS':
##
## select
##
## The following object is masked from 'package:stats':
##
## filter
# Dataset chosen for the project
# https://www.kaggle.com/datasets/suraj520/car-sales-data/code
options(scipen=5)
# Creating a link to the working directory
setwd("C:/Users/ilove/OneDrive/Desktop/project")
set.seed(6668)
rm(list=ls())
# Creating object for the sales data data
vehicle_sales <- read.csv("car_sales_data.csv")
vehicle_sales_backup <- vehicle_sales
vehicle_sales_head <- head(vehicle_sales, 25)
vehicle_sales_head
## Date Salesperson Customer.Name Car.Make Car.Model Car.Year
## 1 2022-08-01 Monica Moore MD Mary Butler Nissan Altima 2018
## 2 2023-03-15 Roberto Rose Richard Pierce Nissan F-150 2016
## 3 2023-04-29 Ashley Ramos Sandra Moore Ford Civic 2016
## 4 2022-09-04 Patrick Harris Johnny Scott Ford Altima 2013
## 5 2022-06-16 Eric Lopez Vanessa Jones Honda Silverado 2022
## 6 2022-12-18 Terry Perkins MD John Olsen Ford Altima 2015
## 7 2022-06-12 Ashley Brown Tyler Lawson Honda F-150 2013
## 8 2022-06-20 Norma Watkins Michael Bond Ford Altima 2015
## 9 2022-09-02 Scott Parker Stephanie Smith Ford Corolla 2021
## 10 2023-04-06 Andrew Smith Ashley Moreno DDS Ford Civic 2018
## 11 2023-03-12 Harold Nelson Isaac Patton Honda Silverado 2021
## 12 2023-01-29 Richard Richardson Justin Gray Toyota Silverado 2022
## 13 2022-05-29 Mia Acosta Rebecca Best Ford Altima 2017
## 14 2022-08-12 Lisa Campbell Lisa Anderson Toyota F-150 2011
## 15 2023-04-06 Carrie Howard Rodney Black Chevrolet Altima 2021
## 16 2022-12-21 James Velasquez Natalie Thompson Honda Corolla 2016
## 17 2022-08-12 Kristin Holmes Michael Hill Nissan Altima 2010
## 18 2022-08-23 Amy Solomon Taylor Moore Nissan Silverado 2018
## 19 2022-10-18 William Williams Jordan Taylor Toyota Civic 2013
## 20 2023-03-09 Patricia Reynolds Alison Walker Ford Altima 2014
## 21 2022-09-28 Michelle Gonzalez Juan Davies Ford Corolla 2014
## 22 2022-12-20 Jennifer Morris Kristin Robinson Toyota Civic 2011
## 23 2022-07-26 Jeremiah Thompson Clifford Evans Nissan Altima 2013
## 24 2022-07-18 Matthew White Nancy Martinez Chevrolet Altima 2020
## 25 2022-12-19 Jason Riley Ruben Gomez Nissan Civic 2010
## Sale.Price Commission.Rate Commission.Earned
## 1 15983 0.07049523 1126.73
## 2 38474 0.13443884 5172.40
## 3 33340 0.11453592 3818.63
## 4 41937 0.09219072 3866.20
## 5 20256 0.11348979 2298.85
## 6 14769 0.07724694 1140.86
## 7 41397 0.14278012 5910.67
## 8 46233 0.07162376 3311.38
## 9 27337 0.09950365 2720.13
## 10 16309 0.14992578 2445.14
## 11 41259 0.09254116 3818.16
## 12 48224 0.09059215 4368.72
## 13 36409 0.12234885 4454.60
## 14 29628 0.11232629 3328.00
## 15 10313 0.10387489 1071.26
## 16 36607 0.05525098 2022.57
## 17 39916 0.05200777 2075.94
## 18 18482 0.08618532 1592.88
## 19 17158 0.12722780 2182.97
## 20 28796 0.09345121 2691.02
## 21 24128 0.08478088 2045.59
## 22 36895 0.12828811 4733.19
## 23 16797 0.14423733 2422.75
## 24 30184 0.13699119 4134.94
## 25 41587 0.08440441 3510.13
vehicle_sales_tail <- tail(vehicle_sales, 25)
vehicle_sales_tail
## Date Salesperson Customer.Name Car.Make
## 2499976 2022-08-28 Julie Franklin Andrew Schroeder Toyota
## 2499977 2022-09-21 Mr. Calvin Hardin Wesley Thompson Honda
## 2499978 2023-03-10 Justin Knight John Woods Chevrolet
## 2499979 2022-12-06 Stephen Simpson Morgan Smith Chevrolet
## 2499980 2023-04-12 Shawn Park Anna Callahan Honda
## 2499981 2023-03-21 Jesse Thomas Holly Evans Honda
## 2499982 2022-05-09 Charles Miller Donald Watkins PhD Ford
## 2499983 2022-09-28 Jeffrey Swanson John Oneal Ford
## 2499984 2022-05-07 Alexandra Charles Rebecca Mata Nissan
## 2499985 2023-01-28 Diana Shelton Nathan Morales Chevrolet
## 2499986 2022-10-02 Jennifer Valentine Whitney Farrell Toyota
## 2499987 2022-08-03 John Davis Erin Nguyen Toyota
## 2499988 2023-02-14 Amanda Chan Mindy Evans Ford
## 2499989 2023-02-27 April Figueroa Natalie Wagner Chevrolet
## 2499990 2022-08-20 Jacob Contreras James Obrien Toyota
## 2499991 2022-12-22 Martha Snyder Mrs. Suzanne Glenn PhD Chevrolet
## 2499992 2022-10-31 Julie Conner Michelle Torres Chevrolet
## 2499993 2023-04-27 John Alvarado Kyle Brennan Toyota
## 2499994 2022-07-28 Nicholas Silva Kyle Garcia Chevrolet
## 2499995 2022-07-04 Matthew Townsend Nicole Clay Chevrolet
## 2499996 2022-05-26 Isabella Moore Shirley Lee Chevrolet
## 2499997 2022-10-03 Kimberly Snow Tara Rodgers Ford
## 2499998 2022-06-07 Jessica Young Jennifer Moore Chevrolet
## 2499999 2023-02-15 Donald Barber Ashley Diaz Honda
## 2500000 2023-03-24 Kayla Fowler Nathan Thompson Honda
## Car.Model Car.Year Sale.Price Commission.Rate Commission.Earned
## 2499976 Silverado 2011 29951 0.07779262 2329.97
## 2499977 F-150 2013 30189 0.14876730 4491.14
## 2499978 Silverado 2019 38915 0.07633102 2970.42
## 2499979 Silverado 2011 38884 0.12075330 4695.37
## 2499980 Altima 2021 31401 0.07780853 2443.27
## 2499981 Altima 2015 40685 0.07762826 3158.31
## 2499982 Civic 2010 27974 0.07823584 2188.57
## 2499983 Altima 2016 36585 0.14972266 5477.60
## 2499984 Silverado 2022 15486 0.07850651 1215.75
## 2499985 F-150 2012 24593 0.09082198 2233.58
## 2499986 Altima 2011 30595 0.05584857 1708.69
## 2499987 Altima 2011 14276 0.07945348 1134.28
## 2499988 F-150 2017 19093 0.13496487 2576.88
## 2499989 Altima 2013 28612 0.10371195 2967.41
## 2499990 Silverado 2014 23190 0.06789420 1574.47
## 2499991 Silverado 2018 45155 0.09400713 4244.89
## 2499992 Silverado 2012 18241 0.09579355 1747.37
## 2499993 Corolla 2013 38427 0.08491244 3262.93
## 2499994 Civic 2012 42240 0.06922977 2924.27
## 2499995 Civic 2022 49338 0.06782602 3346.40
## 2499996 Silverado 2021 49823 0.06297701 3137.70
## 2499997 F-150 2022 18803 0.06833853 1284.97
## 2499998 Civic 2010 30863 0.08891529 2744.19
## 2499999 Silverado 2014 26125 0.08826030 2305.80
## 2500000 Civic 2010 20762 0.13710470 2846.57
qqnorm(vehicle_sales$Sale.Price)
qqline(vehicle_sales$Sale.Price, col="red")

# Looking at the data
str(vehicle_sales)
## 'data.frame': 2500000 obs. of 9 variables:
## $ Date : chr "2022-08-01" "2023-03-15" "2023-04-29" "2022-09-04" ...
## $ Salesperson : chr "Monica Moore MD" "Roberto Rose" "Ashley Ramos" "Patrick Harris" ...
## $ Customer.Name : chr "Mary Butler" "Richard Pierce" "Sandra Moore" "Johnny Scott" ...
## $ Car.Make : chr "Nissan" "Nissan" "Ford" "Ford" ...
## $ Car.Model : chr "Altima" "F-150" "Civic" "Altima" ...
## $ Car.Year : int 2018 2016 2016 2013 2022 2015 2013 2015 2021 2018 ...
## $ Sale.Price : int 15983 38474 33340 41937 20256 14769 41397 46233 27337 16309 ...
## $ Commission.Rate : num 0.0705 0.1344 0.1145 0.0922 0.1135 ...
## $ Commission.Earned: num 1127 5172 3819 3866 2299 ...
summary(vehicle_sales)
## Date Salesperson Customer.Name Car.Make
## Length:2500000 Length:2500000 Length:2500000 Length:2500000
## Class :character Class :character Class :character Class :character
## Mode :character Mode :character Mode :character Mode :character
##
##
##
## Car.Model Car.Year Sale.Price Commission.Rate
## Length:2500000 Min. :2010 Min. :10000 Min. :0.05000
## Class :character 1st Qu.:2013 1st Qu.:20019 1st Qu.:0.07496
## Mode :character Median :2016 Median :30006 Median :0.10001
## Mean :2016 Mean :30012 Mean :0.09999
## 3rd Qu.:2019 3rd Qu.:40022 3rd Qu.:0.12501
## Max. :2022 Max. :50000 Max. :0.15000
## Commission.Earned
## Min. : 501.3
## 1st Qu.:1821.7
## Median :2741.9
## Mean :3001.0
## 3rd Qu.:3978.1
## Max. :7494.5
head(vehicle_sales)
## Date Salesperson Customer.Name Car.Make Car.Model Car.Year
## 1 2022-08-01 Monica Moore MD Mary Butler Nissan Altima 2018
## 2 2023-03-15 Roberto Rose Richard Pierce Nissan F-150 2016
## 3 2023-04-29 Ashley Ramos Sandra Moore Ford Civic 2016
## 4 2022-09-04 Patrick Harris Johnny Scott Ford Altima 2013
## 5 2022-06-16 Eric Lopez Vanessa Jones Honda Silverado 2022
## 6 2022-12-18 Terry Perkins MD John Olsen Ford Altima 2015
## Sale.Price Commission.Rate Commission.Earned
## 1 15983 0.07049523 1126.73
## 2 38474 0.13443884 5172.40
## 3 33340 0.11453592 3818.63
## 4 41937 0.09219072 3866.20
## 5 20256 0.11348979 2298.85
## 6 14769 0.07724694 1140.86
# Keep complete cases only
vehicle_sales[!complete.cases(vehicle_sales)]
## data frame with 0 columns and 2500000 rows
# Remove personal information
vehicle_sales$Salesperson <- NULL
vehicle_sales$Customer.Name <- NULL
# transforming / reformatting the variables
vehicle_sales$Date <- ymd(vehicle_sales$Date)
vehicle_sales$Car.Make <- as.factor(vehicle_sales$Car.Make)
vehicle_sales$Car.Model <- as.factor(vehicle_sales$Car.Model)
vehicle_sales$Car.Year <- as.factor(vehicle_sales$Car.Year)
# Exploratory data analysis
q1 <- qplot(vehicle_sales$Car.Make, main= "Car Make", xlab="Make", ylab="Count")
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
q2 <- qplot(vehicle_sales$Car.Model, main="Car Model", xlab="Model", ylab="Count")
q3 <- qplot(vehicle_sales$Car.Year, main="Car Year", xlab="Year", ylab="Count")
q4 <- qplot(vehicle_sales$Date, vehicle_sales$Commission.Earned, main="Commission Earned Over Time", xlab="Date", ylab="Commission Earned")
g1 <- ggplot(vehicle_sales, aes(x=Sale.Price, y=Commission.Earned)) +
labs(x="Sale Price", y="Commission Earned") + geom_boxplot()
g2 <- ggplot(vehicle_sales, aes(x=Sale.Price, y=Commission.Rate)) +
labs(x="Sale Price", y="Commission Rate") + geom_boxplot()
g3 <- ggplot(vehicle_sales, aes(x=Commission.Rate, y=Commission.Earned))+
labs(x="Commission Rate", y="Commission Earned") + geom_point()
g4 <- ggplot(vehicle_sales, aes(x=Date))+
labs(x="Date", y="Count")+geom_histogram()
grid.arrange(q1, q2, q3, q4, ncol=2)

grid.arrange(g1, g2, g3, g4, ncol=2)
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Scale the data
numeric <- c("Sale.Price", "Commission.Rate", "Commission.Earned")
vehicle_sales[numeric] <- scale(vehicle_sales[numeric])
# Remove outliers
quart <- quantile(vehicle_sales$Commission.Earned, probs=c(.24, .74))
inter_quart_range <- IQR(vehicle_sales$Commission.Earned)
lwr <- quart[1] - 1.5*inter_quart_range
upr <- quart[2] + 1.5*inter_quart_range
data_no_outlier <- subset(vehicle_sales, Commission.Earned > lwr & Commission.Earned < upr)
dim(data_no_outlier)
## [1] 2494930 7
ggplot(data_no_outlier, aes(x=Sale.Price, y=Commission.Earned)) + geom_boxplot()
## Warning: Continuous x aesthetic
## ℹ did you forget `aes(group = ...)`?

# from the mean commission, find values above and below it
avg_commission <- mean(data_no_outlier$Commission.Earned)
data_no_outlier$commission_level <- ifelse(data_no_outlier$Commission.Earned > avg_commission,
"Greater Avg Comm.", "Less Avg Comm.")
# Get the middle value of the sales price data
sale_price_middle <- median(data_no_outlier$Sale.Price)
# divide into high/low sale price groups
higher_saleprice_group <- data_no_outlier[data_no_outlier$Sale.Price > sale_price_middle, ]
lower_saleprice_group <- data_no_outlier[data_no_outlier$Sale.Price <= sale_price_middle, ]
# lets test if the commission earned is higher amongst higher sale price vehicles
# Ho : There is no difference between average commission earned between lower sales and higher sale price cars
# avg commission for each group
avg_high_commission <- mean(higher_saleprice_group$Commission.Earned)
avg_low_commission <- mean(lower_saleprice_group$Commission.Earned)
# std dev of each group
sd_commission_high <- sd(higher_saleprice_group$Commission.Earned)
sd_commission_low <- sd(lower_saleprice_group$Commission.Earned)
# Calculate number of observations in each group
qty_higher_price <- nrow(higher_saleprice_group)
qty_lower_price <- nrow(lower_saleprice_group)
# Perform z-test
z_score <- (avg_high_commission - avg_low_commission) / sqrt(sd_commission_high^2/qty_higher_price + sd_commission_low^2/qty_lower_price)
p_value <- 2 * pnorm(-abs(z_score))
# Output results
z_score
## [1] 1446.795
p_value
## [1] 0
# Wilcoxon test
# We are using unpaired, two-sample because we have two groups and it is two directional.
statistical_test_wilcoxon <- wilcox.test(lower_saleprice_group$Commission.Earned, higher_saleprice_group$Commission.Earned)
statistical_test_wilcoxon <- add_significance(statistical_test_wilcoxon)
# View the results
statistical_test_wilcoxon
##
## Wilcoxon rank sum test with continuity correction
##
## data: lower_saleprice_group$Commission.Earned and higher_saleprice_group$Commission.Earned
## W = 150931266630, p-value < 2.2e-16
## alternative hypothesis: true location shift is not equal to 0