Hyperparameter Estimation with openEBGM

2017-11-15

Background

The basis of DuMouchel’s method lies in the fact that it is an “Empirical Bayes” (EB) method. That is, the data are a driving force behind the choice of the prior distribution (in contrast to typical Bayesian methods, where the prior is chosen without regard to the actual data). One outcome of this is a known posterior distribution that relies on a set of hyperparameters derived from the prior distribution. These hyperparameters are estimated by their most likely value in the context of Empirical Bayesian methods. One process by which this can occur involves maximizing the likelihood function of the marginal distributions of the counts (or in our case, minimizing the negative log-likelihood function).

Optimization

Global optimization is a broad field. There are many existing R packages with minimization routines which may be used in the estimation of these hyperparameters. The hyperparameter estimation functions offered by openEBGM utilize the following:

openEBGM’s functions actually use local optimization algorithms implemented in R’s stats package, except with multiple starting points. The user is encouraged to explore a variety of optimization approaches because the accuracy of a global optimization result is extremely difficult (if not impossible) to verify and other approaches might work just as well (or better).

Additional notes

In his 2001 paper, DuMouchel mentions a methodology he calls “data squashing”, which “compacts” the dataset to reduce the amount of computation needed to estimate the hyperparameters. openEBGM provides an implementation for data squashing.

References

Please see the Introduction to openEBGM vignette.


Estimating the Hyperparameters

Data Squashing

The actual counts (\(N\)) and expected counts (\(E\)) are used to estimate the hyperparameters of the prior distribution. A large contingency table will have many cells, resulting in computational difficulties for the optimization routines needed for estimation. Data squashing (DuMouchel et al., 2001) transforms a set of 2-dimensional points to a smaller number of 3-dimensional points. The idea is to reduce a large number of points \((N, E)\) to a smaller number of points \((N_k, E_k, W_k)\), where \(k = 1,...,M\) and \(W_k\) is the weight of the \(k^{th}\) “superpoint”. To minimize information loss, only points close to each other should be squashed.

For a given \(N\), squashData() combines points with similar \(E\)s into bins using a specified bin size and uses the average \(E\) within each bin as the \(E\) for that bin’s “superpoint”. The new superpoints are weighted by bin size. For example, the points (1, 1.1) and (1, 1.3) could be squashed to the superpoint (1, 1.2, 2).

An example is given below using unstratified expected counts:

library(openEBGM)
data(caers)

processed <- processRaw(data = caers)
processed[1:4, 1:4]
#>                      var1                  var2 N            E
#> 1         1-PHENYLALANINE  HEART RATE INCREASED 1 0.0360548272
#> 2 11 UNSPECIFIED VITAMINS                ASTHMA 1 0.0038736591
#> 3 11 UNSPECIFIED VITAMINS CARDIAC FUNCTION TEST 1 0.0002979738
#> 4 11 UNSPECIFIED VITAMINS            CHEST PAIN 1 0.0360548272

squashed <- squashData(data = processed) #Using defaults
head(squashed)
#>   N            E weight
#> 1 1 0.0002979738     50
#> 2 1 0.0002979738     50
#> 3 1 0.0002979738     50
#> 4 1 0.0002979738     50
#> 5 1 0.0002979738     50
#> 6 1 0.0002979738     50

nrow(processed)
#> [1] 17189
nrow(squashed)
#> [1] 1568

As shown above, the squashed data set has 9.12% of the observations as the full dataset. Using this squashed data, we can then estimate the hyperparameters in a far more efficient manner.

Likelihood Functions

As previously mentioned, the hyperparameters are estimated by minimizing the negative log-likelihood function. There are actually 4 different functions, depending on the use of data squashing and zero counts. All 4 functions, however, are based on the marginal distribution of the counts, which are mixtures of two negative binomial distributions. The hyperparameters are denoted by the vector \(\theta=(\alpha_1,\beta_1,\alpha_2,\beta_2,P)\), where \(P\) is the mixture fraction.

The most commonly used likelihood function is negLLsquash(), which is used when squashing data and not using zero counts. negLLsquash() is not called directly, but rather by some optimization function:

