## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(
  collapse  = TRUE,
  comment   = "#>",
  fig.align = "center",
  message   = FALSE,
  warning   = FALSE
)
library(ethnoecologyR)
library(dplyr)
library(ggplot2)
data(ethnoecology_data)

# Helper: plot any index data frame as a horizontal bar chart.
# `value_col` = name of the column to plot on the x-axis (the index value).
plot_index <- function(df, value_col, title, fill = "#2d6a4f") {
  df      <- as.data.frame(df)
  grp_col <- grep("landscape|informant|species|group",
                  names(df), value = TRUE, ignore.case = TRUE)[1]
  ggplot(df, aes(x = reorder(.data[[grp_col]], .data[[value_col]]),
                 y = .data[[value_col]])) +
    geom_col(fill = fill) +
    coord_flip() +
    labs(title = title, x = NULL, y = value_col) +
    theme_minimal(base_size = 12)
}

## ----data-table, echo = FALSE-------------------------------------------------
knitr::kable(
  head(ethnoecology_data, 6),
  digits   = 0,
  caption  = "First six rows of the example ethnoecology data included with ethnoecologyR"
)

## ----data-prep----------------------------------------------------------------
long_data <- ethno_data_prep(ethnoecology_data, pivot_long = TRUE)
knitr::kable(head(long_data, 8),
             caption = "Long-format data returned by ethno_data_prep()")

## ----LUs, fig.width=8, fig.height=4.5, fig.cap="Landscape Use (LU) per landscape unit"----
lu_result <- LUs(ethnoecology_data)
knitr::kable(lu_result, caption = "Landscape Use index (LU)")
plot_index(lu_result, "LU", "Landscape Use (LU)")

## ----CIEs, fig.width=8, fig.height=4.5, fig.cap="Cultural Importance in Ecology (CIE) per landscape unit"----
cie_result <- CIEs(ethnoecology_data)
knitr::kable(cie_result, digits = 3, caption = "Cultural Importance in Ecology (CIE)")
plot_index(cie_result, "CIE", "Cultural Importance in Ecology (CIE)")

## ----FCs, fig.width=8, fig.height=4.5, fig.cap="Frequency of Citation (FC) per landscape unit"----
fc_result <- FCs_eco(ethnoecology_data)
knitr::kable(fc_result, caption = "Frequency of Citation (FC)")
plot_index(fc_result, "FC", "Frequency of Citation (FC)")

## ----FLs, fig.width=8, fig.height=4.5, fig.cap="Fidelity Level (FL) for the use_forest category"----
fl_result <- FLs_eco(ethnoecology_data, "use_forest")
knitr::kable(fl_result, digits = 1, caption = "Fidelity Level (FL) for use_forest")
plot_index(fl_result, "FL", "Fidelity Level (FL) — use_forest")

## ----NUs, fig.width=8, fig.height=4.5, fig.cap="Number of Uses (NU) per landscape unit"----
nu_result <- NUs_eco(ethnoecology_data)
knitr::kable(nu_result, caption = "Number of Uses (NU)")
plot_index(nu_result, "NU", "Number of Uses (NU)")

## ----CVes, fig.width=8, fig.height=4.5, fig.cap="Cultural Value (CVe) per landscape unit"----
cv_result <- CVe_eco(ethnoecology_data)
knitr::kable(cv_result, digits = 3, caption = "Cultural Value (CVe)")
plot_index(cv_result, "CVe", "Cultural Value (CVe)")

## ----ranking, fig.width=8, fig.height=4, fig.cap="Top 5 landscape units by Cultural Importance (CIE)"----
top_cie <- ethno_ranking(ethnoecology_data,
                         index_function = CIEs,
                         top_n = 5)
knitr::kable(top_cie, digits = 3, caption = "Top 5 landscape units by CIE")
plot_index(top_cie, "CIE", "Top 5 landscape units by CIE")

