## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  echo = TRUE, message = FALSE, warning = FALSE,
  collapse = TRUE, comment = "#>"
)
have_cmdstan <- requireNamespace("cmdstanr", quietly = TRUE)

## ----snapshot-baseline, eval=have_cmdstan-------------------------------------
library(gdpar)

set.seed(42L)
n <- 200L
df <- data.frame(x1 = rnorm(n), x2 = rnorm(n))
df$y <- 1 + 0.6 * df$x1 - 0.4 * df$x2 + rnorm(n, sd = 0.3)

spec <- amm_spec(a = ~ x1 + x2)

fit <- gdpar(
  formula       = y ~ x1 + x2,
  family        = gdpar_family("gaussian"),
  amm           = spec,
  data          = df,
  iter_warmup   = 500L,
  iter_sampling = 500L,
  chains        = 2L,
  refresh       = 0L,
  verbose       = FALSE,
  seed          = 42L
)

snap_baseline <- gdpar_snapshot_fit(fit)
names(snap_baseline)

## ----snapshot-inspect, eval=have_cmdstan--------------------------------------
snap_baseline$structural$p
snap_baseline$structural$components
snap_baseline$discrete
snap_baseline$sanity
head(names(snap_baseline$continuous$theta_ref))
snap_baseline$continuous$theta_ref[[1L]]

## ----persist-illustrative, eval=FALSE-----------------------------------------
# golden <- c(
#   list(
#     schema_version  = 1L,
#     scenario_id     = "my_baseline",
#     gdpar_version   = as.character(utils::packageVersion("gdpar")),
#     cmdstan_version = cmdstanr::cmdstan_version(),
#     R_version       = R.version.string,
#     seed            = 42L,
#     n               = nrow(df),
#     p               = 1L,
#     n_warmup        = 500L,
#     n_sampling      = 500L,
#     n_chains        = 2L,
#     generated_at    = format(Sys.time(), "%Y-%m-%d %H:%M:%S %Z")
#   ),
#   snap_baseline
# )
# saveRDS(golden, file = "tests/regression/golden_my_baseline.rds")

## ----compare-illustrative, eval=FALSE-----------------------------------------
# golden <- readRDS("tests/regression/golden_my_baseline.rds")
# 
# fit_new <- gdpar(
#   formula       = y ~ x1 + x2,
#   family        = gdpar_family("gaussian"),
#   amm           = spec,
#   data          = df,
#   iter_warmup   = golden$n_warmup,
#   iter_sampling = golden$n_sampling,
#   chains        = golden$n_chains,
#   refresh       = 0L,
#   verbose       = FALSE,
#   seed          = golden$seed
# )
# 
# snap_new <- gdpar_snapshot_fit(fit_new)
# cmp <- gdpar_golden_compare(snap_new, golden, k_sigma = 3)
# cmp$passed
# cmp$by_layer
# if (!cmp$passed) print(cmp$failures, row.names = FALSE)

## ----testthat-illustrative, eval=FALSE----------------------------------------
# test_that("my_baseline regression", {
#   testthat::skip_if(Sys.getenv("MY_REGRESSION_CHECK") != "1",
#                     "Set MY_REGRESSION_CHECK=1 to run the regression.")
#   testthat::skip_if_not_installed("cmdstanr")
# 
#   golden <- readRDS(
#     system.file("regression", "golden_my_baseline.rds",
#                 package = "my_pkg")
#   )
#   df <- build_my_data(seed = golden$seed, n = golden$n)
#   fit <- my_fit_wrapper(df, golden)
#   snap <- gdpar_snapshot_fit(fit)
#   cmp  <- gdpar_golden_compare(snap, golden, k_sigma = 3)
# 
#   fail_info <- if (!cmp$passed) {
#     paste0(
#       "\nFailures by layer: ",
#       paste(names(cmp$by_layer), cmp$by_layer, sep = "=",
#             collapse = ", "),
#       "\nFailure rows:\n",
#       paste(utils::capture.output(print(cmp$failures, row.names = FALSE)),
#             collapse = "\n")
#     )
#   } else ""
# 
#   expect_true(cmp$passed, info = fail_info)
# })

