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:

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

Including Plots

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(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.2     
## ── 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(ggthemes)
library(ggrepel)
library(pwrss)
## 
## Attaching package: 'pwrss'
## 
## The following object is masked from 'package:stats':
## 
##     power.t.test
Loan <- read.csv("C:/Users/vj/Desktop/Loan.csv")
library(ggplot2)
library(tidyverse)
library(tidyverse)
library(ggthemes)
library(ggrepel)

library(tidyverse)
library(ggthemes)
library(ggrepel)

library(pwr)
library(pwrss)

##Que1:Devise at least two different null hypotheses based on two different aspects (e.g., columns) of your data.

Hypothesis 1(Null Hypothesis for Rate of Interest by Gender):

Null Hypothesis: The gender of the borrower does not have a significant impact on interest rate of loan. Alternative Hypothesis: The gender of the borrower does have a significant impact on interest rate of loan.

– Hypothesis 2(Null Hypothesis for Loan amount by loan approval type):

Null Hypothesis: The type of approval whether it is pre-approved or not does not have a significant impact on the loan amount. Alternative Hypothesis: The type of approval whether it is pre-approved or not does have a significant impact on the loan amount.

##Que2:

Alpha Level (Significance Level):

Here i am considering a value of 0.05 (5%) ,as it is a very common choice in hypothesis testing.It means that I am willing to accept a 5% chance of making a Type I error (false positive) by rejecting a true null hypothesis.This level of significance is widely accepted in many fields and provides a reasonable balance between sensitivity and specificity in hypothesis testing.

Power Level:

I am taking power value as 0.80 (80%)represents the probability of correctly rejecting a false null hypothesis, i.e., the ability to detect a true effect.It means you have an 80% chance of detecting a significant effect when it truly exists. Choosing a higher power level, such as 0.90 or 0.95, would require a larger sample size, which might not recommended in my case as my dataset having only 5618 rows of data.

Minimum Effect Size (Effect Size Threshold): Considering 0.20 (small to moderate effect size)for my dataset. A minimum effect size of 0.20 represents a small to moderate effect, which is commonly used in social and behavioral sciences. It ensures that we are not just detecting statistically significant results but also results that are practically meaningful. A smaller effect size threshold might require a larger sample size to detect, whereas a larger threshold might make it too easy to find significant results that are not practically significant.

#Testing my dataset if i can able to perform  Neyman-Pearson hypothesis test
# Setting the parameters for the power analysis
effect_size <- 0.2  # Desired effect size (Cohen's d)
alpha <- 0.05       # Significance level (e.g., 0.05 for 5%)
power <- 0.80       # Desired power level (e.g., 0.80 for 80%)

required_sample_size <- pwr.t.test(
  d = effect_size,
  sig.level = alpha,
  power = power,
  type = "two.sample" 
)


print(required_sample_size)
## 
##      Two-sample t test power calculation 
## 
##               n = 393.4057
##               d = 0.2
##       sig.level = 0.05
##           power = 0.8
##     alternative = two.sided
## 
## NOTE: n is number in *each* group

Here the required sample size is less than my actual sample, so i can perform Neyman-Pearson hypothesis test on my data.

#performing Neyman-Pearson hypothesis test for my hypothesis-2
# Data Preparation
# Separating the data into two groups based on approval type
pre_approved <- Loan %>% filter(approv_in_adv == "pre")
not_pre_approved <- Loan %>% filter(approv_in_adv == "nopre")

#  Conducting a Two-Sample t-test
t_test_result <- t.test(pre_approved$loan_amount, not_pre_approved$loan_amount, alternative = "two.sided")

#  Interpret the Results
print(t_test_result)
## 
##  Welch Two Sample t-test
## 
## data:  pre_approved$loan_amount and not_pre_approved$loan_amount
## t = -2.6262, df = 1374.1, p-value = 0.00873
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -28726.248  -4160.816
## sample estimates:
## mean of x mean of y 
##  324597.3  341040.8

The results indicate that there is a statistically significant difference in mean loan amounts between the ‘pre_approved’ and ‘not_pre_approved’ groups.The p-value of 0.00873 is less than the significance level of 0.05, further supporting the rejection of the null hypothesis.In summary, we have evidence to conclude that the type of approval (pre-approved or not) has a significant impact on the loan amount, with the ‘pre_approved’ group having a lower mean loan amount.

#performing Neyman-Pearson hypothesis test for my hypothesis-1
#  Data Preparation
# Separating the data into two groups based on gender
GroupA <- Loan %>% filter(Gender == "Male")
GroupB <- Loan %>% filter(Gender == "Female")



# Conducting a Two-Sample t-test
t_test_result <- t.test(GroupA$rate_of_interest, GroupB$rate_of_interest, alternative = "two.sided")

# Interpret the Results
# Print the results of the t-test
print(t_test_result)
## 
##  Welch Two Sample t-test
## 
## data:  GroupA$rate_of_interest and GroupB$rate_of_interest
## t = -1.6132, df = 2413.4, p-value = 0.1068
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  -0.075305992  0.007328053
## sample estimates:
## mean of x mean of y 
##  4.084076  4.118065

The p-value of 0.1068 is greater than the significance level of 0.05, further supporting the acceptance of the null hypothesis.In summary, we have evidence to conclude that the gender (male or female) does not have any significant impact on the rate of interest.

#performing Fishers exact test for hypothesis-1

# Creating a contingency table
contingency_table <- table(Loan$Gender, cut(Loan$rate_of_interest, breaks = c(0, 10, 20, 30)))

# Perform Fisher's exact test
fisher_test_result <- fisher.test(contingency_table)

# Extract the p-value from the test result
p_value <- fisher_test_result$p.value

# Seting my significance level (alpha)
alpha <- 0.05

# Checking if the p-value is less than alpha
if (p_value < alpha) {
  cat("Reject the null hypothesis: Gender has a significant impact on interest rates.\n")
} else {
  cat("Fail to reject the null hypothesis: Gender does not have a significant impact on interest rates..\n")
}
## Fail to reject the null hypothesis: Gender does not have a significant impact on interest rates..
# Print the p-value
cat("P-value:", p_value, "\n")
## P-value: 1
#performing Fishers exact test for hypothesis-2

# Creating a contingency table without binning loan amounts
contingency_table <- table(Loan$approv_in_adv, Loan$loan_amount)

# Perform Fisher's exact test
fisher_test_result <- fisher.test(contingency_table, simulate.p.value = TRUE)

# Extract the p-value from the test result
p_value <- fisher_test_result$p.value

# Setting your significance level (alpha)
alpha <- 0.05

# Check if the p-value is less than alpha
if (p_value < alpha) {
  cat("Reject the null hypothesis: Approval type has a significant impact on loan amount.\n")
} else {
  cat("Fail to reject the null hypothesis: Approval type does not have a significant impact on loan amount.\n")
}
## Fail to reject the null hypothesis: Approval type does not have a significant impact on loan amount.
# Print the p-value
cat("P-value:", p_value, "\n")
## P-value: 0.6971514
# visualizing hypothesis case 2 using box plot for approval type and loan amount
ggplot(Loan, aes(x = approv_in_adv, y = loan_amount)) +
  geom_boxplot() +
  labs(x = "Approval Type", y = "Loan Amount") +
  ggtitle("Distribution of Loan Amounts by Approval Type") +
  theme_minimal()

In the box plot:

The x-axis represents the two approval types: “Pre-Approved” and “Not Pre-Approved.” The y-axis represents the loan amount. Each box-and-whisker plot corresponds to an approval type, and it provides information about the distribution of loan amounts within each category. You can visually compare the medians of the two box plots. If one median is noticeably higher than the other, it suggests a difference in the central tendency of loan amounts between the two approval types.

#visualizing hypothesis 1 using bar plot for Gender and rate of interest
# Filterring the data to include only "male" and "female" categories
filtered_data <- subset(Loan, Gender %in% c("Male", "Female"))


ggplot(filtered_data, aes(x = Gender, y = rate_of_interest, fill = Gender)) +
  geom_bar(stat = "summary", fun = "mean", position = "dodge") +
  labs(x = "Gender", y = "Average Interest Rate") +
  ggtitle("Average Interest Rates for Male and Female Borrowers") +
  theme_minimal()
## Warning: Removed 4 rows containing non-finite values (`stat_summary()`).

In the bar plot:

The x-axis represents the gender categories, with two bars: “Male” and “Female.” The y-axis represents the average interest rate. Each bar corresponds to a gender category, and the height of the bar represents the average interest rate for that category. As we see there is no significant impact of gender on interest rates.

``

```