credit_data = read.csv(("/Users/tbs/Downloads/Data Sets 'R'/credit_data.csv"))

R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(ggplot2)

PART - 1

library(dplyr)
## 
## 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
# Group by 'Category1'
grouped_data_1 <- credit_data %>%
  group_by(City) %>%
  summarise(
    Mean_Value = mean(Loan.Tenure),
    Median_Value = median(Loan.Tenure)
  )
print(grouped_data_1)
## # A tibble: 23 × 3
##    City            Mean_Value Median_Value
##    <chr>                <dbl>        <dbl>
##  1 Ahmedabad             134.          102
##  2 Bengaluru             136.          102
##  3 Bishanpura            135.          101
##  4 Channarayapatna       133.          101
##  5 Chennai               136.          102
##  6 Coimbatore            133.          101
##  7 Dhulagori             131.           99
##  8 Hyderabad             134.          101
##  9 Jaipur                131.           99
## 10 Kanpur                135.          101
## # ℹ 13 more rows
ggplot(grouped_data_1, aes(City,Median_Value)) + geom_point()+ labs(title = "Credit Data", 'City','Loan.Tenure')+theme(axis.text.x = element_text(angle = 90))

Calculating expected probability for group 1

grouped_data_1 <- grouped_data_1 %>%
 mutate(expected_problty = (Mean_Value/ sum(Mean_Value)))
print(grouped_data_1)
## # A tibble: 23 × 4
##    City            Mean_Value Median_Value expected_problty
##    <chr>                <dbl>        <dbl>            <dbl>
##  1 Ahmedabad             134.          102           0.0438
##  2 Bengaluru             136.          102           0.0444
##  3 Bishanpura            135.          101           0.0439
##  4 Channarayapatna       133.          101           0.0434
##  5 Chennai               136.          102           0.0442
##  6 Coimbatore            133.          101           0.0433
##  7 Dhulagori             131.           99           0.0429
##  8 Hyderabad             134.          101           0.0437
##  9 Jaipur                131.           99           0.0427
## 10 Kanpur                135.          101           0.0439
## # ℹ 13 more rows

Assigning ‘anomaly’ tag to the lowest probability group

anomaly_1 <- grouped_data_1 %>%
  filter(expected_problty == min(expected_problty)) %>%
  pull(City)

print(anomaly_1)
## [1] "Mumbai"

Assigning the lowest probability group an “anomaly” tag and Translating anomaly back into your original data frame i.e data_1

data_1 <- grouped_data_1 %>%
  mutate(anomaly_1 = ifelse(City == anomaly_1, "Anomaly", "Normal"))
print(data_1)
## # A tibble: 23 × 5
##    City            Mean_Value Median_Value expected_problty anomaly_1
##    <chr>                <dbl>        <dbl>            <dbl> <chr>    
##  1 Ahmedabad             134.          102           0.0438 Normal   
##  2 Bengaluru             136.          102           0.0444 Normal   
##  3 Bishanpura            135.          101           0.0439 Normal   
##  4 Channarayapatna       133.          101           0.0434 Normal   
##  5 Chennai               136.          102           0.0442 Normal   
##  6 Coimbatore            133.          101           0.0433 Normal   
##  7 Dhulagori             131.           99           0.0429 Normal   
##  8 Hyderabad             134.          101           0.0437 Normal   
##  9 Jaipur                131.           99           0.0427 Normal   
## 10 Kanpur                135.          101           0.0439 Normal   
## # ℹ 13 more rows

Calculating the frequency of each combination

combination_counts <- credit_data %>%
  group_by(City, Loan.Tenure) %>%
  summarise(count = n(), .groups = 'drop')
print(combination_counts)
## # A tibble: 7,990 × 3
##    City      Loan.Tenure count
##    <chr>           <int> <int>
##  1 Ahmedabad          12    53
##  2 Ahmedabad          13    58
##  3 Ahmedabad          14    37
##  4 Ahmedabad          15    49
##  5 Ahmedabad          16    57
##  6 Ahmedabad          17    53
##  7 Ahmedabad          18    64
##  8 Ahmedabad          19    68
##  9 Ahmedabad          20    48
## 10 Ahmedabad          21    61
## # ℹ 7,980 more rows

PART - 2

library(dplyr)
# Group by 'Category2'
grouped_data_2 <- credit_data %>%
  group_by(State) %>%
  summarise(
    Mean_Value = mean(LTV.Ratio),
    Median_Value = median(LTV.Ratio)
  )
