Learn R Programming

BART (version 1.6)

mbart: BART for multinomial outcomes with Logistic latents

Description

BART is a Bayesian “sum-of-trees” model. For numeric response \(y\), we have \(y = f(x) + \epsilon\), where \(\epsilon \sim Log(0, 1)\). For a multinomial response \(y\), \(P(Y=y | x) = F(f(x))\), where \(F\) denotes the standard Logistic CDF (logit link).

In both cases, \(f\) is the sum of many tree models. The goal is to have very flexible inference for the uknown function \(f\).

In the spirit of “ensemble models”, each tree is constrained by a prior to be a weak learner so that it contributes a small amount to the overall fit.

Usage

mbart(
   x.train, y.train, x.test=matrix(0.0,0,0),
   sparse=FALSE, a=0.5, b=1, augment=FALSE, rho=NULL,
   xinfo=matrix(0.0,0,0), usequants=FALSE,
   cont=FALSE, rm.const=TRUE, tau.interval=0.95,
   k=2.0, power=2.0, base=.95, 
   binaryOffset=0,
   ntree=50L, numcut=100L,
   ndpost=1000L, nskip=100L,
   keepevery=1L,
   nkeeptrain=ndpost, nkeeptest=ndpost,
   
   nkeeptreedraws=ndpost,
   printevery=100L, transposed=FALSE 
)

Arguments

x.train

Explanatory variables for training (in sample) data. May be a matrix or a data frame, with (as usual) rows corresponding to observations and columns to variables. If a variable is a factor in a data frame, it is replaced with dummies. Note that q dummies are created if q>2 and one dummy is created if q=2, where q is the number of levels of the factor. lbart will generate draws of \(f(x)\) for each \(x\) which is a row of x.train.

y.train

Dependent variable for training (in sample) data. If y is numeric a continous response model is fit (Logistic errors). If y is a factor (or just has values 0 and 1) then a binary response model with a logit link is fit.

x.test

Explanatory variables for test (out of sample) data. Should have same structure as x.train. lbart will generate draws of \(f(x)\) for each \(x\) which is a row of x.test.

sparse

Whether to perform variable selection based on a sparse Dirichlet prior rather than simply uniform; see Linero 2016.

a

Sparse parameter for \(Beta(a, b)\) prior: \(0.5<=a<=1\) where lower values inducing more sparsity.

b

Sparse parameter for \(Beta(a, b)\) prior; typically, \(b=1\).

rho

Sparse parameter: typically \(rho=p\) where \(p\) is the number of covariates under consideration.

augment

Whether data augmentation is to be performed in sparse variable selection.

xinfo

You can provide the cutpoints to BART or let BART choose them for you. To provide them, use the xinfo argument to specify a list (matrix) where the items (rows) are the covariates and the contents of the items (columns) are the cutpoints.

usequants

If usequants=FALSE, then the cutpoints in xinfo are generated uniformly; otherwise, if TRUE, uniform quantiles are used for the cutpoints.

cont

Whether or not to assume all variables are continuous.

rm.const

Whether or not to remove constant variables.

tau.interval

The width of the interval to scale the variance for the terminal leaf values.

k

For numeric y, k is the number of prior standard deviations \(E(Y|x) = f(x)\) is away from +/-.5. The response (y.train) is internally scaled to range from -.5 to .5. For binary y, k is the number of prior standard deviations \(f(x)\) is away from +/-3. In both cases, the bigger k is, the more conservative the fitting will be.

power

Power parameter for tree prior.

base

Base parameter for tree prior.

binaryOffset

Currently fixed at zero, but might be relaxed RSN.

ntree

The number of trees in the sum.

numcut

The number of possible values of c (see usequants). If a single number if given, this is used for all variables. Otherwise a vector with length equal to ncol(x.train) is required, where the \(i^{th}\) element gives the number of c used for the \(i^{th}\) variable in x.train. If usequants is false, numcut equally spaced cutoffs are used covering the range of values in the corresponding column of x.train. If usequants is true, then min(numcut, the number of unique values in the corresponding columns of x.train - 1) c values are used.

ndpost

The number of posterior draws returned.

nskip

Number of MCMC iterations to be treated as burn in.

nkeeptrain

Number of MCMC iterations to be returned for train data.

nkeeptest

Number of MCMC iterations to be returned for test data.

nkeeptreedraws

Number of MCMC iterations to be returned for tree draws.

keepevery

Every keepevery draw is kept to be returned to the user.

printevery

As the MCMC runs, a message is printed every printevery draws.

transposed

When running lbart in parallel, it is more memory-efficient to transpose x.train and x.test, if any, prior to calling mc.lbart.

Value

mbart returns an object of type mbart which is essentially a list.

yhat.train

A matrix with ndpost rows and nrow(x.train) columns. Each row corresponds to a draw \(f^*\) from the posterior of \(f\) and each column corresponds to a row of x.train. The \((i,j)\) value is \(f^*(x)\) for the \(i^{th}\) kept draw of \(f\) and the \(j^{th}\) row of x.train. Burn-in is dropped.

yhat.train.mean

train data fits = mean of yhat.train columns.

varcount

a matrix with ndpost rows and nrow(x.train) columns. Each row is for a draw. For each variable (corresponding to the columns), the total count of the number of times that variable is used in a tree decision rule (over all trees) is given.

