library(readr)
## Warning: package 'readr' was built under R version 4.3.3
credit <- read_csv("C:/Users/hp/Desktop/dataset/Credit Card Customers/BankChurners.csv")
## Warning: One or more parsing issues, call `problems()` on your data frame for details,
## e.g.:
##   dat <- vroom(...)
##   problems(dat)
## Rows: 10127 Columns: 21
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr  (7): Attrition_Flag, Gender, Education_Level, Marital_Status, Income_Ca...
## dbl (14): CLIENTNUM, Customer_Age, Dependent_count, Months_on_book, Total_Re...
## 
## ℹ 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.
credit
## # A tibble: 10,127 × 21
##    CLIENTNUM Attrition_Flag  Customer_Age Gender Dependent_count Education_Level
##        <dbl> <chr>                  <dbl> <chr>            <dbl> <chr>          
##  1 768805383 Existing Custo…           45 M                    3 High School    
##  2 818770008 Existing Custo…           49 F                    5 Graduate       
##  3 713982108 Existing Custo…           51 M                    3 Graduate       
##  4 769911858 Existing Custo…           40 F                    4 High School    
##  5 709106358 Existing Custo…           40 M                    3 Uneducated     
##  6 713061558 Existing Custo…           44 M                    2 Graduate       
##  7 810347208 Existing Custo…           51 M                    4 Unknown        
##  8 818906208 Existing Custo…           32 M                    0 High School    
##  9 710930508 Existing Custo…           37 M                    3 Uneducated     
## 10 719661558 Existing Custo…           48 M                    2 Graduate       
## # ℹ 10,117 more rows
## # ℹ 15 more variables: Marital_Status <chr>, Income_Category <chr>,
## #   Card_Category <chr>, Months_on_book <dbl>, Total_Relationship_Count <dbl>,
## #   Months_Inactive_12_mon <dbl>, Contacts_Count_12_mon <dbl>,
## #   Credit_Limit <dbl>, Total_Revolving_Bal <dbl>, Avg_Open_To_Buy <dbl>,
## #   Total_Amt_Chng_Q4_Q1 <dbl>, Total_Trans_Amt <dbl>, Total_Trans_Ct <dbl>,
## #   Total_Ct_Chng_Q4_Q1 <dbl>, Avg_Utilization_Ratio <chr>
# Question 1 : What is the average monthly spending of customers on their credit cards?

average_monthly_spending <- mean(credit$Total_Trans_Amt)
cat("Average monthly spending of customers is : ",average_monthly_spending)
## Average monthly spending of customers is :  4404.086
# Question 2 : What is the highest credit limit among customers?

library('dplyr')
## Warning: package 'dplyr' was built under R version 4.3.3
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
highest_credit_limit <- max(credit$Credit_Limit)
cat("The highest credit limit among customers is : ",highest_credit_limit)
## The highest credit limit among customers is :  34516
# Question 3 : What are the distinct values for marital status among customers?

library('dplyr')
distinct_marital_status <- credit %>%
  distinct(Marital_Status)
  
distinct_marital_status
## # A tibble: 4 × 1
##   Marital_Status
##   <chr>         
## 1 Married       
## 2 Single        
## 3 Unknown       
## 4 Divorced
# Question 4 : How does the average monthly spending vary across different education levels?

library('dplyr')
avg_monthly_spending <- credit %>%
  group_by(Education_Level) %>%
  summarise(avg_monthly_spending = round(mean(Total_Trans_Amt),2))

avg_monthly_spending
## # A tibble: 7 × 2
##   Education_Level avg_monthly_spending
##   <chr>                          <dbl>
## 1 College                        4256.
## 2 Doctorate                      4193.
## 3 Graduate                       4418.
## 4 High School                    4404.
## 5 Post-Graduate                  4515.
## 6 Uneducated                     4484.
## 7 Unknown                        4420.
# Question 5 : What is the customer attrition rate of credit card customers?

attrition_rate <- mean(credit$Attrition_Flag == "Attrited Customer")
cat("Attrition Rate of credit card customers is : ",attrition_rate)
## Attrition Rate of credit card customers is :  0.1606596
# Question 6 : Which credit card products are the most popular among customers?