print(grouped_data_2)
## # A tibble: 10 × 3
##    State         Mean_Value Median_Value
##    <chr>              <dbl>        <dbl>
##  1 Delhi               71.9         72.4
##  2 Gujarat             71.6         72.2
##  3 Karnataka           71.5         72.0
##  4 Kerala              71.6         72.2
##  5 Maharashtra         71.5         72.1
##  6 Rajasthan           71.5         71.7
##  7 Tamil Nadu          71.4         71.6
##  8 Telangana           71.9         72.4
##  9 Uttar Pradesh       71.9         72.5
## 10 West Bengal         71.7         72.2
ggplot(grouped_data_2, aes(State,Median_Value)) + geom_col(fill='orange')+ labs(title = "Credit Data", 'State','LTV.Ratio')+theme(axis.text.x = element_text(angle = 90))

Calculating expected probability for group 2

grouped_data_2 <- grouped_data_2 %>%
 mutate(expected_problty = (Mean_Value/ sum(Mean_Value)))
print(grouped_data_2)
## # A tibble: 10 × 4
##    State         Mean_Value Median_Value expected_problty
##    <chr>              <dbl>        <dbl>            <dbl>
##  1 Delhi               71.9         72.4           0.100 
##  2 Gujarat             71.6         72.2           0.100 
##  3 Karnataka           71.5         72.0           0.0999
##  4 Kerala              71.6         72.2           0.100 
##  5 Maharashtra         71.5         72.1           0.0998
##  6 Rajasthan           71.5         71.7           0.0998
##  7 Tamil Nadu          71.4         71.6           0.0996
##  8 Telangana           71.9         72.4           0.100 
##  9 Uttar Pradesh       71.9         72.5           0.100 
## 10 West Bengal         71.7         72.2           0.100

Assigning ‘anomaly’ tag to the lowest probability group

anomaly_2 <- grouped_data_2 %>%
  filter(expected_problty == min(expected_problty)) %>%
  pull(State)

print(anomaly_2)
## [1] "Tamil Nadu"

Assigning the lowest probability group an “anomaly” tag and Translating anomaly back into your original data frame i.e data_2

data_2 <- grouped_data_2 %>%
  mutate(anomaly_2 = ifelse(State == anomaly_2, "Anomaly", "Normal"))
print(data_2)
## # A tibble: 10 × 5
##    State         Mean_Value Median_Value expected_problty anomaly_2
##    <chr>              <dbl>        <dbl>            <dbl> <chr>    
##  1 Delhi               71.9         72.4           0.100  Normal   
##  2 Gujarat             71.6         72.2           0.100  Normal   
##  3 Karnataka           71.5         72.0           0.0999 Normal   
##  4 Kerala              71.6         72.2           0.100  Normal   
##  5 Maharashtra         71.5         72.1           0.0998 Normal   
##  6 Rajasthan           71.5         71.7           0.0998 Normal   
##  7 Tamil Nadu          71.4         71.6           0.0996 Anomaly  
##  8 Telangana           71.9         72.4           0.100  Normal   
##  9 Uttar Pradesh       71.9         72.5           0.100  Normal   
## 10 West Bengal         71.7         72.2           0.100  Normal

Calculating the frequency of each combination

combination_counts <- credit_data %>%
  group_by(State, LTV.Ratio) %>%
  summarise(count = n(), .groups = 'drop')
print(combination_counts)
## # A tibble: 80,892 × 3
##    State LTV.Ratio count
##    <chr>     <dbl> <int>
##  1 Delhi      40    1420
##  2 Delhi      40.0     3
##  3 Delhi      40.0     3
##  4 Delhi      40.1     3
##  5 Delhi      40.1     3
##  6 Delhi      40.1     3
##  7 Delhi      40.1     3
##  8 Delhi      40.2     3
##  9 Delhi      40.2     3
## 10 Delhi      40.2     3
## # ℹ 80,882 more rows

PART - 3

library(dplyr)
# Group by 'Category3'
grouped_data_3 <- credit_data %>%
  group_by(Occupation) %>%
  summarise(
    Mean_Value = mean(Income),
    Median_Value = median(Income)
  )
