knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

Load package

library(odin2data)

Input data

test_data <- data.frame(
  t = 1:5,
  incidence = c(20, 49, 109, 184, 206) / 1000
)
knitr::kable(test_data)

Construct a model with odin

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

Set up prior distribution with 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)
}

Compile the model components

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

sim <- compile_model(r_prior = r_prior, d_prior = d_prior, y0 = y0, ts_sim = times, m_sim = test_m)
lf <- compile_model_likefree(test_data, sim)

Test distance calculation

pars <- lf$Model$r_prior()
calc_dist(lf, pars)

Fit the model to data

fitted <- fit_abc(lf, 100, target_acc = 0.3)
knitr::kable(summary(fitted)$Post, digits = 2)


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