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

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

# Example 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
)

## ----shannon------------------------------------------------------------------
# Default: natural logarithm
H <- shannon(community)
cat("Shannon H':", round(H, 4), "\n")
cat("Maximum possible H' for", length(community), "species:",
    round(log(length(community)), 4), "\n")
cat("Evenness (H'/H'max):", round(H / log(length(community)), 4), "\n")

## ----bias---------------------------------------------------------------------
cat("Uncorrected:  ", round(shannon(community), 4), "\n")
cat("Miller-Madow: ", round(shannon(community, correction = "miller_madow"), 4), "\n")
cat("Grassberger:  ", round(shannon(community, correction = "grassberger"), 4), "\n")
cat("Chao-Shen:    ", round(shannon(community, correction = "chao_shen"), 4), "\n")

## ----simpson------------------------------------------------------------------
# Dominance (D): probability of same-species pair
D <- simpson(community, type = "dominance")
cat("Simpson dominance (D):    ", round(D, 4), "\n")

# Gini-Simpson (1-D): probability of different-species pair
GS <- simpson(community, type = "gini_simpson")
cat("Gini-Simpson (1-D):       ", round(GS, 4), "\n")

# Inverse Simpson (1/D): effective number of species
inv <- simpson(community, type = "inverse")
cat("Inverse Simpson (1/D):    ", round(inv, 4), "\n")

## ----comparison---------------------------------------------------------------
# Even community
even <- c(sp1 = 20, sp2 = 20, sp3 = 20, sp4 = 20, sp5 = 20)

# Uneven community (same species, different abundances)
uneven <- c(sp1 = 90, sp2 = 4, sp3 = 3, sp4 = 2, sp5 = 1)

cat("=== Even community ===\n")
cat("Shannon:", round(shannon(even), 4), "\n")
cat("Simpson (1-D):", round(simpson(even, type = "gini_simpson"), 4), "\n\n")

cat("=== Uneven community ===\n")
cat("Shannon:", round(shannon(uneven), 4), "\n")
cat("Simpson (1-D):", round(simpson(uneven, type = "gini_simpson"), 4), "\n")

## ----limitation---------------------------------------------------------------
# Community A: 5 species from 5 different orders
comm_A <- c(sp1 = 20, sp2 = 20, sp3 = 20, sp4 = 20, sp5 = 20)

# Community B: 5 species from the same genus
comm_B <- c(sp6 = 20, sp7 = 20, sp8 = 20, sp9 = 20, sp10 = 20)

cat("Community A (5 orders)  - Shannon:", round(shannon(comm_A), 4), "\n")
cat("Community B (1 genus)   - Shannon:", round(shannon(comm_B), 4), "\n")
cat("Identical scores, yet A is far more taxonomically diverse.\n")