theta_init <- c(alpha1 = 0.2, beta1 = 0.1, alpha2 = 2, beta2 = 4, p = 1/3)
stats::nlm(negLLsquash, p = theta_init,
           ni = squashed$N, ei = squashed$E, wi = squashed$weight, N_star = 1)
#> $minimum
#> [1] 4162.496
#> 
#> $estimate
#> [1] 3.25164477 0.39974616 2.02567548 1.90789320 0.06537599
#> 
#> $gradient
#> [1]  3.915842e-05 -3.374225e-04 -2.200019e-04  3.270169e-04 -1.196895e-03
#> 
#> $code
#> [1] 1
#> 
#> $iterations
#> [1] 71

The N_star argument allows the user to choose the smallest value of \(N\) used for hyperparameter estimation. The user must be careful to match N_star with the actual minimum count in ni. If the user wishes to use a larger value for N_star, the vectors supplied to arguments ni, ei, and wi must be filtered. Here, we are using all counts except zeroes, so no filtering is needed. In general, N_star = 1 should be used whenever practical.

Note that you will likely receive warning messages from the optimization function–use your own judgment to determine whether or not they would likely indicate real problems.

The other likelihood functions are negLL(), negLLzero(), and negLLzeroSquash(). Make sure to use the appropriate function for your choice of data squashing and use of zero counts.

Hyperparameter Estimation Functions

Hyperparameters can be calculated by exploring the parameter space of the likelihood function using either the full data set of \(N\)s and \(E\)s or the squashed set. The methodology implemented by this package essentially maximizes the likelihood function (or more specifically, minimizes the negative log-likelihood function). Starting points must be chosen to begin the exploration. DuMouchel (1999, 2001) provides a “lightly justified” set of initial hyperparameters. However, openEBGM’s functions support a large set of starting choices to help reach convergence and reduce the chance of false convergence. We begin by defining some starting points:

theta_init <- data.frame(alpha1 = c(0.2, 0.1, 0.3),
                         beta1  = c(0.1, 0.2, 0.5),
                         alpha2 = c(2,   10,  6),
                         beta2  = c(4,   10,  6),
                         p      = c(1/3, 0.2, 0.5)
                         )

autoHyper()

Now that the initial guesses for the hyperparameters have been defined, the function autoHyper() can be used to determine the actual hyperparameter estimates. autoHyper() performs a “verification check” by requiring the optimization routine to converge at least twice within the bounds of the parameter space. The estimate corresponding to the smallest negative log-likelihood is chosen as a tentative result. By default, this estimate must be similar to at least one other convergent solution. If the algorithm fails to consistently converge, an error message is returned.

system.time(
hyper_estimates_full <- autoHyper(data = processed, theta_init = theta_init, 
                                  squashed = FALSE)
)
#>    user  system elapsed 
#>   41.96    0.00   42.01

squashed2 <- squashData(squashed, count = 2, bin_size = 10, keep_bins = 5)
system.time(
hyper_estimates_squashed <- autoHyper(data = squashed2, theta_init = theta_init)
)
#>    user  system elapsed 
#>    2.45    0.00    2.45

hyper_estimates_full
#> $method
#> [1] "nlminb"
#> 
#> $estimates
#>     alpha1      beta1     alpha2      beta2          P 
#> 3.25596617 0.39998845 2.02374464 1.90612821 0.06530723 
#> 
#> $num_close
#> [1] 2
#> 
#> $theta_hats
#>   guess_num   a1_hat    b1_hat   a2_hat   b2_hat      p_hat code converge
#> 1         1 3.256048 0.3999935 2.023745 1.906121 0.06530615    0     TRUE
#> 2         2 3.256318 0.4000082 2.023726 1.906092 0.06530197    0     TRUE
#> 3         3 3.255966 0.3999884 2.023745 1.906128 0.06530723    0     TRUE
#>   in_bounds  minimum
#> 1      TRUE 4162.456
#> 2      TRUE 4162.456
#> 3      TRUE 4162.456

