## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
has_rf <- requireNamespace("randomForest", quietly = TRUE)
has_igraph <- has_rf && requireNamespace("igraph", quietly = TRUE)
knitr::opts_chunk$set(eval = has_rf, purl = has_rf)

## ----setup--------------------------------------------------------------------
library(Proximum)
library(randomForest)

str(loans, give.attr = FALSE)

## ----model--------------------------------------------------------------------
predictors <- setdiff(names(loans), c("applicant_group", "vintage"))

set.seed(1)
rows <- sample(nrow(loans), 500)
book <- loans[rows, ]

rf <- randomForest(default ~ ., data = book[, predictors], ntree = 500)
px <- as_proximity(rf, newdata = book[, predictors])
summary(px)

## ----network, eval = has_igraph, fig.width = 6.5, fig.height = 5--------------
set.seed(11)
picture <- autoplot(px, type = "network", threshold = 0.2)
picture

## ----communities, eval = has_igraph-------------------------------------------
community <- picture$layers[[2]]$data$community
segments <- aggregate(
  cbind(default == "yes", score, utilisation) ~ community,
  data.frame(community, book), mean
)
names(segments) <- c("community", "default_rate", "score", "utilisation")
segments$n <- as.vector(table(community))
segments[order(-segments$default_rate), ]

mean(book$default == "yes")

## ----windows------------------------------------------------------------------
early <- loans$vintage %in% c("2019", "2020")
late  <- loans$vintage %in% c("2021", "2022")

set.seed(3)
held <- loans[late, ][sample(sum(late), 400), ]

forest_on <- function(subset, seed) {
  set.seed(seed)
  randomForest(default ~ ., data = loans[subset, predictors], ntree = 500)
}
represent <- function(fit) as_proximity(fit, newdata = held[, predictors])

## ----floor--------------------------------------------------------------------
replicates <- lapply(1:4, function(i) represent(forest_on(early, 100 + i)))
stability(replicates)

## ----shift--------------------------------------------------------------------
mantel_test(represent(forest_on(early, 1)),
            represent(forest_on(late, 2)), n_perm = 999)

## ----fairness-----------------------------------------------------------------
permanova(px, ~ default + applicant_group, data = book, n_perm = 999)

