Description Usage Arguments Value See Also Examples
Add columns to a data frame representing empirical Bayes shrinkage
towards an estimated beta prior. This calls the ebb_fit_prior
function to fit the prior, then adds the columns using
augment.ebb_prior
. It is thus a useful wrapper when you're
not interested in the prior itself, but only in performing shrinkage
on data.
1 2 3 | add_ebb_estimate(tbl, x, n, cred_level = 0.95, prior_subset = TRUE, ...)
add_ebb_estimate_(tbl, x, n, prior_subset = TRUE, cred_level = 0.05, ...)
|
tbl |
A table. |
x |
Column containing number of successes. |
n |
Column containing totals. |
cred_level |
Level of credible interval to compute. If NULL, do not compute intervals. |
prior_subset |
An expression evaluating to a logical vector indicating which values should be used for computing the prior. |
... |
Extra arguments passed on to |
The original table, with several columns added based on empirical Bayes shrinkage:
.alpha1 |
Posterior alpha (shape1) parameter |
.beta1 |
Posterior beta (shape2) parameter |
.fitted |
Posterior shrunken estimate |
.raw |
Estimate without shrinkage (success / total) |
.low |
Lower bound of credible interval |
.high |
Upper bound of credible interval |
ebb_prior_tidiers
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | library(dplyr)
set.seed(2017)
# simulate 200 random examples from a beta-binomial
obs <- 200
dat <- data_frame(prob = rbeta(obs, 10, 50),
total = round(rlnorm(obs, 4, 2)) + 1,
x = rbinom(obs, total, prob))
result <- add_ebb_estimate(dat, x, total)
result
# visualize the shrinkage towards the prior mean
library(ggplot2)
ggplot(result, aes(.raw, .fitted, color = log10(total))) +
geom_point() +
geom_abline(color = "red")
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.