card_category_frequency <- table(credit$Card_Category)

sorted_data <- sort(card_category_frequency, decreasing = TRUE)
sorted_data
## 
##     Blue   Silver     Gold Platinum 
##     9436      555      116       20
# Question 7 : How many customers are there with a credit limit greater than $10,000?

library('dplyr')
customers_gt_10000 <- credit %>%
  filter(Credit_Limit > 10000) %>% select(Credit_Limit, everything())

customers_gt_10000
## # A tibble: 2,754 × 21
##    Credit_Limit CLIENTNUM Attrition_Flag    Customer_Age Gender Dependent_count
##           <dbl>     <dbl> <chr>                    <dbl> <chr>            <dbl>
##  1        12691 768805383 Existing Customer           45 M                    3
##  2        34516 810347208 Existing Customer           51 M                    4
##  3        29081 818906208 Existing Customer           32 M                    0
##  4        22352 710930508 Existing Customer           37 M                    3
##  5        11656 719661558 Existing Customer           48 M                    2
##  6        11751 710599683 Existing Customer           56 M                    1
##  7        30367 709967358 Existing Customer           48 M                    4
##  8        13535 753327333 Existing Customer           41 M                    3
##  9        14470 709327383 Existing Customer           45 F                    2
## 10        20979 806165208 Existing Customer           47 M                    1
## # ℹ 2,744 more rows
## # ℹ 15 more variables: Education_Level <chr>, Marital_Status <chr>,
## #   Income_Category <chr>, Card_Category <chr>, Months_on_book <dbl>,
## #   Total_Relationship_Count <dbl>, Months_Inactive_12_mon <dbl>,
## #   Contacts_Count_12_mon <dbl>, Total_Revolving_Bal <dbl>,
## #   Avg_Open_To_Buy <dbl>, Total_Amt_Chng_Q4_Q1 <dbl>, Total_Trans_Amt <dbl>,
## #   Total_Trans_Ct <dbl>, Total_Ct_Chng_Q4_Q1 <dbl>, …
# Question 8 : Filter the data to include customers with a credit limit greater than $5000 and calculate the mean customer age for each income category.

library('dplyr')

filtered_data <- credit %>% filter(Credit_Limit > 5000) %>% group_by(Income_Category) %>% summarise(Mean_customer_age = round(mean(Customer_Age))) 

filtered_data 
## # A tibble: 6 × 2
##   Income_Category Mean_customer_age
##   <chr>                       <dbl>
## 1 $120K +                        48
## 2 $40K - $60K                    46
## 3 $60K - $80K                    46
## 4 $80K - $120K                   46
## 5 Less than $40K                 46
## 6 Unknown                        46
# Question 9 : What is the distribution of months of inactivity (Months_Inactive_12_mon) across different clients?

library('tidyr')
inactive_months <- credit %>%
  select(CLIENTNUM, Months_Inactive_12_mon) %>%
  gather(key = "Months Activity", value = "Value", -CLIENTNUM)

inactive_months 
## # A tibble: 10,127 × 3
##    CLIENTNUM `Months Activity`      Value
##        <dbl> <chr>                  <dbl>
##  1 768805383 Months_Inactive_12_mon     1
##  2 818770008 Months_Inactive_12_mon     1
##  3 713982108 Months_Inactive_12_mon     1
##  4 769911858 Months_Inactive_12_mon     4
##  5 709106358 Months_Inactive_12_mon     1
##  6 713061558 Months_Inactive_12_mon     1
##  7 810347208 Months_Inactive_12_mon     1
##  8 818906208 Months_Inactive_12_mon     2
##  9 710930508 Months_Inactive_12_mon     2
## 10 719661558 Months_Inactive_12_mon     3
## # ℹ 10,117 more rows
# Question 10 : What is the lifetime value of credit card customers?

# Calculate lifetime value for a one-year time frame
# Assuming revenue = total transaction amount
lifetime_value <- sum(credit$Total_Trans_Amt)
cat("Lifetime Value of credit card customers : ",lifetime_value)
## Lifetime Value of credit card customers :  44600182
# Question 11 : What is the distribution of customer ages in the dataset?
library('ggplot2')

