ebb_fit_prior: Estimate the shape parameters of a beta by fitting a...

Description Usage Arguments Value Examples

Description

Estimate the shape parameters of a beta that underlies many pairs of success and total. This is useful for determining the hyperparameters of a prior, for empirical Bayes estimation. This offers three methods for estimating them: the method of moments, based only on the mean and variance of the proportions, maximum likelihood estimation, based on a beta-binomial model, and beta-binomial regression, which allows the mean and variance of the beta to vary based on other parameters in the table.

Usage

1
2
3
ebb_fit_prior(tbl, x, n, method = c("mle", "mm", "gamlss"),
  mu_predictors = ~1, sigma_predictors = ~1, mu_link = "logit",
  sigma_link = "log", start = NULL, ...)

Arguments

tbl

A table.

x

An expression for the number of successes, evaluated within the table.

n

An expression for the total number of trials, evaluated within the table.

method

The method for fitting the beta-binomial model. Either "mle" (maximum likelihood), "mm" (method of moments), or "gamlss" (for beta-binomial regression). Only "gamlss" allows the mu and sigma parameters of the beta to depend on other parameters.

mu_predictors

A formula that the mean of the beta may depend on. Ignored if method is not "gamlss".

sigma_predictors

A formula that the variance of the beta may depend on. Ignored if method is not "gamlss".

mu_link

Link function for mu. Ignored if method is not "gamlss".

sigma_link

Link function for sigma. Ignored if method is not "gamlss".

start

Starting guesses of parameters for "mle" method. If NULL, these are estimated by the method of moments.

...

Extra arguments passed on to mle (if method is "mle") or to gamlss (if method is "gamlss"). Ignored if method is "mm".

Value

An object of class "ebb_prior", which contains the estimated parameters of a beta-binomial distribution:

parameters

Either a one-row tbl_df with alpha and beta parameters, or (if method is "gamlss"), the coefficients for computing mu and sigma

.

fit

The result of the estimation process. An "mle" object if method is "mle", a "gamlss" object if method is "gamlss", and NULL if the method is "mm".

terms

A list of the x, n, mu_predictors, and sigma_predictors terms.

method

The method used to fit the model.

model

The original data, with just the columns needed to fit the model.

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
32
33
34
35
36
37
38
39
40
41
# example with baseball data
library(Lahman)
library(dplyr)

career <- Batting %>%
  anti_join(Pitching, by = "playerID") %>%
  tbl_df() %>%
  filter(yearID > 1980) %>%
  group_by(playerID) %>%
  summarize(H = sum(H), AB = sum(AB)) %>%
  filter(AB > 0)

# maximum likelihood estimation
fit <- career %>% ebb_fit_prior(H, AB)

fit
tidy(fit)
augment(fit)
glance(fit)

# method of moments
career %>% ebb_fit_prior(H, AB, method = "mm")

# mm works better with pre-filtering:
career %>%
  filter(AB > 100) %>%
  ebb_fit_prior(H, AB, method = "mm")

# allow mu to vary with AB using beta-binomial regression
# see: http://varianceexplained.org/r/bayesian_ab_baseball/
eb <- career %>%
  filter(AB > 100) %>%
  ebb_fit_prior(H, AB, mu_predictors = ~ log10(AB))

tidy(eb)
augment(eb)

library(ggplot2)
ggplot(augment(eb), aes(AB, .fitted)) +
  geom_point() +
  scale_x_log10()

dgrtwo/ebbinom documentation built on May 15, 2019, 7:23 a.m.