Bayesian Stacking and Pseudo-BMA weights using the loo package (original) (raw)

2024-07-03

Introduction

This vignette demonstrates the new functionality inloo v2.0.0 for Bayesian stacking and Pseudo-BMA weighting. In this vignette we can’t provide all of the necessary background on this topic, so we encourage readers to refer to the paper

which provides important details on the methods demonstrated in this vignette. Here we just quote from the abstract of the paper:

Abstract: Bayesian model averaging is flawed in the\(\mathcal{M}\)-open setting in which the true data-generating process is not one of the candidate models being fit. We take the idea of stacking from the point estimation literature and generalize to the combination of predictive distributions. We extend the utility function to any proper scoring rule and use Pareto smoothed importance sampling to efficiently compute the required leave-one-out posterior distributions. We compare stacking of predictive distributions to several alternatives: stacking of means, Bayesian model averaging (BMA), Pseudo-BMA, and a variant of Pseudo-BMA that is stabilized using the Bayesian bootstrap. Based on simulations and real-data applications, we recommend stacking of predictive distributions, with bootstrapped-Pseudo-BMA as an approximate alternative when computation cost is an issue.

Ideally, we would avoid the Bayesian model combination problem by extending the model to include the separate models as special cases, and preferably as a continuous expansion of the model space. For example, instead of model averaging over different covariate combinations, all potentially relevant covariates should be included in a predictive model (for causal analysis more care is needed) and a prior assumption that only some of the covariates are relevant can be presented with regularized horseshoe prior (Piironen and Vehtari, 2017a). For variable selection we recommend projective predictive variable selection (Piironen and Vehtari, 2017a; projpredpackage).

To demonstrate how to use loo package to compute Bayesian stacking and Pseudo-BMA weights, we repeat two simple model averaging examples from Chapters 6 and 10 of Statistical Rethinking by Richard McElreath. In _Statistical Rethinking_WAIC is used to form weights which are similar to classical “Akaike weights”. Pseudo-BMA weighting using PSIS-LOO for computation is close to these WAIC weights, but named after the Pseudo Bayes Factor by Geisser and Eddy (1979). As discussed below, in general we prefer using stacking rather than WAIC weights or the similar pseudo-BMA weights.

Setup

In addition to the loo package we will also load therstanarm package for fitting the models.

library(rstanarm)
library(loo)

Example: Primate milk

In Statistical Rethinking, McElreath describes the data for the primate milk example as follows:

A popular hypothesis has it that primates with larger brains produce more energetic milk, so that brains can grow quickly. … The question here is to what extent energy content of milk, measured here by kilocalories, is related to the percent of the brain mass that is neocortex. … We’ll end up needing female body mass as well, to see the masking that hides the relationships among the variables.

data(milk)
d <- milk[complete.cases(milk),]
d$neocortex <- d$neocortex.perc /100
str(d)
'data.frame':   17 obs. of  9 variables:
 $ clade         : Factor w/ 4 levels "Ape","New World Monkey",..: 4 2 2 2 2 2 2 2 3 3 ...
 $ species       : Factor w/ 29 levels "A palliata","Alouatta seniculus",..: 11 2 1 6 27 5 3 4 21 19 ...
 $ kcal.per.g    : num  0.49 0.47 0.56 0.89 0.92 0.8 0.46 0.71 0.68 0.97 ...
 $ perc.fat      : num  16.6 21.2 29.7 53.4 50.6 ...
 $ perc.protein  : num  15.4 23.6 23.5 15.8 22.3 ...
 $ perc.lactose  : num  68 55.2 46.9 30.8 27.1 ...
 $ mass          : num  1.95 5.25 5.37 2.51 0.68 0.12 0.47 0.32 1.55 3.24 ...
 $ neocortex.perc: num  55.2 64.5 64.5 67.6 68.8 ...
 $ neocortex     : num  0.552 0.645 0.645 0.676 0.688 ...

We repeat the analysis in Chapter 6 of Statistical Rethinking using the following four models (here we use the default weakly informative priors in rstanarm, while flat priors were used in Statistical Rethinking).

fit1 <- stan_glm(kcal.per.g ~ 1, data = d, seed = 2030)
fit2 <- update(fit1, formula = kcal.per.g ~ neocortex)
fit3 <- update(fit1, formula = kcal.per.g ~ log(mass))
fit4 <- update(fit1, formula = kcal.per.g ~ neocortex + log(mass))

