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

## ----regression-data----------------------------------------------------------
library(spDBL)
set.seed(42)
n <- 120L
X <- cbind(intercept = 1, x1 = seq(-1, 1, length.out = n),
           x2 = rnorm(n), x3 = rnorm(n))
B <- rbind(c(0.8, -0.3), c(1.2, 0.7), c(0, 0), c(0, 0))
Sigma <- matrix(c(0.7, 0.25, 0.25, 0.5), 2)
Y <- X %*% B + matrix(rnorm(2 * n), n, 2) %*% chol(Sigma)

## ----posterior----------------------------------------------------------------
p <- ncol(X)
q <- ncol(Y)
post <- MNIW_R(X, Y, H = diag(n), v = q + 2, S = diag(q),
               C = matrix(0, p, q), Vb = diag(p))
analytical <- compute_analytical_waic(
  Y, X, Mn = post$Vbnew, mn = post$Cnew,
  Psi_n = post$Snew, nu_n = post$vnew)
unlist(analytical[c("WAIC", "lppd", "p_waic")])
head(analytical$pointwise)

## ----sampled-waic-------------------------------------------------------------
set.seed(123)
draws <- mniw::rMNIW(4000L, Lambda = post$Cnew, Sigma = post$Vbnew,
                     Psi = post$Snew, nu = post$vnew, prec = FALSE)
sampled <- compute_sample_waic(Y, X, draws$X, draws$V,
                               return_log_lik = TRUE)
comparison <- data.frame(
  Method = c("Analytical", "Posterior draws"),
  lppd = c(analytical$lppd, sampled$lppd),
  p_waic = c(analytical$p_waic, sampled$p_waic),
  WAIC = c(analytical$WAIC, sampled$WAIC))
knitr::kable(comparison, digits = 3)

## ----loo-comparison-----------------------------------------------------------
if (requireNamespace("loo", quietly = TRUE)) {
  reference <- loo::waic(sampled$log_lik)
  comparison <- rbind(comparison, data.frame(
    Method = "loo (same draws)",
    lppd = reference$estimates["elpd_waic", "Estimate"] +
      reference$estimates["p_waic", "Estimate"],
    p_waic = reference$estimates["p_waic", "Estimate"],
    WAIC = reference$estimates["waic", "Estimate"]))
  knitr::kable(comparison, digits = 3)
}

## ----sequential-comparison----------------------------------------------------
profile <- do.call(rbind, lapply(seq_len(p), function(k) {
  X_k <- X[, seq_len(k), drop = FALSE]
  fit <- MNIW_R(X_k, Y, H = diag(n), v = q + 2, S = diag(q),
                C = matrix(0, k, q), Vb = diag(k))
  score <- compute_analytical_waic(Y, X_k, fit$Vbnew, fit$Cnew,
                                   fit$Snew, fit$vnew)
  data.frame(Model = if (k == 1L) "Intercept" else
    paste0("Intercept + x1:", k - 1L),
    lppd = score$lppd, p_waic = score$p_waic, WAIC = score$WAIC)
}))
knitr::kable(profile, digits = 3)

## ----comparison-profile, fig.width=8, fig.height=7, eval=requireNamespace("ggplot2", quietly = TRUE)----
metrics <- c("WAIC", "lppd", "p_waic")
labels <- c("WAIC (lower is better)", "lppd", "p_WAIC")
long <- do.call(rbind, lapply(seq_along(metrics), function(j)
  data.frame(Model = profile$Model, Metric = labels[j],
             Value = profile[[metrics[j]]])))
long$Model <- factor(long$Model, levels = profile$Model)
long$Metric <- factor(long$Metric, levels = labels)
colors <- stats::setNames(c("#c85200", "#2b5c8f", "#7570b3"), labels)
ggplot2::ggplot(long, ggplot2::aes(Model, Value, color = Metric, group = Metric)) +
  ggplot2::geom_line(linewidth = 1.1) +
  ggplot2::geom_point(size = 3) +
  ggplot2::facet_wrap(~Metric, ncol = 1, scales = "free_y") +
  ggplot2::scale_color_manual(values = colors) +
  ggplot2::theme_minimal(base_size = 12) +
  ggplot2::theme(legend.position = "none",
    axis.text.x = ggplot2::element_text(angle = 30, hjust = 1),
    panel.grid.minor = ggplot2::element_blank()) +
  ggplot2::labs(title = "Sequential MNIW model comparison",
                x = "Covariates", y = NULL)

