fit_abc: Approximate Bayesian Computation (ABC) for System dynamic...

Description Usage Arguments Examples

View source: R/abc.R

Description

Approximate Bayesian Computation (ABC) for System dynamic models

Usage

1
fit_abc(lf, n_posterior = 300, epsilon = NA, target_acc = 0.1, n_test = 100)

Arguments

lf

a "model_likefree" object with a compile model and data

n_posterior

number of posteriors to collect

epsilon

threshold for collecting

target_acc

target acceptance rate

n_test

number of simulations for testing threshold

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
library(odin2data)

## Example 1: model without warmup stage
test_data <- data.frame(
  t = 1:5,
  incidence = c(20, 49, 109, 184, 206) / 1000
)

### Load the model file
f <- system.file("models/SIR.txt", package = "odin2data")
test_m <- odin::odin(f, verbose=F)


### Generate a prior and set up it as a list
r_prior <- function() {
  list(beta = runif(1, 1, 10), gamma = runif(1, .1, 1))
}

d_prior <- function(pars) {
  dunif(pars$beta, 1, 10, log = T) + dunif(pars$gamma, .1, 1, log = T)
}

times = seq(0, 10, 0.2)
y0 <- c(995, 5, 0)

### Compile all elements as a simulation model
sim <- odin2data::compile_model(d_prior, r_prior, y0, ts_sim = times, m_sim = test_m)

### Compile the model with data
lf <- odin2data::compile_model_likefree(test_data, sim)

### Model fitting
post <- odin2data::fit_abc(lf, 100, target_acc = 0.2)
summary(post)


## Example 2: model with warmup stage
fn_pass_y0 <- function(ys) {
ys[nrow(ys), 2:4]
}

test_data <- data.frame(
  t = 1:5,
  incidence = rep(0.009580023, 5)
)

sim <- odin2data::compile_model(d_prior, r_prior, y0, ts_sim = times, m_sim = test_m,
                                m_wp = test_m, t_wp = 100, fn_pass_y0 = fn_pass_y0)

lf <- odin2data::compile_model_likefree(test_data, sim)

### Model fitting
post <- odin2data::fit_abc(lf, 100, target_acc = 0.2)
summary(post)

TimeWz667/odin2data documentation built on Sept. 30, 2021, 4:11 p.m.