McElreath uses WAIC for model comparison and averaging, so we’ll start by also computing WAIC for these models so we can compare the results to the other options presented later in the vignette. Theloo package provides waic methods for log-likelihood arrays, matrices and functions. Since we fit our model with rstanarm we can use the waic method provided by therstanarm package (a wrapper around waicfrom the loo package), which allows us to just pass in our fitted model objects instead of first extracting the log-likelihood values.

waic1 <- waic(fit1)
waic2 <- waic(fit2)
waic3 <- waic(fit3)
Warning: 
1 (5.9%) p_waic estimates greater than 0.4. We recommend trying loo instead.
Warning: 
2 (11.8%) p_waic estimates greater than 0.4. We recommend trying loo instead.
waics <- c(
  waic1$estimates["elpd_waic", 1],
  waic2$estimates["elpd_waic", 1],
  waic3$estimates["elpd_waic", 1],
  waic4$estimates["elpd_waic", 1]
)

We get some warnings when computing WAIC for models 3 and 4, indicating that we shouldn’t trust the WAIC weights we will compute later. Following the recommendation in the warning, we next use theloo methods to compute PSIS-LOO instead. Theloo package provides loo methods for log-likelihood arrays, matrices, and functions, but since we fit our model with rstanarm we can just pass the fitted model objects directly and rstanarm will extract the needed values to pass to the loo package. (Likerstanarm, some other R packages for fitting Stan models, e.g. brms, also provide similar methods for interfacing with the loo package.)

# note: the loo function accepts a 'cores' argument that we recommend specifying
# when working with bigger datasets

loo1 <- loo(fit1)
loo2 <- loo(fit2)
loo3 <- loo(fit3)
loo4 <- loo(fit4)
lpd_point <- cbind(
  loo1$pointwise[,"elpd_loo"], 
  loo2$pointwise[,"elpd_loo"],
  loo3$pointwise[,"elpd_loo"], 
  loo4$pointwise[,"elpd_loo"]
)

With loo we don’t get any warnings for models 3 and 4, but for illustration of good results, we display the diagnostic details for these models anyway.


Computed from 4000 by 17 log-likelihood matrix.

         Estimate  SE
elpd_loo      4.5 2.3
p_loo         2.1 0.5
looic        -9.1 4.6
------
MCSE of elpd_loo is 0.0.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.5, 1.0]).

All Pareto k estimates are good (k < 0.7).
See help('pareto-k-diagnostic') for details.

Computed from 4000 by 17 log-likelihood matrix.

         Estimate  SE
elpd_loo      8.4 2.8
p_loo         3.3 0.9
looic       -16.8 5.5
------
MCSE of elpd_loo is 0.1.
MCSE and ESS estimates assume MCMC draws (r_eff in [0.4, 1.0]).

All Pareto k estimates are good (k < 0.7).
See help('pareto-k-diagnostic') for details.

One benefit of PSIS-LOO over WAIC is better diagnostics. Here for both models 3 and 4 all \(k<0.7\)and the Monte Carlo SE of elpd_loo is 0.1 or less, and we can expect the model comparison to be reliable.

Next we compute and compare 1) WAIC weights, 2) Pseudo-BMA weights without Bayesian bootstrap, 3) Pseudo-BMA+ weights with Bayesian bootstrap, and 4) Bayesian stacking weights.

waic_wts <- exp(waics) / sum(exp(waics))
pbma_wts <- pseudobma_weights(lpd_point, BB=FALSE)
pbma_BB_wts <- pseudobma_weights(lpd_point) # default is BB=TRUE
stacking_wts <- stacking_weights(lpd_point)
round(cbind(waic_wts, pbma_wts, pbma_BB_wts, stacking_wts), 2)
       waic_wts pbma_wts pbma_BB_wts stacking_wts
model1     0.01     0.02        0.07         0.01
model2     0.01     0.01        0.04         0.00
model3     0.02     0.02        0.04         0.00
model4     0.96     0.95        0.85         0.99

With all approaches Model 4 with neocortex andlog(mass) gets most of the weight. Based on theory, Pseudo-BMA weights without Bayesian bootstrap should be close to WAIC weights, and we can also see that here. Pseudo-BMA+ weights with Bayesian bootstrap provide more cautious weights further away from 0 and 1 (see Yao et al. (2018) for a discussion of why this can be beneficial and results from related experiments). In this particular example, the Bayesian stacking weights are not much different from the other weights.

