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

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

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")
)

## ----deng_levels--------------------------------------------------------------
result <- ozkan_pto(community, tax_tree)

cat("Deng entropy by taxonomic level:\n\n")
for (i in seq_along(result$Ed_levels)) {
  level <- names(result$Ed_levels)[i]
  value <- result$Ed_levels[i]
  cat(sprintf("  %-10s Ed = %.4f\n", level, value))
}

## ----eight_indices------------------------------------------------------------
cat("=== All 8 Ozkan pTO indices ===\n\n")
cat("Standard (all levels):\n")
cat("  uTO      =", round(result$uTO, 4), "  (unweighted diversity)\n")
cat("  TO       =", round(result$TO, 4), "  (weighted diversity)\n")
cat("  uTO+     =", round(result$uTO_plus, 4), "  (unweighted distance)\n")
cat("  TO+      =", round(result$TO_plus, 4), "  (weighted distance)\n\n")

cat("Max-informative levels:\n")
cat("  uTO_max  =", round(result$uTO_max, 4), "  (unweighted, informative only)\n")
cat("  TO_max   =", round(result$TO_max, 4), "  (weighted, informative only)\n")
cat("  uTO+_max =", round(result$uTO_plus_max, 4), "  (unweighted distance, informative only)\n")
cat("  TO+_max  =", round(result$TO_plus_max, 4), "  (weighted distance, informative only)\n")

## ----run1---------------------------------------------------------------------
cat("Run 1 results:\n")
cat("  uTO+ =", round(result$uTO_plus, 4), "\n")
cat("  TO+  =", round(result$TO_plus, 4), "\n")

## ----run2---------------------------------------------------------------------
run2 <- ozkan_pto_resample(community, tax_tree, n_iter = 101, seed = 42)

cat("Run 1 (deterministic):  uTO+ =", round(run2$uTO_plus_det, 4), "\n")
cat("Run 2 (stochastic max): uTO+ =", round(run2$uTO_plus_max, 4), "\n")

## ----run2_plot, fig.width=9, fig.height=5, fig.alt="Iteration plot showing TO+ values across stochastic resampling iterations"----
plot_iteration(run2, component = "TO_plus",
               title = "Run 2: TO+ Across Iterations")

## ----run3---------------------------------------------------------------------
run3 <- ozkan_pto_sensitivity(community, tax_tree, run2, seed = 123)

cat("All levels:       TO+ =", round(run3$TO_plus_max, 4), "\n")
cat("Informative only: TO+ =", round(result$TO_plus_max, 4), "\n")

## ----full---------------------------------------------------------------------
full <- ozkan_pto_full(community, tax_tree, n_iter = 101, seed = 42)

cat("Complete pipeline summary:\n\n")
cat("         uTO+      TO+       uTO       TO\n")
cat("Run 1:", sprintf("%9.4f %9.4f %9.4f %9.4f",
    full$run1$uTO_plus, full$run1$TO_plus,
    full$run1$uTO, full$run1$TO), "\n")
cat("Run 2:", sprintf("%9.4f %9.4f %9.4f %9.4f",
    full$run2$uTO_plus_max, full$run2$TO_plus_max,
    full$run2$uTO_max, full$run2$TO_max), "\n")
cat("Run 3:", sprintf("%9.4f %9.4f %9.4f %9.4f",
    full$run3$uTO_plus_max, full$run3$TO_plus_max,
    full$run3$uTO_max, full$run3$TO_max), "\n")

## ----jackknife----------------------------------------------------------------
jk <- ozkan_pto_jackknife(community, tax_tree)

cat("Jackknife results (TO+ when each species is removed):\n\n")
jk_df <- jk$jackknife_results
for (i in seq_len(nrow(jk_df))) {
  direction <- ifelse(jk_df$TO_plus[i] > result$TO_plus, "UNHAPPY", "happy")
  cat(sprintf("  Remove %-25s -> TO+ = %.4f  [%s]\n",
              jk_df$species[i], jk_df$TO_plus[i], direction))
}

cat("\nHappy species:", jk$n_happy, "\n")
cat("Unhappy species:", jk$n_unhappy, "\n")

## ----compare, fig.width=8, fig.height=8, fig.alt="Radar chart comparing diversity indices between diverse and degraded communities"----
degraded <- c(
  Quercus_coccifera = 40,
  Pinus_brutia      = 35,
  Juniperus_oxycedrus = 10
)

communities <- list(
  "Intact (10 spp)"  = community,
  "Degraded (3 spp)" = degraded
)

plot_radar(communities, tax_tree,
           title = "Intact vs Degraded Forest")

