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.
Loan <- read.csv("C:/Users/vj/Desktop/Loan.csv")
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
library(purrr)
library(ggplot2)
fixed_columns <- c("loan_amount","loan_type","loan_purpose","rate_of_interest","Interest_rate_spread","Upfront_charges","term")
num <- sample(5:10, 1)
# Create a list to store the subsamples
subsample_list <- list()
for (i in 1:num) {
# sample size (approximately 50% of the data)
s_size <- round(0.5 * nrow(Loan))
# Randomly select rows with replacement
s_index <- sample(1:nrow(Loan), size = s_size, replace = TRUE)
# Create the subsample data frame with fixed columns
subsample <- Loan[s_index, fixed_columns]
# Store the subsample in the list
subsample_list[[i]] <- subsample
}
# View the list of subsamples
View(subsample_list)
#print(head(subsample_list))
## Displaying the dimensions of each subsample{r dimensions of each sample}
sapply(subsample_list, dim)
## [,1] [,2] [,3] [,4] [,5] [,6]
## [1,] 2808 2808 2808 2808 2808 2808
## [2,] 7 7 7 7 7 7
##{r summary of subsample}
summary_table <- lapply(subsample_list, function(subsample){
summary_df <-summary(subsample)
print(summary_df)
})
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 335571 Mean :4.086
## 3rd Qu.: 436500 3rd Qu.:4.500
## Max. :1606500 Max. :5.875
## NA's :7
## Interest_rate_spread Upfront_charges term
## Min. :-0.8703 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1020 1st Qu.: 813.9 1st Qu.:360.0
## Median : 0.4017 Median : 2800.3 Median :360.0
## Mean : 0.4645 Mean : 3292.3 Mean :336.3
## 3rd Qu.: 0.7717 3rd Qu.: 4861.1 3rd Qu.:360.0
## Max. : 2.5370 Max. :20678.8 Max. :360.0
## NA's :7 NA's :10
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 56500 Length:2808 Length:2808 Min. :2.375
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 335717 Mean :4.086
## 3rd Qu.: 436500 3rd Qu.:4.500
## Max. :3006500 Max. :5.875
## NA's :3
## Interest_rate_spread Upfront_charges term
## Min. :-0.8863 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1020 1st Qu.: 855.5 1st Qu.:360.0
## Median : 0.3916 Median : 2838.9 Median :360.0
## Mean : 0.4635 Mean : 3373.2 Mean :334.4
## 3rd Qu.: 0.7581 3rd Qu.: 5004.3 3rd Qu.:360.0
## Max. : 2.5370 Max. :60000.0 Max. :360.0
## NA's :3 NA's :5
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.625
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 336012 Mean :4.080
## 3rd Qu.: 439000 3rd Qu.:4.500
## Max. :1496500 Max. :5.875
## NA's :2
## Interest_rate_spread Upfront_charges term
## Min. :-0.87030 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.08715 1st Qu.: 793.8 1st Qu.:360.0
## Median : 0.38070 Median : 2816.0 Median :360.0
## Mean : 0.45068 Mean : 3310.4 Mean :335.1
## 3rd Qu.: 0.75008 3rd Qu.: 4993.8 3rd Qu.:360.0
## Max. : 1.98460 Max. :23090.0 Max. :360.0
## NA's :2 NA's :4
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 26500 Length:2808 Length:2808 Min. :2.375
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 335250 Mean :4.089
## 3rd Qu.: 446500 3rd Qu.:4.500
## Max. :1486500 Max. :5.750
## NA's :5
## Interest_rate_spread Upfront_charges term
## Min. :-0.8273 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1064 1st Qu.: 879.4 1st Qu.:360.0
## Median : 0.4184 Median : 3024.6 Median :360.0
## Mean : 0.4728 Mean : 3478.0 Mean :335.2
## 3rd Qu.: 0.7548 3rd Qu.: 5308.8 3rd Qu.:360.0
## Max. : 2.5370 Max. :20678.8 Max. :360.0
## NA's :5 NA's :6
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 334021 Mean :4.086
## 3rd Qu.: 436500 3rd Qu.:4.500
## Max. :1266500 Max. :5.875
## NA's :2
## Interest_rate_spread Upfront_charges term
## Min. :-0.8863 Min. : 0 Min. : 96.0
## 1st Qu.: 0.1199 1st Qu.: 868 1st Qu.:360.0
## Median : 0.3977 Median : 2834 Median :360.0
## Mean : 0.4678 Mean : 3366 Mean :335.3
## 3rd Qu.: 0.7744 3rd Qu.: 5030 3rd Qu.:360.0
## Max. : 1.9846 Max. :21294 Max. :360.0
## NA's :2 NA's :7
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 216500 Class :character Class :character 1st Qu.:3.625
## Median : 316500 Mode :character Mode :character Median :3.990
## Mean : 338409 Mean :4.070
## 3rd Qu.: 446500 3rd Qu.:4.500
## Max. :1496500 Max. :5.875
## NA's :6
## Interest_rate_spread Upfront_charges term
## Min. :-0.8198 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1019 1st Qu.: 873.4 1st Qu.:360.0
## Median : 0.3936 Median : 2833.9 Median :360.0
## Mean : 0.4627 Mean : 3435.4 Mean :334.5
## 3rd Qu.: 0.7679 3rd Qu.: 5038.1 3rd Qu.:360.0
## Max. : 2.5370 Max. :21293.6 Max. :360.0
## NA's :6 NA's :7
for (i in 1: num){
cat("### Subsample", i, "summary statisics \n")
print(summary_table[[i]])
}
## ### Subsample 1 summary statisics
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 335571 Mean :4.086
## 3rd Qu.: 436500 3rd Qu.:4.500
## Max. :1606500 Max. :5.875
## NA's :7
## Interest_rate_spread Upfront_charges term
## Min. :-0.8703 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1020 1st Qu.: 813.9 1st Qu.:360.0
## Median : 0.4017 Median : 2800.3 Median :360.0
## Mean : 0.4645 Mean : 3292.3 Mean :336.3
## 3rd Qu.: 0.7717 3rd Qu.: 4861.1 3rd Qu.:360.0
## Max. : 2.5370 Max. :20678.8 Max. :360.0
## NA's :7 NA's :10
## ### Subsample 2 summary statisics
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 56500 Length:2808 Length:2808 Min. :2.375
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 335717 Mean :4.086
## 3rd Qu.: 436500 3rd Qu.:4.500
## Max. :3006500 Max. :5.875
## NA's :3
## Interest_rate_spread Upfront_charges term
## Min. :-0.8863 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1020 1st Qu.: 855.5 1st Qu.:360.0
## Median : 0.3916 Median : 2838.9 Median :360.0
## Mean : 0.4635 Mean : 3373.2 Mean :334.4
## 3rd Qu.: 0.7581 3rd Qu.: 5004.3 3rd Qu.:360.0
## Max. : 2.5370 Max. :60000.0 Max. :360.0
## NA's :3 NA's :5
## ### Subsample 3 summary statisics
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.625
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 336012 Mean :4.080
## 3rd Qu.: 439000 3rd Qu.:4.500
## Max. :1496500 Max. :5.875
## NA's :2
## Interest_rate_spread Upfront_charges term
## Min. :-0.87030 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.08715 1st Qu.: 793.8 1st Qu.:360.0
## Median : 0.38070 Median : 2816.0 Median :360.0
## Mean : 0.45068 Mean : 3310.4 Mean :335.1
## 3rd Qu.: 0.75008 3rd Qu.: 4993.8 3rd Qu.:360.0
## Max. : 1.98460 Max. :23090.0 Max. :360.0
## NA's :2 NA's :4
## ### Subsample 4 summary statisics
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 26500 Length:2808 Length:2808 Min. :2.375
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 335250 Mean :4.089
## 3rd Qu.: 446500 3rd Qu.:4.500
## Max. :1486500 Max. :5.750
## NA's :5
## Interest_rate_spread Upfront_charges term
## Min. :-0.8273 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1064 1st Qu.: 879.4 1st Qu.:360.0
## Median : 0.4184 Median : 3024.6 Median :360.0
## Mean : 0.4728 Mean : 3478.0 Mean :335.2
## 3rd Qu.: 0.7548 3rd Qu.: 5308.8 3rd Qu.:360.0
## Max. : 2.5370 Max. :20678.8 Max. :360.0
## NA's :5 NA's :6
## ### Subsample 5 summary statisics
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 206500 Class :character Class :character 1st Qu.:3.750
## Median : 306500 Mode :character Mode :character Median :3.990
## Mean : 334021 Mean :4.086
## 3rd Qu.: 436500 3rd Qu.:4.500
## Max. :1266500 Max. :5.875
## NA's :2
## Interest_rate_spread Upfront_charges term
## Min. :-0.8863 Min. : 0 Min. : 96.0
## 1st Qu.: 0.1199 1st Qu.: 868 1st Qu.:360.0
## Median : 0.3977 Median : 2834 Median :360.0
## Mean : 0.4678 Mean : 3366 Mean :335.3
## 3rd Qu.: 0.7744 3rd Qu.: 5030 3rd Qu.:360.0
## Max. : 1.9846 Max. :21294 Max. :360.0
## NA's :2 NA's :7
## ### Subsample 6 summary statisics
## loan_amount loan_type loan_purpose rate_of_interest
## Min. : 36500 Length:2808 Length:2808 Min. :2.750
## 1st Qu.: 216500 Class :character Class :character 1st Qu.:3.625
## Median : 316500 Mode :character Mode :character Median :3.990
## Mean : 338409 Mean :4.070
## 3rd Qu.: 446500 3rd Qu.:4.500
## Max. :1496500 Max. :5.875
## NA's :6
## Interest_rate_spread Upfront_charges term
## Min. :-0.8198 Min. : 0.0 Min. : 96.0
## 1st Qu.: 0.1019 1st Qu.: 873.4 1st Qu.:360.0
## Median : 0.3936 Median : 2833.9 Median :360.0
## Mean : 0.4627 Mean : 3435.4 Mean :334.5
## 3rd Qu.: 0.7679 3rd Qu.: 5038.1 3rd Qu.:360.0
## Max. : 2.5370 Max. :21293.6 Max. :360.0
## NA's :6 NA's :7
# plot histogram
histograms <- lapply(subsample_list, function(subsample) {
ggplot(subsample, aes(x = rate_of_interest)) +
geom_histogram(binwidth = 0.5, fill = 'blue', color = 'black') +
labs(title = "Histogram for rate_of_interest", x = 'Value', y = 'Frequency')
})
# display histograms and summary
for (i in 1:num) {
cat("Subsample", i, "summary statistics:\n")
print(histograms[[i]])
}
## Subsample 1 summary statistics:
## Warning: Removed 7 rows containing non-finite values (`stat_bin()`).
## Subsample 2 summary statistics:
## Warning: Removed 3 rows containing non-finite values (`stat_bin()`).
## Subsample 3 summary statistics:
## Warning: Removed 2 rows containing non-finite values (`stat_bin()`).
## Subsample 4 summary statistics:
## Warning: Removed 5 rows containing non-finite values (`stat_bin()`).
## Subsample 5 summary statistics:
## Warning: Removed 2 rows containing non-finite values (`stat_bin()`).
## Subsample 6 summary statistics:
## Warning: Removed 6 rows containing non-finite values (`stat_bin()`).
##SUMMARY: Here we have plot histograms for rate_of_interest and
frequency, from all of the samples that taken from the dataset, we can
see there is no anomoly among the samples.All the samples showing
similar type of trends.
## Anomalies and consistency{r anomalies}
means <- lapply(subsample_list, function(subsample) {
mean(subsample$loan_amount)
})
View(means)
mins <- lapply(subsample_list, function(subsample) {
min(subsample$loan_amount)
})
View(mins)
##{r consistency}
for (i in 1:num) {
cat("Subsample", i, "Mean of Quantity", means[[i]], "\n")
cat("Subsample", i, "MIN of Quantity", mins[[i]], "\n")
}
## Subsample 1 Mean of Quantity 335570.5
## Subsample 1 MIN of Quantity 36500
## Subsample 2 Mean of Quantity 335716.5
## Subsample 2 MIN of Quantity 56500
## Subsample 3 Mean of Quantity 336012.1
## Subsample 3 MIN of Quantity 36500
## Subsample 4 Mean of Quantity 335250
## Subsample 4 MIN of Quantity 26500
## Subsample 5 Mean of Quantity 334021.4
## Subsample 5 MIN of Quantity 36500
## Subsample 6 Mean of Quantity 338408.8
## Subsample 6 MIN of Quantity 36500
##conclusion : From the above means and Min value summaries, we can conclude that all the outcome values are similar and no typr of anomoly detected.