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:
C:
You can also embed plots, for example:
# Load the dataset from the Downloads folder
bank.df <- read.csv("C:/Users/pulud/Downloads/banks.csv")
# Display the first few rows of the data
head(bank.df)
## Obs Financial.Condition TotCap.Assets TotExp.Assets TotLns.Lses.Assets
## 1 1 1 9.7 0.12 0.65
## 2 2 1 1.0 0.11 0.62
## 3 3 1 6.9 0.09 1.02
## 4 4 1 5.8 0.10 0.67
## 5 5 1 4.3 0.11 0.69
## 6 6 1 9.1 0.13 0.74
Note that the echo = FALSE parameter was added to the
code chunk to prevent printing of the R code that generated the
plot.
# Fit the logistic regression model
reg <- glm(Financial.Condition ~ TotExp.Assets + TotLns.Lses.Assets,
data = bank.df, family = "binomial")
# Display the summary of the model
summary(reg)
##
## Call:
## glm(formula = Financial.Condition ~ TotExp.Assets + TotLns.Lses.Assets,
## family = "binomial", data = bank.df)
##
## Coefficients:
## Estimate Std. Error z value Pr(>|z|)
## (Intercept) -14.721 6.675 -2.205 0.0274 *
## TotExp.Assets 89.834 47.781 1.880 0.0601 .
## TotLns.Lses.Assets 8.371 5.779 1.449 0.1474
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## (Dispersion parameter for binomial family taken to be 1)
##
## Null deviance: 27.726 on 19 degrees of freedom
## Residual deviance: 13.148 on 17 degrees of freedom
## AIC: 19.148
##
## Number of Fisher Scoring iterations: 6
# Logit as a function of the predictors
logit_formula <- function(TotExp_Assets, TotLns_Lses_Assets) {
return(-14.721 + (89.834 * TotExp_Assets) + (8.371 * TotLns_Lses_Assets))
}
# Odds as a function of the predictors
odds_formula <- function(TotExp_Assets, TotLns_Lses_Assets) {
logit <- logit_formula(TotExp_Assets, TotLns_Lses_Assets)
return(exp(logit))
}
# Probability as a function of the predictors
probability_formula <- function(TotExp_Assets, TotLns_Lses_Assets) {
odds <- odds_formula(TotExp_Assets, TotLns_Lses_Assets)
return(odds / (1 + odds))
}
# Calculate for a new bank with given ratios
new_logit <- logit_formula(0.11, 0.6)
new_odds <- exp(new_logit)
new_prob <- probability_formula(0.11, 0.6)
new_class <- ifelse(new_prob > 0.5, "Weak", "Strong")
# Display the results
cat("Logit:", new_logit, "\n")
## Logit: 0.18334
cat("Odds:", new_odds, "\n")
## Odds: 1.201223
cat("Probability:", new_prob, "\n")
## Probability: 0.545707
cat("Classification:", new_class, "\n")
## Classification: Weak
# Interpretation of the coefficient
# The coefficient for TotLns.Lses.Assets is 8.371.
# This means that for each one-unit increase in the TotLns.Lses.Assets ratio,
# the log-odds of the bank being financially weak increases by 8.371.
# Adjusting the cutoff value
# Since the cost of misclassifying a weak bank as strong is higher,
# the cutoff value should be decreased to reduce the probability threshold,
# making it more likely to classify banks as weak.
# Example: Lowering the cutoff to 0.4
new_cutoff <- 0.4
new_class <- ifelse(new_prob > new_cutoff, "Weak", "Strong")
# Display the new classification
cat("Classification with new cutoff:", new_class, "\n")
## Classification with new cutoff: Weak