%% In addition the list %% has a binaryOffset component giving the value used.

Note that in the multinomial y case yhat.train is f(x). %% + binaryOffset If you want draws of the probability P(Y=y | x) you need to apply the Logistic CDF (plogis) to these values.

Details

BART is an Bayesian MCMC method. At each MCMC interation, we produce a draw from the joint posterior \(f | (x,y)\) in the numeric \(y\) case and just \(f\) in the binary \(y\) case.

Thus, unlike a lot of other modelling methods in R, we do not produce a single model object from which fits and summaries may be extracted. The output consists of values \(f^*(x)\) where * denotes a particular draw. The \(x\) is either a row from the training data (x.train).

References

Chipman, H., George, E., and McCulloch R. (2010) Bayesian Additive Regression Trees. The Annals of Applied Statistics, 4,1, 266-298 <doi: 10.1214/09-AOAS285>.

Chipman, H., George, E., and McCulloch R. (2006) Bayesian Ensemble Learning. Advances in Neural Information Processing Systems 19, Scholkopf, Platt and Hoffman, Eds., MIT Press, Cambridge, MA, 265-272.

Friedman, J.H. (1991) Multivariate adaptive regression splines. The Annals of Statistics, 19, 1--67.

See Also

lbart

Examples

Run this code
# NOT RUN {
##simulate from Friedman's five-dimensional test function
##Friedman JH. Multivariate adaptive regression splines
##(with discussion and a rejoinder by the author).
##Annals of Statistics 1991; 19:1-67.

f = function(x) #only the first 5 matter
    sin(pi*x[ , 1]*x[ , 2]) + 2*(x[ , 3]-.5)^2+x[ , 4]+0.5*x[ , 5]-1.5

sigma = 1.0  # y~Logistic(f(x), sigma)
P = 5        # number of covariates
N = 2500
C = 3        # number of categories
M = 5^P

x <- seq(0, 1, length.out=5)
x.test <- matrix(nrow=M, ncol=P)
dimnames(x.test)[[2]] <- paste0('x', 1:5)
h <- 1
for(x5 in x)
    for(x4 in x)
        for(x3 in x)
            for(x2 in x)
                for(x1 in x) {
                    x.test[h, ] <- c(x1, x2, x3, x4, x5)
                    h <- h+1
                }
Ey.test = f(x.test)

set.seed(12)
x.train=matrix(runif(N*P), N, P)
dimnames(x.train)[[2]] <- paste0('x', 1:5)
Ey.train = f(x.train)
y.train=rlogis(N, Ey.train, sigma)

i <- y.train > -1
j <- y.train > 1

y.train[!i] <- 1
y.train[i] <- 2
y.train[j] <- 3

table(y.train)
##test BART with token run to ensure installation works
## set.seed(99)
## post = mbart(x.train, y.train, x.test,
##              nskip=1, ndpost=1, keepevery=1)

# }
# NOT RUN {
## set.seed(99)
## post = mbart(x.train, y.train, x.test)

post = mc.mbart(x.train, y.train, x.test, mc.cores=8, seed=99)

h <- seq(1, C*N, by=C)

print(cor(post$prob.train.mean[h], plogis(-1, Ey.train, sigma))^2)
print(cor(post$prob.train.mean[h+1], plogis(1, Ey.train, sigma)-plogis(-1, Ey.train, sigma))^2)
print(cor(post$prob.train.mean[h+2], plogis(1, Ey.train, sigma, 0))^2)

plot(plogis(-1, Ey.train, sigma), post$prob.train.mean[h], pch='.',
     xlim=0:1, ylim=0:1, xlab='Known P(y=1)', ylab='Est. P(y=1)')
abline(0, 1)

plot(plogis(1, Ey.train, sigma)-plogis(-1, Ey.train, sigma), post$prob.train.mean[h+1], pch='.',
     xlim=0:1, ylim=0:1, xlab='Known P(y=2)', ylab='Est. P(y=2)')
abline(0, 1)

plot(plogis(1, Ey.train, sigma, 0), post$prob.train.mean[h+2], pch='.',
     xlim=0:1, ylim=0:1, xlab='Known P(y=3)', ylab='Est. P(y=3)')
abline(0, 1)

h <- seq(1, C*M, by=C)

print(cor(post$prob.test.mean[h], plogis(-1, Ey.test, sigma))^2)
print(cor(post$prob.test.mean[h+1], plogis(1, Ey.test, sigma)-plogis(-1, Ey.test, sigma))^2)
print(cor(post$prob.test.mean[h+2], plogis(1, Ey.test, sigma, 0))^2)

plot(plogis(-1, Ey.test, sigma), post$prob.test.mean[h], pch='.',
     xlim=0:1, ylim=0:1, xlab='Known P(y=1)', ylab='Est. P(y=1)')
abline(0, 1)

plot(plogis(1, Ey.test, sigma)-plogis(-1, Ey.test, sigma), post$prob.test.mean[h+1], pch='.',
     xlim=0:1, ylim=0:1, xlab='Known P(y=2)', ylab='Est. P(y=2)')
abline(0, 1)

plot(plogis(1, Ey.test, sigma, 0), post$prob.test.mean[h+2], pch='.',
     xlim=0:1, ylim=0:1, xlab='Known P(y=3)', ylab='Est. P(y=3)')
abline(0, 1)

# }

Run the code above in your browser using DataLab