## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = TRUE
)

library(ecoGLMM)

## ----installation, eval=FALSE-------------------------------------------------
# # Install the CRAN version when available:
# install.packages("ecoGLMM")
# 
# # Install the development version from GitHub:
# # install.packages("remotes")
# remotes::install_github("andre-fcsantos/ecoGLMM", upgrade = "never")
# 
# library(ecoGLMM)

## ----simulate-data------------------------------------------------------------
set.seed(123)

sites <- paste0("Site_", seq_len(10))
period_levels <- c("T-0", "T-1", "T-2", "T-3", "T-4")

example_data <- expand.grid(
  site = sites,
  period = period_levels,
  replicate = seq_len(3),
  stringsAsFactors = FALSE
)

n <- nrow(example_data)
example_data$forest <- runif(n, 10, 95)
example_data$urban <- runif(n, 0, 60)

site_effect <- rnorm(length(sites), 0, 0.25)
names(site_effect) <- sites

linear_predictor <-
  -0.5 +
  0.015 * example_data$forest -
  0.012 * example_data$urban +
  site_effect[example_data$site]

example_data$acoustic_index <- plogis(
  linear_predictor + rnorm(n, 0, 0.35)
)

example_data$acoustic_index <- adjust_beta(example_data$acoustic_index)

head(example_data)

## ----configuration------------------------------------------------------------
response_config <- data.frame(
  response = "acoustic_index",
  family = "beta",
  label = "Simulated acoustic index"
)

## ----acoustic-preparation, eval=FALSE-----------------------------------------
# # analysis_data <- prepare_acoustic_indices(
# #   data = raw_data,
# #   ndsi = "NDSI",
# #   beta_responses = c("AEI", "ACT"),
# #   ndsi_output = "NDSI_beta"
# # )

## ----run-analysis-------------------------------------------------------------
fit <- run_ecoglmm(
  data = example_data,
  config = response_config,
  predictors = c("forest", "urban"),
  period = "period",
  group = "site",
  period_levels = period_levels,
  standardize = TRUE,
  complete_cases = TRUE,
  include_additive = TRUE,
  include_interactions = TRUE,
  competitive_delta = 2
)

fit
summary(fit)

## ----inspect-results----------------------------------------------------------
fit$selection$acoustic_index
fit$interaction_tests$acoustic_index
fit$coefficients
fit$preparation

## ----diagnostics, eval=FALSE--------------------------------------------------
# diagnostics <- diagnose_models(
#   fit,
#   nsim = 1000,
#   seed = 123
# )
# 
# diagnostics$summary
# 
# # Inspect the DHARMa residual plot:
# plot(diagnostics$residuals$acoustic_index)

## ----figures------------------------------------------------------------------
plot_effect(fit, "acoustic_index")
plot_selection(fit, "acoustic_index", metric = "delta")
plot_selection(fit, "acoustic_index", metric = "weight")
plot_coefficients(fit)

## ----export, eval=FALSE-------------------------------------------------------
# exported_files <- export_ecoglmm(
#   object = fit,
#   path = file.path(tempdir(), "ecoGLMM_results"),
#   diagnostics = diagnostics,
#   figures = TRUE
# )
# 
# exported_files

## ----locate-template, eval=FALSE----------------------------------------------
# template_path <- system.file(
#   "examples",
#   "ecoGLMM_template.R",
#   package = "ecoGLMM"
# )
# 
# file.copy(
#   from = template_path,
#   to = file.path(tempdir(), "ecoGLMM_template.R"),
#   overwrite = FALSE
# )
# 
# file.edit(file.path(tempdir(), "ecoGLMM_template.R"))

## ----display-generic-template, echo=FALSE, results="asis"---------------------
template_path <- system.file(
  "examples",
  "ecoGLMM_template.R",
  package = "ecoGLMM"
)

if (!nzchar(template_path)) {
  stop("The generic analysis template was not found in the installed package.")
}

template_code <- readLines(template_path, warn = FALSE)

cat(
  "```r\n",
  paste(template_code, collapse = "\n"),
  "\n```\n",
  sep = ""
)

