## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")
library(babebi)

## ----data---------------------------------------------------------------------
S <- matrix(c(1, 0.65, 0.65, 1.5), 2)
scenario <- bivariate_scenario(N = 12, beta = 0.6, gamma = 0.2,
                               covariance = S)
dat <- simulate_bivariate(scenario, seed = 22, format = "long")
head(dat, 8)
fit_model_id <- function(model) {
  fit_bivariate(dat, id, time, rater, y, model = model,
                time_order = c("pre", "post"),
                rater_order = c("r1", "r2"))
}
f3 <- fit_model_id("M3")
f3$sample_cov

## ----reference----------------------------------------------------------------
ref <- posterior_bivariate(f3, prior = "reference")
contrast_bivariate(ref, "reference")
contrast_bivariate(ref, "discrepancy")

## ----prior--------------------------------------------------------------------
prior <- bivariate_prior(location = c(0, 0), kappa = 0.5,
                         shape = 3, covariance = diag(2))
post <- posterior_bivariate(f3, prior)
contrast_bivariate(post, "reference", threshold = 0.3)
contrast_bivariate(post, "average", rope = c(-0.2, 0.2))
contrast_bivariate(post, "weighted", weight = 0.25)
contrast_bivariate(post, "discrepancy", target = "predictive")

## ----draw---------------------------------------------------------------------
head(draw_bivariate(post, n_draws = 1000, seed = 23, predictive = TRUE))

## ----evidence-----------------------------------------------------------------
bayesfactor_bivariate(post, "reference", null = 0)
bayesfactor_bivariate(post, "discrepancy", null = 0)
bayesfactor_bivariate(post, "discrepancy", hypothesis = "interval",
                      bounds = c(-0.2, 0.2))
prior_wide <- bivariate_prior(c(0, 0), kappa = 0.1,
                             shape = 3, covariance = 2 * diag(2))
sensitivity_bivariate(f3, list(initial = prior, wider = prior_wide),
                      contrast = "reference", null = 0)

## ----covariance-comparison----------------------------------------------------
models <- c("M0", "M1", "M2", "M3")
posts <- lapply(models, function(m) posterior_bivariate(fit_model_id(m), prior))
compare_bivariate(posts, reference = "M0",
                  model_prior = c(M0 = 1, M1 = 1, M2 = 1, M3 = 1))

## ----agreement----------------------------------------------------------------
ag <- agreement_bivariate(post, margin = 0.5, min_coverage = 0.8,
                          n_draws = 2000, seed = 24)
print(ag)
ag$practical

## ----simulation---------------------------------------------------------------
methods <- list(legacy = "legacy", paired_t = "paired_t",
                exact_M0 = list(model = "M0", prior = "reference"),
                exact_M3 = list(model = "M3", prior = "reference"),
                proper_M3 = list(model = "M3", prior = prior))
val <- validate_bivariate(scenario, methods, contrast = "reference",
                          R = 20, n_draws = 1000, seed = 25)
subset(summarise_validation(val), metric %in% c("bias", "rmse", "coverage"))
compare_validation(val, "exact_M3", "legacy", metric = "mse")

## ----calibrated---------------------------------------------------------------
empirical <- scenario_from_fit(f3)
empirical$location
empirical$covariance
heavy <- bivariate_scenario(12, 0.6, 0.2, S,
                            distribution = "student", df = 5)
scenario_truth(heavy, "discrepancy", margin = 0.5)

## ----sbc----------------------------------------------------------------------
sbc <- sbc_bivariate(N = 8, model = "M3", prior = prior,
                     contrast = "reference", R = 20,
                     n_draws = 100, bins = 5, seed = 26)
names(sbc)

## ----ordinal-data-------------------------------------------------------------
ord <- simulate_ordinal(N = 20, coefficients = c(-0.3, 0.1, 0.5, 0.2),
                        random_covariance = matrix(c(0.5, 0.2, 0.2, 0.6), 2),
                        cutpoints = c(0, 1),
                        category_order = c("low", "middle", "high"), seed = 27)
op <- ordinal_prior(coefficient_mean = rep(0, 4), coefficient_sd = rep(1, 4),
                    random_df = 6, random_covariance = 0.5 * diag(2),
                    cutpoint_mean = 1, cutpoint_sd = 0.5)

## ----ordinal-fit, eval=FALSE--------------------------------------------------
# of <- fit_ordinal(ord, id, time, rater, y,
#                   category_order = c("low", "middle", "high"),
#                   time_order = c("pre", "post"), rater_order = c("r1", "r2"),
#                   prior = op, chains = 4, warmup = 1000, draws = 2000, seed = 28)
# ordinal_diagnostics(of)
# oc <- contrast_ordinal(of, "reference")
# oc$diagnostics
# if (all(ordinal_diagnostics(of)$adequate) && isTRUE(oc$diagnostics$adequate)) {
#   print(oc)
#   print(predict_ordinal(of))
# }

