library(pROC)
## Type 'citation("pROC")' for a citation.
##
## Attaching package: 'pROC'
## The following objects are masked from 'package:stats':
##
## cov, smooth, var
data <- read.csv("Default.csv")
data$default <- as.factor(data$default)
model1 <- glm(default ~ income + balance, data = data, family = "binomial")
model2 <- glm(default ~ income + balance + student, data = data, family = "binomial")
model3 <- glm(default ~ balance, data = data, family = "binomial")
prob1 <- predict(model1, type = "response")
prob2 <- predict(model2, type = "response")
prob3 <- predict(model3, type = "response")
roc1 <- roc(data$default, prob1)
## Setting levels: control = No, case = Yes
## Setting direction: controls < cases
roc2 <- roc(data$default, prob2)
## Setting levels: control = No, case = Yes
## Setting direction: controls < cases
roc3 <- roc(data$default, prob3)
## Setting levels: control = No, case = Yes
## Setting direction: controls < cases
plot(roc1, col="blue", main="ROC Curve Comparison")
plot(roc2, col="red", add=TRUE)
plot(roc3, col="green", add=TRUE)
legend("bottomright",
legend=c("Model 1", "Model 2", "Model 3"),
col=c("blue","red","green"),
lwd=2)
auc(roc1)
## Area under the curve: 0.9491
auc(roc2)
## Area under the curve: 0.9496
auc(roc3)
## Area under the curve: 0.948
The ROC curve compares the classification performance of the three models. A model with a curve closer to the top-left corner has better predictive ability. Based on the AUC values, Model X performs the best because it has the highest AUC.