library(readr)
library(tidyr)
library(tidyverse)
## Warning: package 'ggplot2' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ purrr     1.0.2
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ 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(ggplot2)
library(dplyr)
library(GGally)
## Warning: package 'GGally' was built under R version 4.3.3
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(corrplot)
## Warning: package 'corrplot' was built under R version 4.3.3
## corrplot 0.92 loaded
library(ggpubr)
## Warning: package 'ggpubr' was built under R version 4.3.3
library(gridExtra)
## Warning: package 'gridExtra' was built under R version 4.3.3
## 
## Attaching package: 'gridExtra'
## 
## The following object is masked from 'package:dplyr':
## 
##     combine
customer_data <- read_csv("C:/Users/hp/OneDrive/Desktop/customer_data.csv")
## Rows: 16590 Columns: 12
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (6): gender, education, region, loyalty_status, purchase_frequency, prod...
## dbl (6): id, age, income, purchase_amount, promotion_usage, satisfaction_score
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.

Exploratory Analysis

Q1-Sorting the data as per the region and Gender

sortdata<-customer_data %>% arrange(region)
REGION<-sortdata %>% group_by(region) %>% summarise(count=n())

# Filtering TOTAL NUMBERS OF MALES
Males<-sortdata %>%filter(gender=="Male")

# Filtering TOTAL NUMBERS OF FEMALES
Females<-sortdata %>% filter(gender=="Female")

Q2-Find the Average Purchase amount of each group based on their occupation

sortdata %>% group_by(education) %>% summarise("average"=mean(purchase_amount))
## # A tibble: 4 × 2
##   education  average
##   <chr>        <dbl>
## 1 Bachelor     9733.
## 2 College      9674.
## 3 HighSchool   9526.
## 4 Masters      9526.

Q3- Find the Variation in the Education Level among the students

sortdata %>% group_by(education) %>% summarise("Count"=n())
## # A tibble: 4 × 2
##   education  Count
##   <chr>      <int>
## 1 Bachelor    4974
## 2 College     6545
## 3 HighSchool  3395
## 4 Masters     1676

Q4-Grouping and Counting the Customers based on their region and finding the reason with the max number of customers

regmax<-sortdata %>% group_by(region) %>% summarise("Count"=n())
data.frame(regmax) %>% filter(Count==max(Count))
##   region Count
## 1   West  5038

Visualisation

Q5-Visualization of the category of products which are purchased by each gender

ggplot(sortdata,aes(x=gender,fill=product_category))+geom_bar()+
  labs(title = "Products purchased by Gender",xlab="Gender",ylab="Count")

Q6- What is the Average purchase amount per education category

avg_edu<-sortdata %>% group_by(education) %>% 
  summarise("Average"=mean(purchase_amount,na.rm = TRUE))

avg_edu<-data.frame(avg_edu)

ggplot(avg_edu, aes(x = education, y = Average)) +
  geom_bar(stat = "identity", fill = "skyblue") +
  labs(title = "Average Education", x = "Education Level", y = "Average Amount") +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Q7- What is the Purchasing range of the customers based on status

ggplot(data =sortdata,aes(y=education,fill=loyalty_status)) +
  geom_bar()+labs(title = "Loyalty meter",y="Count")

Q8- How many of frequent Purchases by people in all the regions

frequentin_region<-customer_data%>%filter(region=="West" |region=="North" |region=="South" |
                                            region=="East",purchase_frequency=="frequent")
ggplot(data=frequentin_region,aes(y=region,fill=factor(education)))+geom_bar()+
  labs(title="Frequent Purchasing bar ",x="Total Orders",y="Regions")

Q9- What is the Purchase frequency that are done by regions

ggplot(data=customer_data,aes(y=region,fill=purchase_frequency))+geom_bar()+
  labs(title="Purchase frequency accross Regions",x="Total Orders",y="Regions")

Q10- How many Purchase that are done by regions based on education level

