posterior_gamma_poisson: posterior_gamma_poisson

Description Usage Arguments Value Examples

View source: R/posterior_gamma_poisson.R

Description

posterior_gamma_poisson

Usage

1
2
3
4
5
6
7
posterior_gamma_poisson(
  df,
  ab_prior = NULL,
  dist = c("post", "pred"),
  Nsamp = 1000,
  ...
)

Arguments

df

data.frame object, containing at least columns named 'x' containing non-negative integer values and 'g' containing group labels.

ab_prior

length-2, positive numeric vector specifying prior hyperparameter values for a, b; if NULL, values fit by empirical Bayes (EMPB).

dist

string specifying what type of samples to return: either 'post' (for samples from the posterior distribution), or 'pred' (for samples from the posterior-predictive distribution).

Nsamp

positive integer, number of samples to generate per group.

...

optional parameters to be passed to control EMPB convergence, in the case 'ab_prior' is NULL; see 'empb_gamma_poisson' or 'empb_gamma_poisson_c'.

Value

matrix of samples from the posterior (or posterior-predictive) distribution, where (named) columns are for group IDs included in df$g, and rows are samples; assuming df$x ~ poisson(L_g), and L_g ~ gamma(a, b), where 'L_g' denotes a group-level parameter.

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
# Generate example data:
set.seed(31)
a = 23
b = 9

# Number of groups:
NG = 10

# Creating group IDs:
g = replicate(NG, paste(sample(LETTERS, 10), sep="", collapse=""))

# Generating 'true' L parameters:
L = rgamma(length(g), a, b)

# Number of experiments, i.e. rows in df:
numexps = 100

# Filling df with pseudo data; note the requisite columns 'x' and 'g':
df = data.frame('x' = numeric(0), 'g' = character(0))
for(k in 1:numexps){
  gk = sample(g, 1)
  xk = rpois(1, L[g == gk])
  df = rbind(df, data.frame('x' = xk, 'g' = gk))
}

# Generating 1000 posterior distribution samples for each group:
posterior_values = posterior_gamma_poisson(df = df)
dim(posterior_values)

# Create histogram of posterior distribution samples for first group (by alphabetic order):
hist(posterior_values[, 1], main = colnames(posterior_values)[1])

alexandercoulter/DHBayes documentation built on Dec. 19, 2021, 12:29 a.m.