empb_beta_binomial: empb_beta_binomial

Description Usage Arguments Value Examples

View source: R/empb_beta_binomial.R

Description

empb_beta_binomial

Usage

1
2
3
4
5
6
7
empb_beta_binomial(
  df,
  eta = 0.1,
  tol = 1e-05,
  maxIter = 10000,
  method = c("newton", "gdescent")
)

Arguments

df

data.frame object, containing at least columns named 'x' containing non-negative integer values (number of successes), 'n' containing non-negative integer values (number of trials), and 'g' containing group labels.

eta

positive numeric dampening parameter for Newton's method, gradient descent algorithm.

tol

non-negative numeric tolerance parameter for exiting optimization algorithm.

maxIter

positive integer setting maximum number of iterations for optimization algorithm.

method

string controlling optimization method; default 'newton'.

Value

list object containing empirical Bayes (EMPB) estimates of a, b hyperparameters, assuming df$x ~ binom(p_g, df$n), and p_g ~ beta(a, b), where 'p_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 = 3
b = 9

# Number of groups:
NG = 10

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

# Generating 'true' p parameters:
p = rbeta(length(g), a, b)

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

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

# Generating empirical Bayes (EMPB) solutions for a, b:
ab_fit = empb_beta_binomial(df = df)

# Compare fitted values to known values:
cbind(c(a, b), c(ab_fit$a, ab_fit$b))

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