## ----richness, fig.width=8, fig.height=4.5, fig.cap="Richness per landscape unit"----
rich_result <- ethno_richness(ethnoecology_data, group_by = "landscape_unit")
knitr::kable(rich_result, caption = "Richness per landscape unit")
plot_index(rich_result, "Richness", "Richness per landscape unit")

## ----habitat, fig.width=9, fig.height=4.5, fig.cap="Habitat × use-category cross-table"----
hab_result <- ethno_habitat_analysis(ethnoecology_data)
knitr::kable(hab_result, caption = "Habitat × use-category cross-table")

## ----chord-landscape, fig.width=8, fig.height=8, fig.cap="Chord diagram of landscape units and uses"----
ethno_landscape_chord(ethnoecology_data, by = "landscape_unit")

## ----chord-informant, fig.width=8, fig.height=8, fig.cap="Chord diagram of informants and uses"----
ethno_landscape_chord(ethnoecology_data, by = "informant")

## ----alluvial, fig.width=10, fig.height=7, fig.cap="Alluvial plot of landscape uses"----
ethno_landscape_alluvial(ethnoecology_data)

## ----network, fig.width=9, fig.height=7, fig.cap="Bipartite network of landscape units and use categories"----
ethno_landscape_network(ethnoecology_data, by = "landscape_unit")

## ----radial, fig.width=8, fig.height=8, fig.cap="Radial plot of three ethnoecology indices"----
landscape_radial_plot(ethnoecology_data, list(LUs, CIEs, FCs_eco))

## ----radial-grid, fig.width=10, fig.height=9, fig.cap="Comparison of four ethnoecology indices in a 2×2 grid"----
library(cowplot)

plots <- list(
  LU  = landscape_radial_plot(ethnoecology_data, list(LUs)),
  CIE = landscape_radial_plot(ethnoecology_data, list(CIEs)),
  FC  = landscape_radial_plot(ethnoecology_data, list(FCs_eco)),
  NU  = landscape_radial_plot(ethnoecology_data, list(NUs_eco))
)

cowplot::plot_grid(
  plotlist    = plots,
  labels      = names(plots),
  nrow        = 2,
  align       = "hv",
  label_size  = 14
)

## ----resilience, fig.width=8, fig.height=5, fig.cap="Landscape Resilience Index (LRI) per landscape unit"----
lri_result <- ethno_resilience(ethnoecology_data)
knitr::kable(lri_result, digits = 3, caption = "Landscape Resilience Index (LRI)")
plot_index(lri_result, "LRI", "Landscape Resilience Index (LRI)")

## ----bayes, eval = FALSE------------------------------------------------------
# fit <- ethno_bayes_land(ethnoecology_data, iter = 500, chains = 2)
# print(fit, pars = c("theta[1]", "theta[2]", "theta[3]"))

## ----bootstrap----------------------------------------------------------------
set.seed(42)
boot_ci <- ethno_boot_land(ethnoecology_data,
                           index_function = CIEs,
                           R = 100)
print(boot_ci)

## ----workflow, eval = FALSE---------------------------------------------------
# library(ethnoecologyR)
# library(dplyr)
# library(ggplot2)
# 
# # 1. Load data
# data(ethnoecology_data)
# 
# # 2. Prepare
# long <- ethno_data_prep(ethnoecology_data, pivot_long = TRUE)
# 
# # 3. Core indices
# lu  <- LUs(ethnoecology_data)
# cie <- CIEs(ethnoecology_data)
# nu  <- NUs_eco(ethnoecology_data)
# 
# # 4. Rank landscape units by cultural importance
# top <- ethno_ranking(ethnoecology_data, index_function = CIEs, top_n = 5)
# print(top)
# 
# # 5. Habitat × use cross-table
# habitat <- ethno_habitat_analysis(ethnoecology_data)
# print(habitat)
# 
# # 6. Resilience index
# lri <- ethno_resilience(ethnoecology_data)
# print(lri)
# 
# # 7. Visualizations
# ethno_landscape_network(ethnoecology_data)
# ethno_landscape_alluvial(ethnoecology_data)
# landscape_radial_plot(ethnoecology_data, list(LUs, CIEs, FCs_eco))

