## ----setup, include = FALSE---------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----data---------------------------------------------------------------------
library(oda)

# Cross-classification: rows = adjustment type, cols = motivation.
#                    Indiv (0)  Comm (1)   total
#  Ridges       (1)      85       173       258
#  Shifting     (2)      65       170       235
#  Relocation   (3)     172        10       182
#  Intensified  (4)      45         0        45
#                       367       353       720

motivation  <- c(rep(0L,  85), rep(1L, 173),   # adjustment = 1
                 rep(0L,  65), rep(1L, 170),   # adjustment = 2
                 rep(0L, 172), rep(1L,  10),   # adjustment = 3
                 rep(0L,  45), rep(1L,   0))   # adjustment = 4
adjustment  <- c(rep(1L, 258), rep(2L, 235),
                 rep(3L, 182), rep(4L,  45))

table(adjustment, motivation,
      dnn = c("Adjustment (1=Ridges,2=Shifting,3=Relocation,4=Intensified)",
              "Motivation (0=Individual, 1=Community)"))

## ----fit-canonical, eval=FALSE------------------------------------------------
# # Canonical reference run (mc_iter = 25000L; not evaluated in CRAN vignette)
# fit <- oda_fit(
#   x         = adjustment,
#   y         = motivation,
#   attr_type = "categorical",
#   mc_iter   = 25000L,
#   loo       = "on"
# )

## ----fit----------------------------------------------------------------------
# CRAN-safe run: mc_iter = 500L for vignette rendering speed.
# Training rule, ESS, and confusion matrix are identical to the canonical run.
fit <- oda_fit(
  x         = adjustment,
  y         = motivation,
  attr_type = "categorical",
  mc_iter   = 500L,
  mc_seed   = 42L,
  loo       = "on"
)

## ----print-fit----------------------------------------------------------------
print(fit)

## ----confusion----------------------------------------------------------------
# Confusion matrix: actual motivation (rows) x predicted motivation (cols)
conf_mat <- matrix(
  c(fit$confusion$TN, fit$confusion$FP,
    fit$confusion$FN, fit$confusion$TP),
  nrow = 2L, byrow = TRUE,
  dimnames = list(Actual    = c("Indiv(0)", "Comm(1)"),
                  Predicted = c("Indiv(0)", "Comm(1)"))
)
print(conf_mat)

## ----metrics------------------------------------------------------------------
summary(fit)

## ----pv-----------------------------------------------------------------------
# Predictive value: accuracy when the model makes a prediction into each class
pv_indiv <- fit$confusion$TN / (fit$confusion$TN + fit$confusion$FN)
pv_comm  <- fit$confusion$TP / (fit$confusion$TP + fit$confusion$FP)
cat("PV Individual (0):", round(pv_indiv * 100, 1), "%\n")
cat("PV Community  (1):", round(pv_comm  * 100, 1), "%\n")

