BernGrid: Bayesian inference for a proportion via grid method

Description Usage Arguments Examples

View source: R/BernGrid.R

Description

This intended as a black-box example with a small data set to illustrate the results of Bayesian updating. The model is a Bernoulli model with fixed proportion over all trials. Prior, data, and grid resolution can be supplied by the user. See examples.

Usage

 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
BernGrid(
  data = "HHTT",
  p = seq(0, 1, length.out = resolution + 1),
  prior = 1,
  results = c("plot", "data"),
  steps = FALSE,
  geom = if (length(p) <= 20) geom_col else geom_area,
  alpha = 0.5,
  resolution = 200,
  filter = TRUE,
  ...
)

bern_grid(
  data = "HHTT",
  p = seq(0, 1, length.out = resolution + 1),
  prior = 1,
  results = c("plot", "data"),
  steps = FALSE,
  geom = if (length(p) <= 20) geom_col else geom_area,
  alpha = 0.5,
  resolution = 200,
  filter = TRUE,
  ...
)

Arguments

data

a vector of 0's and 1's or a character string with two characters representing success/failure or head/tails from the bernoulli experiment.

p

a vector of probabilites to update.

prior

a vector of prior values at each probability in p, a constant, a function, or a one-sided formula. If a function or formula, purrr::map_dbl() is used to apply to each value of p. (See help for purrr::map_dbl() for details regarding how this works.)

results

either "plot" or "data" to indicate what information is returned.

steps

a logical indicating wether the process should be shown step-by-step or for all the data at once.

geom

a geom used for plotting. Good choices include ggplot2::geom_line(), ggplot2::geom_col(), ggplot2::geom_point(), ggplot2::geom_area()

alpha

opacity used for the plot

resolution

if p is not specified, resolution an be used to create an equally spaced grid of resolution + 1 values from 0 to 1.

filter

an expression used to filter the results. See examples.

...

additional arguments passed to the geom.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
BernGrid("HHTTH", p = seq(0, 1, by = 0.25), results = "data")
BernGrid("HHTTH",
            prior = ~ dbeta(.x, shape1 = 2, shape2 = 5))
BernGrid("HHTTH", geom = geom_col, resolution = 100,
            prior = ~ dbeta(.x, shape1 = 2, shape2 = 5))
BernGrid("HHTTH", geom = geom_area, resolution = 100,
            prior = ~ dbeta(.x, shape1 = 2, shape2 = 5))
BernGrid("HHTTH",
  p = seq(0, 1, by = 0.25), prior = c(.1, .2 , .4, .2, .1))
BernGrid("SSFFS", p = seq(0, 1, by = 0.25), steps = TRUE, results = "data")
BernGrid("SSFFS", p = seq(0, 1, by = 0.25), alpha = 0.9, steps = TRUE)
BernGrid("SSFFS", p = seq(0, 1, by = 0.25), alpha = 0.5, steps = TRUE,
  filter = n %in% c(4,5))
BernGrid("HHTTHTHHTHHH", alpha = 0.9, steps = TRUE)
BernGrid("HHTTHTHHTHHH", steps = TRUE, geom = geom_area)
BernGrid("HHTTHTHHTHHH", alpha = 0.9, steps = TRUE, filter = n %in% c(1, 5, 10))
BernGrid("HHTTHTHHTHHH", alpha = 0.9, steps = TRUE, filter = n == 10)
# same posterior as in the previous example -- order doesn't matter
BernGrid("TTTTHHHHHHHH", alpha = 0.9, steps = TRUE, filter = n == 10)
BernGrid("HHHHHHHHHHHH", alpha = 0.9)
BernGrid("HHHHHHHHHHHH", alpha = 0.9, steps = TRUE)
BernGrid(rbinom(12, 1, 0.25), alpha = 0.9, steps = TRUE)

rpruim/CalvinBayes documentation built on April 12, 2021, 1:49 p.m.