binpost {revdbayes} | R Documentation |
Random sampling from a binomial posterior distribution
Description
Samples from the posterior distribution of the probability p
of a binomial distribution.
Usage
binpost(n, prior, ds_bin, param = c("logit", "p"))
Arguments
n |
A numeric scalar. The size of posterior sample required. |
prior |
A function to evaluate the prior, created by
|
ds_bin |
A numeric list. Sufficient statistics for inference
about a binomial probability
|
param |
A character scalar. Only relevant if If If The latter tends to make the optimizations involved in the ratio-of-uniforms algorithm more stable and to increase the probability of acceptance, but at the expense of slower function evaluations. |
Details
If prior$prior == "bin_beta"
then the posterior for p
is a beta distribution so rbeta
is used to
sample from the posterior.
If prior$prior == "bin_mdi"
then
rejection sampling is used to sample from the posterior with an envelope
function equal to the density of a
beta(ds$m
+ 1, ds$n_raw - ds$m
+ 1) density.
If prior$prior == "bin_northrop"
then
rejection sampling is used to sample from the posterior with an envelope
function equal to the posterior density that results from using a
Haldane prior.
If prior$prior
is a (user-supplied) R function then
ru
is used to sample from the posterior using the
generalised ratio-of-uniforms method.
Value
An object (list) of class "binpost"
with components
bin_sim_vals: |
An |
bin_logf: |
A function returning the log-posterior for
|
bin_logf_args: |
A list of arguments to |
If prior$prior
is a (user-supplied) R function then this list
also contains ru_object
the object of class "ru"
returned by ru
.
See Also
set_bin_prior
for setting a prior distribution
for the binomial probability p
.
Examples
u <- quantile(gom, probs = 0.65)
ds_bin <- list()
ds_bin$n_raw <- length(gom)
ds_bin$m <- sum(gom > u)
bp <- set_bin_prior(prior = "jeffreys")
temp <- binpost(n = 1000, prior = bp, ds_bin = ds_bin)
graphics::hist(temp$bin_sim_vals, prob = TRUE)
# Setting a beta prior (Jeffreys in this case) by hand
beta_prior_fn <- function(p, ab) {
return(stats::dbeta(p, shape1 = ab[1], shape2 = ab[2], log = TRUE))
}
jeffreys <- set_bin_prior(beta_prior_fn, ab = c(1 / 2, 1 / 2))
temp <- binpost(n = 1000, prior = jeffreys, ds_bin = ds_bin)