## ----include = FALSE----------------------------------------------------------
ok <- requireNamespace("MASS", quietly = TRUE)
knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7,
                      fig.height = 4, dpi = 72, out.width = "100%", eval = ok)

## -----------------------------------------------------------------------------
library(SynthesizerPlus)
data(Boston, package = "MASS")
fit <- fit_synthesizer(Boston)

## -----------------------------------------------------------------------------
grid <- c(1, 0.8, 0.6, 0.4, 0.2, 0)
dist <- t(sapply(grid, function(cl) {
  syn <- generate(fit, closeness = cl, seed = 1)
  c(closeness = cl,
    `discriminator AUC` = discriminator_auc(Boston, syn, seed = 1),
    `DCR ratio` = dcr(Boston, syn, seed = 1)$ratio,
    `marginal distance` = mean(marginal_metrics(Boston, syn)$distance),
    `association error` = mean(abs(association_matrix(syn) - association_matrix(Boston))))
}))
round(dist, 3)

## -----------------------------------------------------------------------------
near <- generate(fit, seed = 2)
far <- generate(fit, perturb = perturbation(noise = 0.6, swap = 0.3), seed = 2)
c(near = dcr(Boston, near, seed = 1)$ratio, far = dcr(Boston, far, seed = 1)$ratio)
round(rbind(real = sapply(Boston[c("medv", "lstat", "rm")], sd),
            near = sapply(near[c("medv", "lstat", "rm")], sd),
            far = sapply(far[c("medv", "lstat", "rm")], sd)), 2)

## -----------------------------------------------------------------------------
plot_dcr(dcr(Boston, far, seed = 1))

## -----------------------------------------------------------------------------
scenario <- perturbation(shift = c(crim = 1), scale = c(rm = 1.5), dependence = 1.3)
scenario
stress <- generate(fit, perturb = scenario, seed = 3)

## ----fig.height = 3.5---------------------------------------------------------
plot_marginals(Boston, stress, vars = c("crim", "rm", "medv"))

## -----------------------------------------------------------------------------
fit_i <- fit_synthesizer(iris[c(1:50, 51:70, 101:105), ])   # unbalanced species
round(rbind(
  real = prop.table(table(iris[c(1:50, 51:70, 101:105), "Species"])),
  `T = 1` = prop.table(table(generate(fit_i, n = 5000, seed = 1)$Species)),
  `T = 3` = prop.table(table(generate(fit_i, n = 5000, seed = 1,
                                      perturb = perturbation(temperature = 3))$Species))
), 2)

## -----------------------------------------------------------------------------
cal <- calibrate_closeness(fit, Boston, target = 0.85, metric = "auc",
                           grid = seq(0, 1, by = 0.1), refine = 3)
cal
plot(cal)

## -----------------------------------------------------------------------------
syn <- generate(fit, closeness = cal$closeness, seed = 99)
discriminator_auc(Boston, syn, seed = 5)

## -----------------------------------------------------------------------------
big <- augment_data(Boston[1:60, ], n = 1000, closeness = 0.8, seed = 1)
dim(big)

