Logistic regression

require(ISLR)
## Loading required package: ISLR
require(ggplot2)
## Loading required package: ggplot2
data(Default)


a <- subset(Default, default=="Yes")
b <- subset(Default, default=="No")
train <- rbind(a, b[1:300,])
train_id <- sample(dim(train)[1],400)
train_set <- train[train_id,]
test_set <- train[-train_id,]

fit <- glm(default ~ balance + income, data=train_set, family="binomial")
pred <- predict(fit, test_set, type="response")
pred <- ifelse(pred > 0.5, "Yes", "No")
accuracy <- mean(pred == test_set$default)
accuracy
## [1] 0.9055794
# Plot decision boundary
boundary_grid <- expand.grid(income=seq(min(Default$income), max(Default$income), 500),
                             balance=seq(min(Default$balance), max(Default$balance), 5))
pred <- predict(fit, boundary_grid, type="response")
pred <- ifelse(pred > 0.5, "Yes", "No")
boundary_grid$default <- pred

ggplot(boundary_grid, aes(x=balance, y=income)) + 
  geom_point(aes(color=default), alpha=0.2, size=0.8) + 
  geom_point(data=train, aes(x=balance, y=income, color=default)) + 
  labs(title="Predicting default based on income and balance",
       xlab="Credit cared balance",
       ylab="Income")