## ----setup, include=FALSE-----------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.width = 7,
  fig.height = 4.5,
  message = FALSE,
  warning = FALSE
)

## ----packages-----------------------------------------------------------------
library(VCMoE)

## ----simulate-----------------------------------------------------------------
set.seed(52)

sim <- simulate_vcmoe_binomial(
  n = 300,
  k = 2,
  seed = 52,
  trials = 30,
  separation = 2.5,
  scenario = "well_separated"
)

head(sim$data)
summary(sim$data$success / sim$data$trials)

## ----fit----------------------------------------------------------------------
fit <- vcmoe_fit(
  cbind(success, failure) ~ z1 | x1,
  data = sim$data,
  u = "u",
  family = "binomial",
  k = 2,
  bandwidth = 0.50,
  u_grid = seq(0.15, 0.85, length.out = 5),
  control = list(maxit = 100, n_starts = 2, seed = 53, warn_ambiguous = FALSE)
)

fit

## ----coefficients-------------------------------------------------------------
coef(fit, "expert")[, , "z1"]

## ----predictions--------------------------------------------------------------
head(predict(fit, type = "component"))
head(predict(fit, type = "mean"))
head(predict(fit, type = "posterior"))

## ----posterior-confidence-----------------------------------------------------
post <- predict(fit, type = "posterior")
mean(apply(post, 1, max))

## ----diagnostics--------------------------------------------------------------
diagnostics <- vcmoe_diagnostics(fit)
diagnostics[, c("u", "converged", "ambiguous", "posterior_entropy", "effective_n")]

## ----coefficient-plot---------------------------------------------------------
plot_coefficients(fit, "expert")

## ----posterior-plot-----------------------------------------------------------
plot_posterior(fit)

## ----bernoulli-format, eval=FALSE---------------------------------------------
# bern <- simulate_vcmoe_binomial(n = 300, k = 2, trials = 1)
# 
# bern_fit <- vcmoe_fit(
#   y ~ z1 | x1,
#   data = bern$data,
#   u = "u",
#   family = "binomial",
#   k = 2,
#   bandwidth = 0.50
# )

