# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
#                                                                             #
#   _packages.R                                                                #
#   Install R packages required by course code (if not already installed).     #
#                                                                             #
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

# 0. Packages used across course .R files -------------------------------------

packages = c(
  "blme",         # Bayesian regularised mixed models (00)
  "broom",        # Tidying functions for models (01)
  "coda",         # MCMC diagnostics (01, 06, 07, 08, 09)
  "doParallel",   # Parallel backend (00)
  "dplyr",        # Data manipulation (00–11)
  "forcats",      # Factor handling (04, 05)
  "foreach",      # Loops (00)
  "ggplot2",      # Plotting (00–09)
  "ggridges",     # Ridge plots (04, 05)
  "HDInterval",   # HDI summaries (01, 04, 05, 10, 11)
  "igraph",       # Network graphs (10, 11)
  "MCMCvis",      # MCMC visualisation (10, 11)
  "matrixStats",  # Maxtrix tools for posterior samples (08, 09)
  "meta",         # Meta-analysis, NMA (02, 03)
  "metafor",      # Meta-analysis (02, 03)
  "mice",         # Multiple imputation (00)
  "micemd",       # 2l.jomo multilevel MI (00)
  "miceadds",     # MICE utilities (00)
  "mitml",        # Multilevel MI (00)
  "netmeta",      # Network meta-analysis (10, 11)
  "purrr",        # Functional programming (00–05)
  "readxl",       # Excel input (00–05, 10, 11)
  "rjags",        # JAGS interface (04, 05, 10, 11)
  "runjags",      # run.jags() (01, 04–09)
  "stringr",      # String utilities (02–05, 10, 11)
  "tidyr",        # Reshape/tidy (00–05)
  "tidyverse",    # Core tidy packages (10, 11)
  "xlsx"          # Excel read/write (10, 11)
)

# Install any that are not installed -----------------------------------------

missing = packages[!(packages %in% installed.packages()[, "Package"])]
if (length(missing) > 0L) {
  install.packages(missing, dependencies = TRUE)
} else {
  message("All required packages are already installed.")
}
