-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
113 lines (76 loc) · 2.71 KB
/
README.Rmd
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
---
output: github_document
---
<!-- badges: start -->
[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/protoABC)](https://CRAN.R-project.org/package=protoABC)
[![R-CMD-check](https://github.com/AnthonyEbert/protoABC/workflows/R-CMD-check/badge.svg)](https://github.com/AnthonyEbert/protoABC/actions)
[![codecov](https://codecov.io/gh/AnthonyEbert/protoABC/branch/master/graph/badge.svg)](https://codecov.io/gh/AnthonyEbert/protoABC)
<!-- badges: end -->
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
set.seed(1)
```
# protoABC
The goal of protoABC is to provide a way to perform ABC (approximate Bayesian computation) inference as flexibly as possible. That is with arbitrarily complex simulation algorithms and distance functions.
The way we implement this is to consider the distance as the output of the simulation and leave the internal details to the user. Parameters in, distance out.
You need to supply a function which returns a positive number (the distance) with up to two arguments, the first is a vector of your parameters of interest, the second is a list of additional inputs \texttt{inp} to your function.
## Installation
You can install protoABC from github with:
```{r gh-installation, eval = FALSE}
# install.packages("devtools")
devtools::install_github("AnthonyEbert/protoABC")
```
## Example
The simplest example, a normal distribution. The summary statistic is the sample mean.
```{r example}
library(protoABC)
sample <- rnorm(1000, 3, 1.5)
inp <- list(
sample_mean = mean(sample),
sample_sd = sd(sample)
)
prior <- function(n){data.frame(mu = rnorm(n, 5))}
distance <- function(theta, inp){
sim <- rnorm(1000, theta)
output <- abs(mean(sim) - inp$sample_mean)
return(output)
}
abc_post_1 <- abc_start(
prior,
distance,
inp,
method = "rejection",
control = list(epsilon = 0.1)
)
summary(abc_post_1)
```
The simplest example, a normal distribution. The summary statistics are the sample mean and the standard deviation.
```{r}
prior <- function(n){
data.frame(mu = runif(n, 2, 4), sd = rgamma(n, 1, 1))
}
prior_eval <- function(theta){
prior_value <- dunif(theta["mu"], 2, 4) * dgamma(theta["sd"], 1, 1)
return(prior_value)
}
distance <- function(theta, inp){
sim <- rnorm(1000, theta["mu"], theta["sd"])
output <- sqrt( (mean(sim) - inp$sample_mean)^2 + (sd(sim) - inp$sample_sd)^2)
return(output)
}
abc_post_2 <- abc_start(
prior,
distance,
inp,
method = "RABC",
control = list(n = 1000, prior_eval = prior_eval, pacc_final = 0.1),
output_control = list(print_output = FALSE)
)
## Summary
summary(abc_post_2)
```