hyper_estimates_squashed
#> $method
#> [1] "nlminb"
#> 
#> $estimates
#>     alpha1      beta1     alpha2      beta2          P 
#> 3.25374326 0.39988617 2.02613782 1.90809094 0.06534647 
#> 
#> $num_close
#> [1] 2
#> 
#> $theta_hats
#>   guess_num   a1_hat    b1_hat   a2_hat   b2_hat      p_hat code converge
#> 1         1 3.251937 0.3997614 2.026192 1.908226 0.06536607    0     TRUE
#> 2         2 3.253743 0.3998862 2.026138 1.908091 0.06534647    0     TRUE
#> 3         3 3.253796 0.3998902 2.026169 1.908116 0.06534669    0     TRUE
#>   in_bounds minimum
#> 1      TRUE 4161.93
#> 2      TRUE 4161.93
#> 3      TRUE 4161.93

As seen above, the process is much faster when utilizing the squashed data, with estimates that are nearly identical. Of course, the amount of efficiency increase depends on the parameter values used in the call to squashData() and the size of the original data set. Another factor affecting run time is the number of starting points used. In general, you should use five or fewer starting points and limit the number of data points to a maximum of about 20,000 if possible. Notice that we squashed the same data again, which is allowed if we use a different value for count each time.

autoHyper() utilizes multiple minimization techniques to verify convergence. It first attempts the stats::nlminb() function, which implements a quasi-Newton unconstrained optimization technique using PORT routines. If nlminb() fails to consistently converge, autoHyper() next attempts stats::nlm(), which is a non-linear maximization algorithm based on the Newton method. Finally, if the first two approaches fail, a quasi-Newton method (also known as a variable metric algorithm) is used, which “…uses function values and gradients to build up a picture of the surface to be optimized.” (source: R documentation for stats::optim) This routine is implemented by the stats::optim() function with the method = BFGS argument.

autoHyper() can now return standard errors and confidence intervals for the hyperparameter estimates:

autoHyper(squashed2, theta_init = theta_init, conf_ints = TRUE)$conf_int
#>        pt_est     SE   LL_95  UL_95
#> a1_hat 3.2537 2.2875 -1.2296 7.7371
#> b1_hat 0.3999 0.1439  0.1179 0.6819
#> a2_hat 2.0261 0.4514  1.1414 2.9108
#> b2_hat 1.9081 0.4328  1.0599 2.7563
#> p_hat  0.0653 0.0358 -0.0048 0.1355

exploreHypers()

autoHyper() is a semi-automated (but imperfect) approach to hyperparameter estimation. The user is encouraged to explore various optimization approaches as no single approach will always work (the functions in openEBGM are not the only optimization functions available in R). One way to explore is with openEBGM’s exploreHypers() function, which is actually called by autoHyper() behind-the-scenes. exploreHypers() can now also return standard errors for the hyperparameter estimates:

exploreHypers(data = squashed2, theta_init = theta_init, std_errors = TRUE)
#> $estimates
#>   guess_num   a1_hat    b1_hat   a2_hat   b2_hat      p_hat code converge
#> 1         1 3.251937 0.3997614 2.026192 1.908226 0.06536607    0     TRUE
#> 2         2 3.253743 0.3998862 2.026138 1.908091 0.06534647    0     TRUE
#> 3         3 3.253796 0.3998902 2.026169 1.908116 0.06534669    0     TRUE
#>   in_bounds minimum
#> 1      TRUE 4161.93
#> 2      TRUE 4161.93
#> 3      TRUE 4161.93
#> 
#> $std_errs
#>   guess_num    a1_se     b1_se     a2_se     b2_se       p_se
#> 1         1 2.280903 0.1435339 0.4512148 0.4323755 0.03572939
#> 2         2 2.287478 0.1438959 0.4513849 0.4327522 0.03578442
#> 3         3 2.288223 0.1439303 0.4514478 0.4328547 0.03579575

exploreHypers() offers three gradient-based optimization methods and is basically just a wrapper around commonly used functions from the stats package mentioned earlier: nlminb() (the default), nlm(), & optim().

Although the user is encouraged to explore various approaches for maximum likelihood hyperparameter estimation, autoHyper() will often give reasonable results with minimal effort. Once the hyperparameters have been estimated, they can be used in the calculation of the \(EBGM\) and quantile scores by applying them to the posterior distribution. This process can be found in the Empirical Bayes Metrics with openEBGM vignette.