Question 1

credit <- read.table(file = "http://nathanieldphillips.com/wp-content/uploads/2015/05/credit.txt",
                     header = T,
                     sep = ",",
                     stringsAsFactors = F)

Question 2

hist(x = credit$amount,
     main = "Personal loans by German borrowers",
     xlab = "Loan size (in DM)",
     ylab = "Frequency",
     ylim = c(0, 500))

abline(v = median(credit$amount),
       lwd = 2,
       lty = 2)

paste("Median =", median(credit$amount), sep = " ")
## [1] "Median = 2319.5"
text(median(credit$amount), 400,
     labels = paste("Median =", median(credit$amount), sep = " "),
     adj = 0,
     pos = 4)

Question 3

plot(x = credit$age,
     y = credit$amount,
     main = "Borrower age and loan amount",
     xlab = "Borrower Age",
     ylab = "Loan amount (in DM)",
     xlim = c(20, 75),
     ylim = c(0, 17500),
     col = "gray",
     pch = 16)

Question 4

require("beanplot")
## Loading required package: beanplot
beanplot(amount ~ years_at_residence,
         data = credit,
         main = "Number of years at residence and loan amount",
         xlab = "Years at Residence",
         ylab = "Loan amount in DM (log-transformed)",
         what = c(1, 1, 1, 1),log="y",
         col = "white")

Question 5

plot(x = "",
     y = "",
     main = "Loan duration and amount of skilled and unskilled borrowers",
     xlab = "Loan Duration (in months)",
     ylab = "Loan Amount (in DM)",
     xlim = c(0,80),
     ylim = c(0,15000))

abline(h = seq(from = 0, to = 15000, by = 5000),
       col = "gray")
abline(h = seq(from = 0, to = 15000, by = 1000),
       col = "gray",
       lwd = .5)
abline(v = seq(from = 0, to = 80, by = 20),
       col = "gray")
abline(v = seq(from = 0, to = 80, by = 10),
       col = "gray", 
       lwd = .5)



job1 <- subset(credit, job == "skilled")

job2 <- subset(credit, job == "unskilled")

points(x = job1$months_loan_duration,
     y = job1$amount,
     col = "red",
     pch = 16)

points(x = job2$months_loan_duration,
       y = job2$amount,
       col = "blue",
       pch = 16)

legend("topright",
       legend = c("Skilled", "Unskilled"),
       col = c("red", "blue"),
       pch = c(16, 16))

Question 6

critical <- subset(credit, credit_history == "critical")$default == "yes"
poor <- subset(credit, credit_history == "poor")$default == "yes"
good <- subset(credit, credit_history == "good")$default == "yes"
verygood <- subset(credit, credit_history == "very good")$default == "yes"
perfect <- subset(credit, credit_history == "perfect")$default == "yes"

means.vec <- c(mean(critical), mean(poor), mean(good), mean(verygood), mean(perfect))


lenghts.vec <- c(length(critical), length(poor), length(good), length(verygood), length(perfect))


plot(x = credit$credit_history,
     y = credit$default,
     main = "Credit History and Default Rate",
     xlab = "Credit History",
     ylab = "Default Rate",
     xlim = c(0, 6),
     ylim = c(0, 1),
     type = "n",
     xaxt = "n")
## Warning in xy.coords(x, y, xlabel, ylabel, log): NAs durch Umwandlung
## erzeugt
## Warning in xy.coords(x, y, xlabel, ylabel, log): NAs durch Umwandlung
## erzeugt
mtext(side = 1, text = c("critical", "poor", "good", "very good", "perfect"), at = 1:5, line = 1)

mtext(side = 3, text = "Size of circles indicates number of people in category", line = 0.3)

points(1:5, 
       means.vec, 
       cex = lenghts.vec / 26, 
       col = "black",
       pch = 21)

points(1:5, 
       means.vec, 
       pch = 16, 
       col = "black")


text(1:5, 
     means.vec, 
     labels = round(means.vec, 2),
     pos = 1)

segments(rep(0, 5),
         means.vec,
         1:5,
         means.vec,
         lty = 2)