Skip to contents

bayesm.HART implements the MCMC sampling routine for the Hierarchical Additive Regression Trees (HART) logit model proposed in Wiemann (2025).

The HART logit model flexibly leverages potentially many observed consumer characteristics and thus generalizes existing hierarchical models that exclusively model the representative consumer as a linear function of a select few characteristics. HART’s combination of a flexible nonparametric prior within the hierarchical model provides a coherent framework for (Bayes-) optimal managerial decisions that adapt to the firm’s familiarity with the consumer: first, HART flexibly leverages observed characteristics for granular predictions about new consumers; second, their individual-level preference estimates adapt optimally as a consumer’s choices accumulate.

See the corresponding working paper Personalization with HART for further discussion and details.

The bayesm.HART package builds on the excellent (and highly popular) bayesm and BART packages. The syntax for estimating the HART logit model mirrors the syntax of the bayesm package. Researchers with existing bayesm code will only need to adapt the Prior argument to get started–see the code snippet below.

Installation

Install the latest development version from GitHub (requires devtools package):

if (!require("devtools")) {
  install.packages("devtools")
}
devtools::install_github("thomaswiemann/bayesm.HART", dependencies = TRUE)

Example

The following example applies the HART logit model to the canonical conjoint dataset of Allenby and Ginter (1995) on credit card design. The first code block loads the data and formats it into the list structure required by rhierMnlRwMixture. It also specifies the MCMC hyperparameters. The syntax for this codeblock is identical for the bayesm and bayesm.HART packages.

# Load bayesm.HART for the sampler; load bayesm for the bank data
library(bayesm.HART)
library(bayesm)

# Load and prepare data from the 'bank' dataset (same as bayesm)
data(bank)
choiceAtt <- bank$choiceAtt
hh <- levels(factor(choiceAtt$id))
nhh <- length(hh)
lgtdata <- vector("list", length = nhh)
for (i in 1:nhh) {
    y = 2 - choiceAtt[choiceAtt[,1]==hh[i], 2]
    nobs = length(y)
    X_temp = as.matrix(choiceAtt[choiceAtt[,1]==hh[i], c(3:16)])
    X = matrix(0, nrow = nrow(X_temp) * 2, ncol = ncol(X_temp))
    X[seq(1, nrow(X), by = 2), ] = X_temp
    lgtdata[[i]] = list(y=y, X=X)
}
Z <- as.matrix(bank$demo[, -1]) # omit id
Z <- t(t(Z) - colMeans(Z)) # de-mean covariates as required by bayesm

# Final data object (same as bayesm)
Data <- list(lgtdata = lgtdata, Z = Z, p = 2)

# MCMC hyperparameters (same as bayesm)
Mcmc <- list(R = 2000, keep = 2, nprint = 500)

To fit the HART logit model using a Metropolis-within-Gibbs sampler, call the rhierMnlRwMixture routine. The key difference between conventional calls to linear hierarchical specifications based on bayesm is the use of the additional Prior argument bart. For simplicity, the code below specifies a HART model with only 50 trees per factor, leaving all other hyperparameters at their defaults. See also ?rhierMnlRwMixture for details.

# Fit the HART logit model
out <- bayesm.HART::rhierMnlRwMixture(
    Data = Data, Mcmc = Mcmc, 
    Prior = list(
      ncomp = 1, 
      bart = list(num_trees = 50) # new HART prior parameters
      ),
    r_verbose = F # suppress R print output (optional)
)
#>  MCMC Iteration (est time to end - min) 
#>  500 (0.9)
#>  1000 (0.6)
#>  1500 (0.3)
#>  2000 (0.0)
#>  Total Time Elapsed: 1.32

With the MCMC draws from the fitted model, we can characterize the posterior estimates for any quantity of interest. A key object is the representative respondent, which represents the expected part-worths for a respondent conditional on their characteristics. The following code computes the posterior mean and standard deviation of these expected part-worths for three credit card attributes.

DeltaZ_hat <- predict(out, newdata = Data, type = "DeltaZ+mu", 
                      burn = 250, r_verbose=F)

posterior_mean <- apply(DeltaZ_hat, 2, mean)
posterior_sd <- apply(DeltaZ_hat, 2, sd)

# Indices for the desired coefficients:
# 2: Interest Low Fixed
# 8: Annual Fee Low
# 10: Bank Out-of-State
selected_indices <- c(2, 8, 10)

# Create a matrix with means and sds as rows
results_matrix <- rbind(
  `Posterior Mean` = posterior_mean[selected_indices],
  `Posterior SD`   = posterior_sd[selected_indices]
)

# Convert to a data frame and set column names
results_df <- as.data.frame(results_matrix)
colnames(results_df) <- c("Interest Low Fixed", "Annual Fee Low", "Bank Out-of-State")

# Print the data frame to the console
print(results_df, digits = 3)
#>                Interest Low Fixed Annual Fee Low Bank Out-of-State
#> Posterior Mean               5.08           4.33            -3.411
#> Posterior SD                 0.96           0.99             0.993

Learn More about bayesm.HART

Curious and want to learn more? Have a look at the bayesm.HART vignettes:

Acknowledgements

bayesm.HART originated as a fork of the bayesm and BART packages. Its current implementation heavily leverages the codebase and foundational work from both packages. I gratefully acknowledge the contributions of their respective authors:

  • bayesm: Peter Rossi
  • BART: Robert McCulloch, Rodney Sparapani, Robert Gramacy, Matthew Pratola, Charles Spanbauer, Martyn Plummer, Nicky Best, Kate Cowles, Karen Vines

References

Allenby, Greg M. and James L. Ginter (1995). “Using Extremes to Design Products and Segment Markets.” Journal of Marketing Research 32.4, pp. 392–403.

Chipman, Hugh A., Edward I. George, and Robert E. McCulloch (2010). “BART: Bayesian Additive Regression Trees.” Annals of Applied Statistics 4.1.

Rossi, Peter E., Greg M. Allenby, and Robert McCulloch (2009). Bayesian Statistics and Marketing. Reprint. Wiley Series in Probability and Statistics. Chichester: Wiley.

Rossi, Peter (2023). bayesm: Bayesian Inference for Marketing/Micro-Econometrics. Comprehensive R Archive Network.

Sparapani, Rodney, Charles Spanbauer, and Robert McCulloch (2021). “Nonparametric Machine Learning and Efficient Computation with Bayesian Additive Regression Trees: The BART R Package.” Journal of Statistical Software 97, pp. 1–66.

Wiemann, Thomas (2025). “Personalization with HART.” Working paper.