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

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

set.seed(1)
n <- 600
X <- data.frame(matrix(rnorm(n * 6), n, 6))
y <- factor(ifelse(X$X1 + X$X2 + rnorm(n) > 0, "a", "b"))
training <- cbind(X, y = y)

forest <- randomForest(y ~ ., data = training, ntree = 500)
px <- as_proximity(forest, newdata = training)
format(object.size(px), units = "auto")

## ----sparsify-----------------------------------------------------------------
sp <- sparsify(px, threshold = 0.05)
sp

## ----sparsify-summary---------------------------------------------------------
summary(sp)

## ----sparsify-cost------------------------------------------------------------
c(
  dense = format(object.size(px), units = "auto"),
  sparse = format(object.size(sp), units = "auto")
)

## ----sparsify-refusal, error = TRUE-------------------------------------------
try({
mantel_test(sp, px, n_perm = 99)
})

## ----sparsify-densification---------------------------------------------------
dissimilarity <- as_dissimilarity(px)
c(
  proximity = mean(as.matrix(px) != 0),
  dissimilarity = mean(dissimilarity != 0),
  centred = mean(double_centre(dissimilarity) != 0)
)

## ----nystrom------------------------------------------------------------------
approximation <- nystrom(forest, training, landmarks = 60, strata = training$y)
approximation

## ----nystrom-summary----------------------------------------------------------
summary(approximation)

## ----nystrom-landmarks--------------------------------------------------------
sapply(c(20, 60, 180), function(m) {
  summary(nystrom(forest, training, landmarks = m))$diagonal_error
})

## ----nystrom-embedding--------------------------------------------------------
coordinates <- embedding(approximation, k = 2)
dim(coordinates)

protest(approximation, px, n_perm = 199)

## ----nystrom-refusal, error = TRUE--------------------------------------------
try({
cka(approximation, px)
})

## ----n-trees------------------------------------------------------------------
required <- n_trees_required(forest, training, eps = 0.2)
required
attr(required, "path")

## ----n-trees-plot, fig.width = 6, fig.height = 4------------------------------
autoplot(required)

## ----n-trees-integer----------------------------------------------------------
required + 100L

## ----n-trees-unreachable------------------------------------------------------
unreachable <- n_trees_required(forest, training, eps = 0.01)
c(answer = unreachable, projected_trees = attr(unreachable, "projected"))

## ----stability----------------------------------------------------------------
replicates <- lapply(1:4, function(i) {
  as_proximity(randomForest(y ~ ., data = training, ntree = 250), newdata = training)
})
agreement <- stability(replicates)
agreement

## ----stability-plot, fig.width = 6, fig.height = 3.4--------------------------
autoplot(agreement)

## ----stream-------------------------------------------------------------------
set.seed(2)
shallow <- randomForest(y ~ ., data = training, ntree = 500, maxnodes = 8)

streamed <- proximity_stream(forest, training)
streamed_shallow <- proximity_stream(shallow, training)
streamed

## ----stream-agrees------------------------------------------------------------
c(streamed = cka(streamed, streamed_shallow),
  dense    = cka(px, as_proximity(shallow, newdata = training)))

