This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.

Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Cmd+Shift+Enter.

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

##1Q) Select an interesting binary column of data, Converting “Existing Customer” to binary (0 or 1), Summary of the Existing Customer column

binary_column <- "Existing.Customer"
credit_data$`Existing.Customer` <- ifelse(credit_data$`Existing.Customer` == "Yes", 1, 0)
table(credit_data$Existing.Customer)
## 
##      0      1 
## 173952 105904

##2Q) Building a logistic regression model for this variable, using between 1-4 explanatory variables, Interpret the coefficients, and explain what they mean Using the Standard Error for at least one coefficient, build a C.I. for that coefficient, and interpret its meaning

# Load the car package for extracting standard errors
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:dplyr':
## 
##     recode
## The following object is masked from 'package:purrr':
## 
##     some
# Fit the logistic regression model
model <- glm(Existing.Customer ~ Age + Income + Credit.Score + Loan.Amount, data = credit_data, family = "binomial")
## Warning: glm.fit: algorithm did not converge
## Warning: glm.fit: fitted probabilities numerically 0 or 1 occurred
summary(model)
## 
## Call:
## glm(formula = Existing.Customer ~ Age + Income + Credit.Score + 
##     Loan.Amount, family = "binomial", data = credit_data)
## 
## Coefficients:
##                Estimate Std. Error z value Pr(>|z|)
## (Intercept)  -1.613e+04  1.157e+04  -1.395    0.163
## Age           9.186e-04  7.348e-01   0.001    0.999
## Income        1.065e-06  2.566e-04   0.004    0.997
## Credit.Score  2.480e+01  1.779e+01   1.395    0.163
## Loan.Amount  -2.484e-07  2.427e-04  -0.001    0.999
## 
## (Dispersion parameter for binomial family taken to be 1)
## 
##     Null deviance: 3.7125e+05  on 279855  degrees of freedom
## Residual deviance: 9.3046e-03  on 279851  degrees of freedom
## AIC: 10.009
## 
## Number of Fisher Scoring iterations: 25

Intercept: The intercept represents the log-odds of being an existing customer when all other predictor variables (Age, Income, Credit Score, Loan Amount) are zero. In this case, the estimated log-odds are approximately -16,130. However, the large standard error and high p-value suggest that the intercept is not statistically significant. This means that the model doesn’t provide meaningful information when all predictors are zero.

Age: The coefficient for “Age” is approximately 0.0009186. This small coefficient suggests that for a one-year increase in age, the log-odds of being an existing customer increase by 0.0009186. However, the very large standard error and a p-value of 0.999 indicate that “Age” is not a statistically significant predictor. The data do not provide enough evidence to conclude that age has a significant impact on being an existing customer.

Income: The coefficient for “Income” is approximately 0.000001065. This coefficient implies that for a one-unit increase in income, the log-odds of being an existing customer increase by 0.000001065. Similar to “Age,” the standard error is relatively large, and the p-value is very close to 1 (0.997), indicating that “Income” is not a statistically significant predictor.

Credit Score: The coefficient for “Credit Score” is approximately 24.8. This suggests that for a one-unit increase in credit score, the log-odds of being an existing customer increase by 24.8. However, similar to the intercept, the large standard error and a p-value of 0.163 indicate that “Credit Score” is not a statistically significant predictor.

Loan Amount: The coefficient for “Loan Amount” is approximately -0.0000002484. This small coefficient suggests that for a one-unit increase in loan amount, the log-odds of being an existing customer decrease by 0.0000002484. The large standard error and a p-value of 0.999 indicate that “Loan Amount” is not a statistically significant predictor.

In summary, based on the provided results, none of the predictor variables (Age, Income, Credit Score, Loan Amount) are statistically significant in predicting whether someone is an existing customer. The large standard errors and high p-values suggest that there is insufficient evidence to conclude that these variables have a significant impact on the likelihood of being an existing customer in the logistic regression model.

# Get the standard error for the Age coefficient
se_age <- sqrt(diag(vcov(model))["Age"])

# Calculate the confidence interval
lower_age <- coef(model)["Age"] - 1.96 * se_age
upper_age <- coef(model)["Age"] + 1.96 * se_age

# Display the confidence interval
cat("95% Confidence Interval for Age Coefficient: (", lower_age, ", ", upper_age, ")\n")
## 95% Confidence Interval for Age Coefficient: ( -1.439277 ,  1.441114 )

This confidence interval tells us that we are 95% confident that the true coefficient for the “Age” variable falls within the range of -1.439277 to 1.441114.

##3Q) Consider a transformation for any explanatory variable, and illustrate why you need the transformation (or why you do not) Scatter Plots

model <- lm(Loan.Amount ~ Credit.Score,
            filter(credit_data, State=='Karnataka'))

rsquared <- summary(model)$r.squared

credit_data |> 
  filter(State=='Karnataka') |>
  ggplot(mapping = aes(x = Credit.Score, 
                       y = Loan.Amount)) +
  geom_point(color = "orange") +  # Specify the color for the data points
  geom_smooth(method = 'lm', color = 'blue', linetype = 'dashed', 
              se = FALSE) +  # Linear regression line with gray color
  geom_smooth(color = 'red', se = FALSE) +  # Another linear regression line with red color
  labs(title = "Price vs. Price Per Sq. Ft.",
       subtitle = paste("Linear Fit R-Squared =", round(rsquared, 3))) +
  theme_classic()
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

The scatter plot shows linear relationship between response and explanatory variable. so there’s no need of further transformation.