## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 8,
  fig.height = 5
)

## ----setup--------------------------------------------------------------------
library(taxdiv)

# Mediterranean forest community
community <- c(
  Quercus_coccifera    = 25,
  Quercus_infectoria   = 18,
  Pinus_brutia         = 30,
  Pinus_nigra          = 12,
  Juniperus_excelsa    = 8,
  Juniperus_oxycedrus  = 6,
  Arbutus_andrachne    = 15,
  Styrax_officinalis   = 4,
  Cercis_siliquastrum  = 3,
  Olea_europaea        = 10
)

tax_tree <- build_tax_tree(
  species = names(community),
  Genus   = c("Quercus", "Quercus", "Pinus", "Pinus",
              "Juniperus", "Juniperus", "Arbutus", "Styrax",
              "Cercis", "Olea"),
  Family  = c("Fagaceae", "Fagaceae", "Pinaceae", "Pinaceae",
              "Cupressaceae", "Cupressaceae", "Ericaceae", "Styracaceae",
              "Fabaceae", "Oleaceae"),
  Order   = c("Fagales", "Fagales", "Pinales", "Pinales",
              "Pinales", "Pinales", "Ericales", "Ericales",
              "Fabales", "Lamiales")
)

## ----distance-----------------------------------------------------------------
dist_mat <- tax_distance_matrix(tax_tree)
round(dist_mat, 2)

## ----delta--------------------------------------------------------------------
d <- delta(community, tax_tree)
cat("Delta:", round(d, 4), "\n")

## ----delta_star---------------------------------------------------------------
ds <- delta_star(community, tax_tree)
cat("Delta*:", round(ds, 4), "\n")

## ----avtd---------------------------------------------------------------------
spp <- names(community)
avg_td <- avtd(spp, tax_tree)
cat("AvTD (Delta+):", round(avg_td, 4), "\n")

## ----vartd--------------------------------------------------------------------
var_td <- vartd(spp, tax_tree)
cat("VarTD (Lambda+):", round(var_td, 4), "\n")

## ----all_four-----------------------------------------------------------------
cat("Delta  (abundance-weighted diversity):  ", round(d, 4), "\n")
cat("Delta* (abundance-weighted distinctness):", round(ds, 4), "\n")
cat("AvTD   (presence/absence distinctness):  ", round(avg_td, 4), "\n")
cat("VarTD  (variation in distinctness):      ", round(var_td, 4), "\n")

## ----simulation---------------------------------------------------------------
# Use the built-in anatolian_trees dataset as species pool
data(anatolian_trees)

sim <- simulate_td(
  tax_tree = anatolian_trees,
  s_range = c(3, 15),
  n_sim = 99,
  index = "avtd",
  seed = 42
)

cat("Simulation complete.\n")
cat("Species pool size:", nrow(anatolian_trees), "\n")

## ----funnel, fig.width=9, fig.height=6, fig.alt="Funnel plot showing 95% confidence bands for AvTD with observed community point"----
plot_funnel(sim,
            observed = data.frame(
              site = "Mediterranean",
              s = length(community),
              value = avg_td
            ),
            index = "avtd",
            title = "AvTD Significance Test")

