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:
summary(cars)
## speed dist
## Min. : 4.0 Min. : 2.00
## 1st Qu.:12.0 1st Qu.: 26.00
## Median :15.0 Median : 36.00
## Mean :15.4 Mean : 42.98
## 3rd Qu.:19.0 3rd Qu.: 56.00
## Max. :25.0 Max. :120.00
You can also embed plots, for example:
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
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
##Loading my dataset LOAN
Loan <- read.csv("C:/Users/vj/Desktop/Loan.csv")
library(ggplot2)
##Grouping Data 1
grouped_data_1 <- Loan %>%group_by(loan_type) %>%
summarise(
Mean_Value_of_ROI = mean(rate_of_interest,na.rm=T))
print
## function (x, ...)
## UseMethod("print")
## <bytecode: 0x00000261846dcc78>
## <environment: namespace:base>
(grouped_data_1)
## # A tibble: 3 × 2
## loan_type Mean_Value_of_ROI
## <chr> <dbl>
## 1 type1 4.10
## 2 type2 4.02
## 3 type3 3.89
##Grouping Data 2
grouped_data_2 <- Loan %>%group_by(credit_type) %>%
summarise(
sum_loan_amount = sum(loan_amount,na.rm=T))
print
## function (x, ...)
## UseMethod("print")
## <bytecode: 0x00000261846dcc78>
## <environment: namespace:base>
(grouped_data_2)
## # A tibble: 3 × 2
## credit_type sum_loan_amount
## <chr> <int>
## 1 CIB 715840000
## 2 CRIF 611784500
## 3 EXP 572446000
##Grouping Data 3
grouped_data_3 <- Loan %>%group_by(loan_purpose) %>%
summarise(
sd_Upfront_charges = sd(Upfront_charges,na.rm=T))
print
## function (x, ...)
## UseMethod("print")
## <bytecode: 0x00000261846dcc78>
## <environment: namespace:base>
(grouped_data_3)
## # A tibble: 4 × 2
## loan_purpose sd_Upfront_charges
## <chr> <dbl>
## 1 p1 3224.
## 2 p2 2393.
## 3 p3 3423.
## 4 p4 2942.
##BAR plot for group data 1
ggplot(grouped_data_1, aes(loan_type, Mean_Value_of_ROI)) + geom_col()+ labs(title = "Avg value of rate of interest", 'Loan_type','Avg_of_ROI')+theme(axis.text.x = element_text(angle = 90))
## Calculating expected probability for group 1 based on Loan_type
grouped_data_1 <- grouped_data_1 %>%
mutate(exp_prob = (Mean_Value_of_ROI/ sum(Mean_Value_of_ROI)))
print(grouped_data_1)
## # A tibble: 3 × 3
## loan_type Mean_Value_of_ROI exp_prob
## <chr> <dbl> <dbl>
## 1 type1 4.10 0.342
## 2 type2 4.02 0.334
## 3 type3 3.89 0.324
Anomoly_1 <- grouped_data_1 %>%
filter(exp_prob == min(exp_prob)) %>%
pull(loan_type)
print(Anomoly_1)
## [1] "type3"
##Assigning the lowest probability group an “anomaly” tag and Translating anomaly back into your original data frame
Loan <- Loan %>%
mutate(Anomoly_List = ifelse(loan_type == Anomoly_1, "Anomaly", "Normal"))
print(head(Loan))
## ID year Gender approv_in_adv loan_type loan_purpose loan_amount
## 1 24892 2019 Male pre type1 p1 406500
## 2 24894 2019 Joint pre type1 p1 696500
## 3 24895 2019 Joint pre type1 p1 706500
## 4 24896 2019 Joint pre type1 p3 346500
## 5 24898 2019 Joint nopre type1 p3 376500
## 6 24899 2019 Sex Not Available nopre type3 p3 436500
## rate_of_interest Interest_rate_spread Upfront_charges term Neg_ammortization
## 1 4.560 0.2000 595.0 360 neg_amm
## 2 4.000 0.3042 0.0 360 not_neg
## 3 3.990 0.1523 370.0 360 not_neg
## 4 4.500 0.9998 5120.0 360 not_neg
## 5 4.875 0.7395 1150.0 360 not_neg
## 6 3.490 -0.2776 2316.5 360 not_neg
## interest_only property_value total_units income credit_type Credit_Score
## 1 not_int 508000 1U 9480 EXP 834
## 2 not_int 758000 1U 10440 CRIF 602
## 3 not_int 1008000 1U 10080 EXP 864
## 4 not_int 438000 1U 5040 EXP 860
## 5 not_int 478000 1U 5580 CIB 580
## 6 not_int 688000 1U 6720 CIB 788
## age Region Anomoly_List
## 1 35-44 south Normal
## 2 25-34 North Normal
## 3 35-44 North Normal
## 4 55-64 North Normal
## 5 55-64 central Normal
## 6 55-64 south Anomaly
##Calculating the frequency of each combination ###
combination_counts_1 <- Loan %>%
group_by(loan_type,rate_of_interest) %>%
summarise(count = n(), .groups = 'drop')
print(combination_counts_1)
## # A tibble: 107 × 3
## loan_type rate_of_interest count
## <chr> <dbl> <int>
## 1 type1 2.38 1
## 2 type1 2.75 9
## 3 type1 2.85 1
## 4 type1 2.88 10
## 5 type1 2.92 2
## 6 type1 2.98 5
## 7 type1 2.99 32
## 8 type1 3 12
## 9 type1 3.12 51
## 10 type1 3.18 1
## # ℹ 97 more rows
##scatter plot for group data 2
ggplot(grouped_data_2, aes(x = credit_type, y = sum_loan_amount)) +
geom_point() +
labs(title = "Scatterplot Example", x = "credit_type", y = "sum")
##Calculating expected probability for group 2 based on Loan_amount
grouped_data_2 <- grouped_data_2 %>%
mutate(exp_prob = (sum_loan_amount/ sum(sum_loan_amount)))
print(grouped_data_2)
## # A tibble: 3 × 3
## credit_type sum_loan_amount exp_prob
## <chr> <int> <dbl>
## 1 CIB 715840000 0.377
## 2 CRIF 611784500 0.322
## 3 EXP 572446000 0.301
##Assigning ‘anomaly’ tag to the lowest probability
Anomoly_2 <- grouped_data_2 %>%
filter(exp_prob == min(exp_prob)) %>%
pull(credit_type)
print(Anomoly_2)
## [1] "EXP"
##Assigning the lowest probability group an “anomaly” tag and Translating anomaly back into your original data frame
Loan <- Loan %>%
mutate(Anomoly_List_2 = ifelse(credit_type == Anomoly_2, "Anomaly", "Normal"))
print(head(Loan))
## ID year Gender approv_in_adv loan_type loan_purpose loan_amount
## 1 24892 2019 Male pre type1 p1 406500
## 2 24894 2019 Joint pre type1 p1 696500
## 3 24895 2019 Joint pre type1 p1 706500
## 4 24896 2019 Joint pre type1 p3 346500
## 5 24898 2019 Joint nopre type1 p3 376500
## 6 24899 2019 Sex Not Available nopre type3 p3 436500
## rate_of_interest Interest_rate_spread Upfront_charges term Neg_ammortization
## 1 4.560 0.2000 595.0 360 neg_amm
## 2 4.000 0.3042 0.0 360 not_neg
## 3 3.990 0.1523 370.0 360 not_neg
## 4 4.500 0.9998 5120.0 360 not_neg
## 5 4.875 0.7395 1150.0 360 not_neg
## 6 3.490 -0.2776 2316.5 360 not_neg
## interest_only property_value total_units income credit_type Credit_Score
## 1 not_int 508000 1U 9480 EXP 834
## 2 not_int 758000 1U 10440 CRIF 602
## 3 not_int 1008000 1U 10080 EXP 864
## 4 not_int 438000 1U 5040 EXP 860
## 5 not_int 478000 1U 5580 CIB 580
## 6 not_int 688000 1U 6720 CIB 788
## age Region Anomoly_List Anomoly_List_2
## 1 35-44 south Normal Anomaly
## 2 25-34 North Normal Normal
## 3 35-44 North Normal Anomaly
## 4 55-64 North Normal Anomaly
## 5 55-64 central Normal Normal
## 6 55-64 south Anomaly Normal
##Calculating the frequency of each combination for group 2
combination_counts_2 <- Loan %>%
group_by(credit_type,loan_amount) %>%
summarise(count = n(), .groups = 'drop')
print(combination_counts_2)
## # A tibble: 264 × 3
## credit_type loan_amount count
## <chr> <int> <int>
## 1 CIB 36500 1
## 2 CIB 56500 5
## 3 CIB 66500 3
## 4 CIB 76500 24
## 5 CIB 86500 22
## 6 CIB 96500 14
## 7 CIB 106500 38
## 8 CIB 116500 39
## 9 CIB 126500 42
## 10 CIB 136500 49
## # ℹ 254 more rows
##Heatmap plot for group 3
ggplot(grouped_data_3, aes(x = loan_purpose, y = 1, fill = sd_Upfront_charges)) +
geom_tile() +
labs(title = "Heatmap of Numerical vs. Categorical", x = "Loan_purpose", y = "sd_Upfront_charges") +
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(exp_prob = (sd_Upfront_charges/ sum(sd_Upfront_charges)))
print(grouped_data_2)
## # A tibble: 3 × 3
## credit_type sum_loan_amount exp_prob
## <chr> <int> <dbl>
## 1 CIB 715840000 0.377
## 2 CRIF 611784500 0.322
## 3 EXP 572446000 0.301
##finding ‘anomaly’ tag to the lowest probability
Anomoly_3 <- grouped_data_3 %>%
filter(exp_prob == min(exp_prob)) %>%
pull(loan_purpose)
print(Anomoly_3)
## [1] "p2"
##assigning anomoly to original dataset
Loan <- Loan %>%
mutate(Anomoly_List_3 = ifelse(loan_purpose == Anomoly_3, "Anomaly", "Normal"))
print(head(Loan))
## ID year Gender approv_in_adv loan_type loan_purpose loan_amount
## 1 24892 2019 Male pre type1 p1 406500
## 2 24894 2019 Joint pre type1 p1 696500
## 3 24895 2019 Joint pre type1 p1 706500
## 4 24896 2019 Joint pre type1 p3 346500
## 5 24898 2019 Joint nopre type1 p3 376500
## 6 24899 2019 Sex Not Available nopre type3 p3 436500
## rate_of_interest Interest_rate_spread Upfront_charges term Neg_ammortization
## 1 4.560 0.2000 595.0 360 neg_amm
## 2 4.000 0.3042 0.0 360 not_neg
## 3 3.990 0.1523 370.0 360 not_neg
## 4 4.500 0.9998 5120.0 360 not_neg
## 5 4.875 0.7395 1150.0 360 not_neg
## 6 3.490 -0.2776 2316.5 360 not_neg
## interest_only property_value total_units income credit_type Credit_Score
## 1 not_int 508000 1U 9480 EXP 834
## 2 not_int 758000 1U 10440 CRIF 602
## 3 not_int 1008000 1U 10080 EXP 864
## 4 not_int 438000 1U 5040 EXP 860
## 5 not_int 478000 1U 5580 CIB 580
## 6 not_int 688000 1U 6720 CIB 788
## age Region Anomoly_List Anomoly_List_2 Anomoly_List_3
## 1 35-44 south Normal Anomaly Normal
## 2 25-34 North Normal Normal Normal
## 3 35-44 North Normal Anomaly Normal
## 4 55-64 North Normal Anomaly Normal
## 5 55-64 central Normal Normal Normal
## 6 55-64 south Anomaly Normal Normal
##Calculating the frequency of each combination for group 3
combination_counts_3 <- Loan %>%
group_by(loan_type,Upfront_charges) %>%
summarise(count = n(), .groups = 'drop')
print(combination_counts_3)
## # A tibble: 4,190 × 3
## loan_type Upfront_charges count
## <chr> <dbl> <int>
## 1 type1 0 704
## 2 type1 1 1
## 3 type1 4.04 1
## 4 type1 7.9 1
## 5 type1 8.92 1
## 6 type1 9.77 1
## 7 type1 10.0 1
## 8 type1 16.6 1
## 9 type1 19.2 1
## 10 type1 19.6 1
## # ℹ 4,180 more rows
##plot for combination count for group 1
ggplot(combination_counts_1, aes(x = combination_counts_1$loan_type, y = combination_counts_1$count))+ geom_bar(stat = "identity", position = position_dodge(width = 0.8)) +
facet_wrap(~combination_counts_1$rate_of_interest) +
labs(
x = "loan_type",
y = "count",
fill = "team",
title = "Grouped Barplot loan_type"
) +
theme_minimal() + scale_fill_discrete() + theme(legend.position="top")
## Warning: Use of `combination_counts_1$loan_type` is discouraged.
## ℹ Use `loan_type` instead.
## Warning: Use of `combination_counts_1$count` is discouraged.
## ℹ Use `count` instead.
Conclusion: For some of the numerical columns ,corresponding categorical values are not having clear pattern or relationship between them, it causes irregular pattern and hard to visualize them ,such columns are Neg_ammortization,interest_only. we cant plot between these column as it would give uneven patterns.