ggplot(credit, aes(x = Customer_Age)) + 
  geom_histogram(binwidth = 5, fill = "lightgreen", color = "black") +
    labs(title = "Distribution of Customer Ages",
         x = "Age",
         y = "Frequency")

# Question 12 : How does the length of time a customer(gender wise) has been with the bank relate to their credit card usage?

library('ggplot2')

ggplot(credit, aes(x = Months_on_book, y = Total_Trans_Amt, col = Gender)) +
  geom_point() +
  labs(title = "Relationship Between Length of Time as Customer and Credit Card Usage",
       x = "Months on Book",
       y = "Total Transaction Amount")

# Question 13 : What is the distribution of credit card customers by income level and gender?

library('ggplot2')

ggplot(credit, aes(x = Income_Category, fill = Gender)) +
  geom_bar(position = "stack") +
  labs(title = "Distribution of Credit Card Customers by Income Level and Gender",
       x = "Income Level",
       y = "Count") +
  scale_fill_manual(labels = c("Female","Male"),
                    values = c("skyblue", "blue")) 

# Question 14 : What is the distribution of customers across different income categories?

pie(table(credit$Income_Category),
    main = "Distribution of customers across different income categories",
    labels = unique(credit$Income_Category),
    col = rainbow(length(unique(credit$Income_Category))))

# Question 15 : What is the distribution of education levels among individuals in the dataset?

library('ggplot2')

education_counts <- credit %>%
  count(Education_Level) %>%
  arrange(desc(n))


ggplot(education_counts, aes(x = reorder(Education_Level, n), y = n)) +
  geom_bar(stat = "identity", fill = rainbow(length(unique(credit$Education_Level)))) +
  labs(title = "Distribution of Education Levels",
       x = "Education Level",
       y = "Count")

# Question 16 : How does the total transaction amount influence the credit limits assigned to customers?

library('dplyr')
library('ggplot2')

credit_data <- sample_n(credit,1000) %>% 
  select(Credit_Limit, Customer_Age, Income_Category, Total_Trans_Amt)

ggplot(credit_data, aes(y = Credit_Limit, x = Total_Trans_Amt)) + geom_point() + geom_smooth(method = "lm")
## `geom_smooth()` using formula = 'y ~ x'

# Question 17 : How does the distribution of total transaction amounts vary across different card categories?

library('ggplot2')

ggplot(credit, aes(x = Card_Category, y = Total_Trans_Amt)) +
  geom_boxplot(fill = c("blue","gold","purple","gray"), color = "black") +
  labs(title = "Distribution of Total Transaction Amounts Across Card Categories",
       x = "Card Category",
       y = "Total Transaction Amount")

# Question 18 : What is the correlation between different customer attributes such as age, credit limit, total transaction amount, dependency count?

library('corrplot')
## Warning: package 'corrplot' was built under R version 4.3.3
## corrplot 0.92 loaded
correlation <- cor(credit[, c("Customer_Age", "Credit_Limit", "Total_Trans_Amt","Dependent_count")], method = "pearson")

corrplot(correlation, method = "number")

# Question 19 : What are the relationships and patterns among attrition flag, customer age, and gender in the credit card dataset?

library('GGally')
## Warning: package 'GGally' was built under R version 4.3.3
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
ggpairs(credit, columns = c("Attrition_Flag","Customer_Age","Gender"), aes(color = "Income_Category"))
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

# Question 20 : How does a customer's total transaction count influence their total transaction amount? 

library('dplyr')

credit_data1 <- credit %>% 
  select(Total_Trans_Amt, Total_Trans_Ct)

lm_models <- lm(Total_Trans_Amt ~ Total_Trans_Ct, data = credit_data1)

new_total_trans_ct <- data.frame(Total_Trans_Ct = c(100, 45, 76))  
predict(lm_models, new_total_trans_ct) %>% round(2)
##       1       2       3 
## 8509.40 2084.13 5705.65
# Question 21 : What is the standard deviation of customer ages in the dataset?

library('dplyr')

age_std_dev <- sd(credit$Customer_Age)
cat("Standard Deviation of customer age is : ",age_std_dev)
## Standard Deviation of customer age is :  8.016814