One of the benefits of stacking is that it manages well if there are many similar models. Consider for example that there could be many irrelevant covariates that when included would produce a similar model to one of the existing models. To emulate this situation here we simply copy the first model a bunch of times, but you can imagine that instead we would have ten alternative models with about the same predictive performance. WAIC weights for such a scenario would be close to the following:

waic_wts_demo <- 
  exp(waics[c(1,1,1,1,1,1,1,1,1,1,2,3,4)]) /
  sum(exp(waics[c(1,1,1,1,1,1,1,1,1,1,2,3,4)]))
round(waic_wts_demo, 3)
 [1] 0.013 0.013 0.013 0.013 0.013 0.013 0.013 0.013 0.013 0.013 0.006 0.016
[13] 0.847

Notice how much the weight for model 4 is lowered now that more models similar to model 1 (or in this case identical) have been added. Both WAIC weights and Pseudo-BMA approaches first estimate the predictive performance separately for each model and then compute weights based on estimated relative predictive performances. Similar models share similar weights so the weights of other models must be reduced for the total sum of the weights to remain the same.

On the other hand, stacking optimizes the weights jointly, allowing for the very similar models (in this toy example repeated models) to share their weight while more unique models keep their original weights. In our example we can see this difference clearly:

stacking_weights(lpd_point[,c(1,1,1,1,1,1,1,1,1,1,2,3,4)])
Method: stacking
------
        weight
model1  0.001 
model2  0.001 
model3  0.001 
model4  0.001 
model5  0.001 
model6  0.001 
model7  0.001 
model8  0.001 
model9  0.001 
model10 0.001 
model11 0.000 
model12 0.000 
model13 0.987 

Using stacking, the weight for the best model stays essentially unchanged.

Simpler coding using loo_model_weights function

Although in the examples above we called thestacking_weights and pseudobma_weightsfunctions directly, we can also use the loo_model_weightswrapper, which takes as its input either a list of pointwise log-likelihood matrices or a list of precomputed loo objects. There are also loo_model_weights methods for stanreg objects (fitted model objects from rstanarm) as well as fitted model objects from other packages (e.g. brms) that do the preparation work for the user (see, e.g., the examples athelp("loo_model_weights", package = "rstanarm")).

# using list of loo objects
loo_list <- list(loo10, loo11, loo12)
loo_model_weights(loo_list)
Method: stacking
------
      weight
fit10 0.000 
fit11 0.802 
fit12 0.198 
loo_model_weights(loo_list, method = "pseudobma")
Method: pseudo-BMA+ with Bayesian bootstrap
------
      weight
fit10 0.310 
fit11 0.539 
fit12 0.151 
loo_model_weights(loo_list, method = "pseudobma", BB = FALSE)
Method: pseudo-BMA
------
      weight
fit10 0.356 
fit11 0.628 
fit12 0.015 

References

McElreath, R. (2016). Statistical rethinking: A Bayesian course with examples in R and Stan. Chapman & Hall/CRC. http://xcelab.net/rm/statistical-rethinking/

Piironen, J. and Vehtari, A. (2017a). Sparsity information and regularization in the horseshoe and other shrinkage priors. In Electronic Journal of Statistics, 11(2):5018-5051. Online.

Piironen, J. and Vehtari, A. (2017b). Comparison of Bayesian predictive methods for model selection. Statistics and Computing, 27(3):711-735. :10.1007/s11222-016-9649-y. Online.

Vehtari, A., Gelman, A., and Gabry, J. (2017). Practical Bayesian model evaluation using leave-one-out cross-validation and WAIC.Statistics and Computing. 27(5), 1413–1432. :10.1007/s11222-016-9696-4. online,arXiv preprint arXiv:1507.04544.

Vehtari, A., Simpson, D., Gelman, A., Yao, Y., and Gabry, J. (2024). Pareto smoothed importance sampling. Journal of Machine Learning Research, 25(72):1-58. PDF

Yao, Y., Vehtari, A., Simpson, D., and Gelman, A. (2018). Using stacking to average Bayesian predictive distributions. In Bayesian Analysis, :10.1214/17-BA1091. Online.