---
title: "imabc"
output: rmarkdown::html_vignette
bibliography: references.bib
vignette: >
  %\VignetteIndexEntry{imabc}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

```{r, include = FALSE}
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
```

# Introduction to IMABC

A thorough introduction to Incremental Mixture Approximate Bayesian Computation (IMABC) method can be found in @rutter_microsimulation_2019. Here we will walk through the code found in @imabc_pkg.

```{r setup}
library(imabc)
```

## Step 1 - Define the priors
For a more detailed explanation of how the priors functions work, see the `vignette("priors")`

```{r priors, eval = FALSE}
priors <- define_priors(
  # x1: Uniform Prior (from base R)
  x1 = add_prior(
    dist_base_name = "unif",
    min = 0.2, max = 0.9
  ),
  # x2: Truncated Normal (from truncnorm package)
  add_prior(
    parameter_name = "x2",
    density_fn = "dtruncnorm",
    mean = 0.5, sd = 0.05, a = 0.4, b = 0.8, # a = min and b = max
    min = 0.4, max = 0.8 # User must specify both in truncnorm
  ),
  # V3: Fixed parameter (not calibrated)
  add_prior(0.5)
)
```

## Step 2 - Define target values
A more detailed description will be available in the near future at `vignette("targets")`

```{r targets, eval = FALSE}
targets <- define_targets(
  # G1: Grouped targets include T1 and T2
  G1 = group_targets(
    T1 = add_target(
      target = 1.5,
      starting_range = c(1.0, 2.0),
      stopping_range = c(1.49, 1.51)
    ),
    add_target(
      target_name = "T2",
      target = 0.5,
      starting_range = c(0.2, 0.9),
      stopping_range = c(0.49, 0.51)
    )
  )
)
```

## Step 3 - Define the target function
A more detailed description will be available in the near future at `vignette("target_functions")`
```{r target_fn, eval = FALSE}
fn1 <- function(x1, x2) { x1 + x2 + sample(c(-1, 1), 1)*rnorm(1, 0, 0.1) }
fn2 <- function(x1, x2) { x1 * x2 + sample(c(-1, 1), 1)*rnorm(1, 0, 0.1) }
fn <- function(x1, x2) {
  res <- c()
  res["T2"] <- fn1(x1, x2)
  res["T1"] <- fn2(x1, x2)
  return(res)
}
target_fun <- define_target_function(
  targets, priors, FUN = fn, use_seed = FALSE
)
```

## Step 4 - Calibrate the model
The primary function is imabc(). The inputs and their descriptions are as follows:
```{r imabc, eval = FALSE}
calibration_results <- imabc(
  priors = priors,
  targets = targets_nogroup,
  target_fun = target_fun,
  seed = 54321,
  N_start = 2000,
  N_centers = 2,
  Center_n = 500,
  N_cov_points = 50,
  N_post = 100
)
```