ggplot(data=sortdata,aes(y=region,fill=factor(education)))+geom_bar()+
  labs(title="Purchasing bar ",x="Total Orders",y="Regions")

Q11- What is the loyalty status of people accross different purchase frequencies

ggplot(data =sortdata,aes(y=purchase_frequency,fill=loyalty_status)) +
  geom_bar()+labs(title = "Loyalty meter",y="Frequent")

Q12- What is the distribution of education level

tab<-table(sortdata$education)
colr<-c("Bachelor","HighSchool","Masters","College")
pct <- round(tab/sum(tab)*100)
pie(tab,
    main = "Education Distribution",
    labels=paste(colr,paste(pct,"%",sep = " "),sep=" "),
    col=rainbow(length(colr)))

Q13- What is the total distribution of Products

PRO<-table(sortdata$product_category)
colr<-c("Books","clothing","Electronics","Health","Food")
pct <- round(PRO/sum(PRO)*100)
pie(PRO,
    main = "Product Distribution",
    labels=paste(colr,paste(pct,"%",sep = " "),sep=" "),
    col=rainbow(length(colr)))

Q14-Visualization of how much most of the customers spend on purchase

meanpur<-sortdata %>% summarise("avg"=mean(purchase_amount))
ggplot(data=sortdata,aes(x=(purchase_amount)))+geom_histogram(color="white",fill="blue")+
  geom_vline(aes(xintercept=meanpur$avg,color="red",linewidth=0.01))+
  labs(title = "Purchase amount ",x="Income")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

Q15-Check if higher income relates to higher spending on purchases

cor.test(sortdata$income,sortdata$purchase_amount)
## 
##  Pearson's product-moment correlation
## 
## data:  sortdata$income and sortdata$purchase_amount
## t = 380.48, df = 16588, p-value < 2.2e-16
## alternative hypothesis: true correlation is not equal to 0
## 95 percent confidence interval:
##  0.9456159 0.9487454
## sample estimates:
##       cor 
## 0.9472032
ggscatter(sortdata,
          x="income",
          y="purchase_amount",
          cor.coef=TRUE,
          conf.int=TRUE,
          add="reg.line",
          cor.method="pearson",
          xlab="Income",
          ylab="Purchase Amount")

Q16- Bar Graph depicting the product categories as per the region

ggplot(data=sortdata,aes(x=region,fill=product_category))+geom_bar()+
  labs(title="Product categories as per the regions",
       x="Region")

Q17- Corrplot of correlation between the purchase_amount,promotion_usage,satisfaction_score

cor_akt<-sortdata %>% select(income,purchase_amount)
cus_cor_plot2<-cor(cor_akt)
corrplot(cus_cor_plot2)

Q18 Summary of the Dataset

summary(sortdata)
##        id             age        gender              income     
##  Min.   :    1   Min.   :13   Length:16590       Min.   : 5000  
##  1st Qu.: 4148   1st Qu.:27   Class :character   1st Qu.:16303  
##  Median : 8296   Median :30   Mode  :character   Median :27694  
##  Mean   : 8296   Mean   :30                      Mean   :27580  
##  3rd Qu.:12443   3rd Qu.:33                      3rd Qu.:38995  
##  Max.   :16590   Max.   :47                      Max.   :49994  
##   education            region          loyalty_status     purchase_frequency
##  Length:16590       Length:16590       Length:16590       Length:16590      
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##  purchase_amount product_category   promotion_usage  satisfaction_score
##  Min.   : 1118   Length:16590       Min.   :0.0000   Min.   :1.000     
##  1st Qu.: 5608   Class :character   1st Qu.:0.0000   1st Qu.:4.000     
##  Median : 9500   Mode  :character   Median :0.0000   Median :5.000     
##  Mean   : 9647                      Mean   :0.2991   Mean   :4.999     
##  3rd Qu.:13346                      3rd Qu.:1.0000   3rd Qu.:6.000     
##  Max.   :24147                      Max.   :1.0000   Max.   :9.000