print(grouped_data_3)
## # A tibble: 15 × 3
##    Occupation             Mean_Value Median_Value
##    <chr>                       <dbl>        <dbl>
##  1 Banker                     81851.        74000
##  2 Business Owner             76175.        67000
##  3 Civil Servant              81737.        73000
##  4 Contractor                 76575.        68000
##  5 Doctor                     81758.        74000
##  6 Farmer                     76995.        68000
##  7 Graphic Designer           67482.        60000
##  8 Independent Consultant     67951.        61000
##  9 Photographer               68982.        63000
## 10 Shopkeeper                 76294.        67000
## 11 Software Engineer          82257.        74000
## 12 Student                    60974.        55000
## 13 Teacher                    82010.        73000
## 14 Writer                     68683.        61000
## 15 <NA>                       61991.        57000
ggplot(grouped_data_3, aes(x = Occupation, y = 1, fill = Median_Value)) +
  geom_tile() +
  labs(title = "Heatmap of Numerical vs. Categorical", x = "Occupation", y = "Median_Value") +
  scale_fill_gradient(low = "black", high = "blue") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Calculating expected probability for group 3

grouped_data_3 <- grouped_data_3 %>%
 mutate(expected_problty = (Mean_Value/ sum(Mean_Value)))
print(grouped_data_3)
## # A tibble: 15 × 4
##    Occupation             Mean_Value Median_Value expected_problty
##    <chr>                       <dbl>        <dbl>            <dbl>
##  1 Banker                     81851.        74000           0.0736
##  2 Business Owner             76175.        67000           0.0685
##  3 Civil Servant              81737.        73000           0.0735
##  4 Contractor                 76575.        68000           0.0689
##  5 Doctor                     81758.        74000           0.0735
##  6 Farmer                     76995.        68000           0.0693
##  7 Graphic Designer           67482.        60000           0.0607
##  8 Independent Consultant     67951.        61000           0.0611
##  9 Photographer               68982.        63000           0.0621
## 10 Shopkeeper                 76294.        67000           0.0686
## 11 Software Engineer          82257.        74000           0.0740
## 12 Student                    60974.        55000           0.0548
## 13 Teacher                    82010.        73000           0.0738
## 14 Writer                     68683.        61000           0.0618
## 15 <NA>                       61991.        57000           0.0558

Assigning ‘anomaly’ tag to the lowest probability group

anomaly_3 <- grouped_data_3 %>%
  filter(expected_problty == min(expected_problty)) %>%
  pull(Occupation)

print(anomaly_3)
## [1] "Student"

Assigning the lowest probability group an “anomaly” tag and Translating anomaly back into your original data frame i.e data_1

data_3 <- grouped_data_3 %>%
  mutate(anomaly_3 = ifelse(Occupation == anomaly_3, "Anomaly", "Normal"))
print(data_3)
## # A tibble: 15 × 5
##    Occupation             Mean_Value Median_Value expected_problty anomaly_3
##    <chr>                       <dbl>        <dbl>            <dbl> <chr>    
##  1 Banker                     81851.        74000           0.0736 Normal   
##  2 Business Owner             76175.        67000           0.0685 Normal   
##  3 Civil Servant              81737.        73000           0.0735 Normal   
##  4 Contractor                 76575.        68000           0.0689 Normal   
##  5 Doctor                     81758.        74000           0.0735 Normal   
##  6 Farmer                     76995.        68000           0.0693 Normal   
##  7 Graphic Designer           67482.        60000           0.0607 Normal   
##  8 Independent Consultant     67951.        61000           0.0611 Normal   
##  9 Photographer               68982.        63000           0.0621 Normal   
## 10 Shopkeeper                 76294.        67000           0.0686 Normal   
## 11 Software Engineer          82257.        74000           0.0740 Normal   
## 12 Student                    60974.        55000           0.0548 Anomaly  
## 13 Teacher                    82010.        73000           0.0738 Normal   
## 14 Writer                     68683.        61000           0.0618 Normal   
## 15 <NA>                       61991.        57000           0.0558 <NA>

Calculating the frequency of each combination

combination_counts <- credit_data %>%
  group_by(Occupation, Income) %>%
  summarise(count = n(), .groups = 'drop')
print(combination_counts)
## # A tibble: 2,898 × 3
##    Occupation Income count
##    <chr>       <int> <int>
##  1 Banker       9000     9
##  2 Banker      10000    46
##  3 Banker      11000    43
##  4 Banker      12000    45
##  5 Banker      13000    82
##  6 Banker      14000    65
##  7 Banker      15000    82
##  8 Banker      16000   125
##  9 Banker      17000   133
## 10 Banker      18000   140
## # ℹ 2,888 more rows

CONCLUSION :- I did not treat the “Existing_Customer” column as a categorical variable because when the “Existing_Customer” values are ‘NO’, the “Number of Existing Loans” and “Profile Score” columns predominantly contain ‘0’ values. As a result, I did not categorize the “Existing_Customer” column.

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.