ROC and AUC in R

install.packages("pROC")
library(pROC)

install.packages("randomForest")
library(randomForest)

set.seed(420)

num.samples <- 100

weight <- sort(rnorm(n = num.samples, mean = 172, sd = 29))
obese <- ifelse(test = (runif(n=num.samples)<(rank(weight)/100)), yes = 1, no = 0); obese

plot(x = weight, y = obese)
glm.fit = glm(obese~weight, family = binomial)
lines(weight, glm.fit$fitted.values)

# "pty" aka "plot type", and "s" means square. "m" means Maximum
par(pty = 's')
roc(obese, glm.fit$fitted.values, plot=TRUE)
# if we want to print AUC, we can set the "print.auc = TRUE"
roc(obese, glm.fit$fitted.values, plot=TRUE, legacy.axes=TRUE, percent = TRUE, 
    xlab = "False Positive Percentage", ylab = "True Positive Percentage",
    # the color of the line
    col = "#377eb8",
    # the wide of the line
    lwd = 5,
    print.auc = TRUE,
    # "#377eb8" is blue, to make the color semi-transparent, we add "22" to the end, "35" is OK as well.
    # print partial AUC and draw the partial area
    print.auc.x = 45, partial.auc = c(100, 90), auc.polygon = TRUE, auc.polygon.col = "#377eb822"
)
roc.info = roc(obese, glm.fit$fitted.values, legacy.axes = TRUE)
roc.df = data.frame(
  tpp = roc.info$sensitivities*100,
  fpp = (1-roc.info$specificities)*100,
  thresholds = roc.info$thresholds
)

head(roc.df)
tail(roc.dff)

roc.df[roc.df$tpp > 60 & roc.df$tpp < 80, ]
legend("bottomright", legend=c("Logistic Regression", "Random Forest"),
       col = c("blue", "green"), lwd = 4)