## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment  = "#>",
  fig.width  = 6,
  fig.height = 5
)
library(depthR)
set.seed(42)

## ----mahalanobis-example------------------------------------------------------
data <- matrix(rnorm(200), nrow = 50, ncol = 4)
x    <- rbind(colMeans(data),        # center
              colMeans(data) + 3)    # outlier direction

mahalanobis_depth(x, data)

## ----tukey-example, eval = FALSE----------------------------------------------
# tukey_depth(x, data, batch_size = 50, min_batches = 3, patience = 2)

## ----simplicial-example, eval = FALSE-----------------------------------------
# simplicial_depth(x, data, batch_size = 50, min_batches = 3, max_batches = 5)

## ----projection-example, eval = FALSE-----------------------------------------
# projection_depth(x, data, batch_size = 50, min_batches = 3, patience = 2)

## ----spatial-example----------------------------------------------------------
spatial_depth(x, data)

## ----compute-depth------------------------------------------------------------
dd <- compute_depth(data, depth_fn = mahalanobis_depth)
dd

## ----depth-median-------------------------------------------------------------
m <- median(dd)
cat("Median index:", m$index, "\n")
print(round(m$point, 3))
cat("Depth:", round(m$depth, 4), "\n")

## ----depth-ranks--------------------------------------------------------------
r <- rank(dd)
cat("5 deepest observations:\n")
print(which(r <= 5))

## ----central-region-----------------------------------------------------------
cr <- central_region(dd, alpha = 0.50)
cat("Inner 50% contains", length(cr$indices), "observations\n")

## ----outlier-setup------------------------------------------------------------
set.seed(1)
clean             <- matrix(rnorm(200), nrow = 100, ncol = 2)
outliers_injected <- matrix(runif(10, min = 4, max = 6), nrow = 5, ncol = 2)
contaminated      <- rbind(clean, outliers_injected)

dd_cont <- compute_depth(contaminated, depth_fn = mahalanobis_depth)
out     <- outliers(dd_cont, threshold = 0.05)

cat("Injected outliers are rows 101-105\n")
cat("Detected outlier indices:\n")
print(sort(out$indices))

## ----outlier-plot-------------------------------------------------------------
plot(dd_cont, outlier_threshold = 0.05,
     main = "Mahalanobis depth — injected outliers flagged in red")

## ----dd-same------------------------------------------------------------------
set.seed(2)
x_same <- matrix(rnorm(200), nrow = 100, ncol = 2)
y_same <- matrix(rnorm(200), nrow = 100, ncol = 2)

dd_plot(x_same, y_same,
        depth_fn = mahalanobis_depth,
        main     = "DD-Plot: same distribution")

## ----dd-shift-----------------------------------------------------------------
y_shift <- matrix(rnorm(200, mean = 2), nrow = 100, ncol = 2)

dd_plot(x_same, y_shift,
        depth_fn = mahalanobis_depth,
        main     = "DD-Plot: location shift of 2")

## ----high-d-setup-------------------------------------------------------------
set.seed(3)
n       <- 100
d       <- 10
data_hd <- matrix(rnorm(n * d), nrow = n, ncol = d)
x_hd    <- rbind(colMeans(data_hd), colMeans(data_hd) + 4)
cat("Dimension d =", d, ", n =", n, "\n")

## ----high-d-mahal-------------------------------------------------------------
cat("Mahalanobis depth:\n")
print(round(mahalanobis_depth(x_hd, data_hd), 4))

## ----high-d-tukey, eval = FALSE-----------------------------------------------
# cat("\nTukey depth:\n")
# print(round(tukey_depth(x_hd, data_hd,
#             batch_size = 50, min_batches = 3, patience = 2), 4))

## ----high-d-spatial-----------------------------------------------------------
cat("\nSpatial depth:\n")
print(round(spatial_depth(x_hd, data_hd), 4))

