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
library(pwr)
credit_data = read.csv(("/Users/tbs/Downloads/Data Sets 'R'/credit_data.csv"))

library(ggplot2)

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

Hypothesis 1 (Null Hypothesis for Credit Score by Existing Customer):

Null Hypothesis:- There is no difference in the mean credit scores between customers who are existing customers and those who are not existing customers.

Alternative Hypothesis:- There is difference in the mean credit scores between customers who are existing customers and those who are not existing customers.

– Hypothesis 2 (Null Hypothesis for Credit Score by Employment Profile):

Null Hypothesis:- There is no difference in the mean credit scores between customers with a Salaried employment profile and customers with a Freelancer employment profile.

Alternative Hypothesis:- There is difference in the mean credit scores between customers with a Salaried employment profile and customers with a Freelancer employment profile.

##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-1 :-

# Data Preparation
# Separating the data into two groups based on approval type
a1 <- subset(credit_data,  Existing.Customer== "Yes")
a2 <- subset(credit_data, Existing.Customer == "No")
# Perform t-test
t_out <- t.test(a1$Credit.Score, a2$Credit.Score)

# View test results
t_out
## 
##  Welch Two Sample t-test
## 
## data:  a1$Credit.Score and a2$Credit.Score
## t = 893.36, df = 279827, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  279.5397 280.7690
## sample estimates:
## mean of x mean of y 
##  757.0912  476.9369
# Check p-value
pval <- t_out$p.value

# Interpret results
if (pval < alpha) {
  print("Reject H0. There is difference in the mean credit scores between customers who are existing customers and those who are not existing customers.")
} else {
  print("Fail to reject H0. There is no difference in the mean credit scores between customers who are existing customers and those who are not existing customers.")
}
## [1] "Reject H0. There is difference in the mean credit scores between customers who are existing customers and those who are not existing customers."

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-2 :-

# Data Preparation
# Separating the data into two groups based on approval type
b1 <- subset(credit_data,  Employment.Profile == "Salaried")
b2 <- subset(credit_data, Employment.Profile == "Freelancer")
# Perform t-test
t_out <- t.test(b1$Credit.Score, b2$Credit.Score)

# View test results
t_out
## 
##  Welch Two Sample t-test
## 
## data:  b1$Credit.Score and b2$Credit.Score
## t = 24.397, df = 30736, p-value < 2.2e-16
## alternative hypothesis: true difference in means is not equal to 0
## 95 percent confidence interval:
##  26.07082 30.62578
## sample estimates:
## mean of x mean of y 
##  594.5946  566.2463
# Check p-value
pval <- t_out$p.value

# Interpret results
if (pval < alpha) {
  print("Reject H0. There is no difference in the mean credit scores between customers with a Salaried employment profile and customers with a freelancer employment profile")
} else {
  print("Fail to reject H0. There is difference in the mean credit scores between customers with a Salaried employment profile and customers with a Freelancer employment profile")}
## [1] "Reject H0. There is no difference in the mean credit scores between customers with a Salaried employment profile and customers with a freelancer employment profile"

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

# Create a contingency table to cross-tabulate Credit.Score and Existing.Customer
contingency_table <- table(credit_data$Credit.Score, credit_data$Existing.Customer)

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

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

# Define the significance level (alpha)
alpha <- 0.05

# Check if the p-value is less than alpha (perform hypothesis test)
if (p_value < alpha) {
  cat("Reject H0. There is a difference in the mean credit scores between customers who are existing customers and those who are not existing customers.")
} else {
  cat("Fail to reject H0. There is no difference in the mean credit scores between customers who are existing customers and those who are not existing customers.")
}
## Reject H0. There is a difference in the mean credit scores between customers who are existing customers and those who are not existing customers.

##performing Fishers exact test for hypothesis-2

# Create a contingency table to cross-tabulate Credit.Score and Employment.Profile
contingency_table <- table(credit_data$Credit.Score, credit_data$Employment.Profile)

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

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

# Define the significance level (alpha)
alpha <- 0.05

# Check if the p-value is less than alpha (perform hypothesis test)
if (p_value < alpha) {
  cat("Reject H0. There is no difference in the mean credit scores between customers with a Salaried employment profile and customers with a Freelancer employment profile.")
} else {
  cat("Fail to reject H0. There is a difference in the mean credit scores between customers with a Salaried employment profile and customers with a Freelancer employment profile.")
}
## Reject H0. There is no difference in the mean credit scores between customers with a Salaried employment profile and customers with a Freelancer employment profile.
# Bar Plot
library(ggplot2)

# Create a box plot
ggplot(data = credit_data, aes(x = Existing.Customer, y = Credit.Score, fill = Existing.Customer)) +
  geom_boxplot() +
  labs(
    x = "Existing Customer",
    y = "Credit Score",
    fill = "Existing Customer"
  ) +
  theme_minimal() +
  ggtitle("Credit Score Distribution by Existing Customer (Box Plot)") +
  scale_fill_brewer(palette = "Set2") +  # Use a color palette
  theme(
    legend.position = "top",  # Move legend to the top
    plot.title = element_text(size = 16, hjust = 0.5),  # Adjust title size and centering
    axis.title = element_text(size = 12),  # Adjust axis label size
    legend.title = element_text(size = 12)  # Adjust legend title size
  )

The box plot illustrates the distribution of credit scores, comparing existing customers to non-existing customers. It highlights differences in central tendencies (medians) and variability in credit scores, offering insights into potential variations in creditworthiness between these customer groups.

# Filter the data for "Salaried" and "Freelancer" employment profiles
filtered_data <- credit_data[credit_data$Employment.Profile %in% c("Salaried", "Freelancer"), ]

# Create a box plot
ggplot(data = filtered_data, aes(x = Employment.Profile, y = Credit.Score, fill = Employment.Profile)) +
  geom_boxplot() +
  labs(
    x = "Employment Profile",
    y = "Credit Score",
    fill = "Employment Profile"
  ) +
  theme_minimal() +
  ggtitle("Box Plot of Credit Score by Employment Profile (Salaried and Freelancer)")

The box plot compares the distribution of credit scores for “Salaried” and “Freelancer” profiles. It shows differences in central tendencies (medians) and variability in credit scores, helping to identify potential distinctions in creditworthiness between the two employment categories.