# gbart: Generalized BART for continuous and binary outcomes In BART: Bayesian Additive Regression Trees

## Description

BART is a Bayesian “sum-of-trees” model.
For a numeric response y, we have y = f(x) + e, where e ~ N(0,sigma^2).

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

 ``` 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 42 43 44 45 46 47``` ```gbart( x.train, y.train, x.test=matrix(0,0,0), type='wbart', ntype=as.integer( factor(type, levels=c('wbart', 'pbart', 'lbart'))), sparse=FALSE, theta=0, omega=1, a=0.5, b=1, augment=FALSE, rho=NULL, xinfo=matrix(0,0,0), usequants=FALSE, rm.const=TRUE, sigest=NA, sigdf=3, sigquant=0.90, k=2, power=2, base=0.95, lambda=NA, tau.num=c(NA, 3, 6)[ntype], offset=NULL, w=rep(1, length(y.train)), ntree=c(200L, 50L, 50L)[ntype], numcut=100L, ndpost=1000L, nskip=100L, keepevery=c(1L, 10L, 10L)[ntype], printevery=100L, transposed=FALSE, hostname=FALSE, mc.cores = 1L, ## mc.gbart only nice = 19L, ## mc.gbart only seed = 99L ## mc.gbart only ) mc.gbart( x.train, y.train, x.test=matrix(0,0,0), type='wbart', ntype=as.integer( factor(type, levels=c('wbart', 'pbart', 'lbart'))), sparse=FALSE, theta=0, omega=1, a=0.5, b=1, augment=FALSE, rho=NULL, xinfo=matrix(0,0,0), usequants=FALSE, rm.const=TRUE, sigest=NA, sigdf=3, sigquant=0.90, k=2, power=2, base=0.95, lambda=NA, tau.num=c(NA, 3, 6)[ntype], offset=NULL, w=rep(1, length(y.train)), ntree=c(200L, 50L, 50L)[ntype], numcut=100L, ndpost=1000L, nskip=100L, keepevery=c(1L, 10L, 10L)[ntype], printevery=100L, transposed=FALSE, hostname=FALSE, mc.cores = 2L, nice = 19L, seed = 99L ) ```

## 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 created if q=2 where q is the number of levels of the factor. `gbart` will generate draws of f(x) for each x which is a row of `x.train`. `y.train` Continuous or binary dependent variable for training (in sample) data. If y is numeric, then a continuous BART model is fit (Normal errors). If y is binary (has only 0's and 1's), then a binary BART model with a probit link is fit by default: you can over-ride the default via the argument `type` to specify a logit BART model. `x.test` Explanatory variables for test (out of sample) data. Should have same structure as `x.train`. `gbart` will generate draws of f(x) for each x which is a row of `x.test`. `type` You can use this argument to specify the type of fit. `'wbart'` for continuous BART, `'pbart'` for probit BART or `'lbart'` for logit BART. `ntype` The integer equivalent of `type` where `'wbart'` is 1, `'pbart'` is 2 and `'lbart'` is 3. `sparse` Whether to perform variable selection based on a sparse Dirichlet prior rather than simply uniform; see Linero 2016. `theta` Set theta parameter; zero means random. `omega` Set omega parameter; zero means random. `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. `rm.const` Whether or not to remove constant variables. `sigest` The prior for the error variance (sigma\^2) is inverted chi-squared (the standard conditionally conjugate prior). The prior is specified by choosing the degrees of freedom, a rough estimate of the corresponding standard deviation and a quantile to put this rough estimate at. If `sigest=NA` then the rough estimate will be the usual least squares estimator. Otherwise the supplied value will be used. Not used if y is binary. `sigdf` Degrees of freedom for error variance prior. Not used if y is binary. `sigquant` The quantile of the prior that the rough estimate (see `sigest`) is placed at. The closer the quantile is to 1, the more aggresive the fit will be as you are putting more prior weight on error standard deviations (sigma) less than the rough estimate. Not used if y is binary. `k` For numeric y, `k` is the number of prior standard deviations E(Y|x) = f(x) is away from +/-0.5. The response, codey.train, is internally scaled to range from -0.5 to 0.5. For binary y, `k` is the number of prior standard deviations f(x) is away from +/-3. The bigger `k` is, the more conservative the fitting will be. `power` Power parameter for tree prior. `base` Base parameter for tree prior.
 `lambda` The scale of the prior for the variance. If `lambda` is zero, then the variance is to be considered fixed and known at the given value of `sigest`. Not used if y is binary. `tau.num` The numerator in the `tau` definition, i.e., `tau=tau.num/(k*sqrt(ntree))`.
 `offset` Continous BART operates on `y.train` centered by `offset` which defaults to `mean(y.train)`. With binary BART, the centering is P(Y=1 | x) = F(f(x) + offset) where `offset` defaults to `F^{-1}(mean(y.train))`. You can use the `offset` parameter to over-ride these defaults. `w` Vector of weights which multiply the standard deviation. Not used if y is binary. `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) values are used. `ndpost` The number of posterior draws returned. `nskip` Number of MCMC iterations to be treated as burn in. `printevery` As the MCMC runs, a message is printed every printevery draws. `keepevery` Every keepevery draw is kept to be returned to the user. `transposed` When running `gbart` in parallel, it is more memory-efficient to transpose `x.train` and `x.test`, if any, prior to calling `mc.gbart`. `hostname` When running on a cluster occasionally it is useful to track on which node each chain is running; to do so set this argument to `TRUE`. `seed` Setting the seed required for reproducible MCMC. `mc.cores` Number of cores to employ in parallel. `nice` Set the job niceness. The default niceness is 19: niceness goes from 0 (highest) to 19 (lowest).

## Details

BART is a Bayesian MCMC method. At each MCMC interation, we produce a draw from the joint posterior (f,sigma) \| (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) (and sigma* in the numeric case) where * denotes a particular draw. The x is either a row from the training data, `x.train` or the test data, `x.test`.

For `x.train`/x.test with missing data elements, `gbart` will singly impute them with hot decking. For one or more missing covariates, record-level hot-decking imputation deWaPann11 is employed that is biased towards the null, i.e., nonmissing values from another record are randomly selected regardless of the outcome. Since `mc.gbart` runs multiple `gbart` threads in parallel, `mc.gbart` performs multiple imputation with hot decking, i.e., a separate imputation for each thread. This record-level hot-decking imputation is biased towards the null, i.e., nonmissing values from another record are randomly selected regardless of `y.train`.

## Value

`gbart` returns an object of type `gbart` which is essentially a list. In the numeric y case, the list has components:

 `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.test` Same as yhat.train but now the x's are the rows of the test data. `yhat.train.mean` train data fits = mean of yhat.train columns. `yhat.test.mean` test data fits = mean of yhat.test columns. `sigma` post burn in draws of sigma, length = ndpost. `first.sigma` burn-in draws of sigma. `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. `sigest` The rough error standard deviation (sigma) used in the prior.

`pbart`

## 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``` ```##simulate data (example from Friedman MARS paper) f = function(x){ 10*sin(pi*x[,1]*x[,2]) + 20*(x[,3]-.5)^2+10*x[,4]+5*x[,5] } sigma = 1.0 #y = f(x) + sigma*z , z~N(0,1) n = 100 #number of observations set.seed(99) x=matrix(runif(n*10),n,10) #10 variables, only first 5 matter Ey = f(x) y=Ey+sigma*rnorm(n) lmFit = lm(y~.,data.frame(x,y)) #compare lm fit to BART later ##test BART with token run to ensure installation works set.seed(99) bartFit = wbart(x,y,nskip=5,ndpost=5) ## Not run: ##run BART set.seed(99) bartFit = wbart(x,y) ##compare BART fit to linear matter and truth = Ey fitmat = cbind(y,Ey,lmFit\$fitted,bartFit\$yhat.train.mean) colnames(fitmat) = c('y','Ey','lm','bart') print(cor(fitmat)) ## End(Not run) ```

### Example output

```Loading required package: survival
*****Into main of wbart
*****Data:
data:n,p,np: 100, 10, 0
y1,yn: -7.976311, -4.977809
x1,x[n*p]: 0.584712, 0.123268
*****Number of Trees: 200
*****Number of Cut Points: 100
*****burn and ndpost: 5, 5
*****Prior:
beta,alpha,tau,nu,lambda: 2.000000,0.950000,0.460475,3.000000,1.480161
*****sigma: 2.756573
*****w (weights): 1.000000 ... 1.000000
*****nkeeptrain,nkeeptest,nkeeptestme,nkeeptreedraws: 5,5,5,5
*****printevery: 100
*****skiptr,skipte,skipteme,skiptreedraws: 1,1,1,1

MCMC
done 0 (out of 10)
time: 0s
check counts
trcnt,tecnt,temecnt,treedrawscnt: 5,0,0,5
*****Into main of wbart
*****Data:
data:n,p,np: 100, 10, 0
y1,yn: -7.976311, -4.977809
x1,x[n*p]: 0.584712, 0.123268
*****Number of Trees: 200
*****Number of Cut Points: 100
*****burn and ndpost: 100, 1000
*****Prior:
beta,alpha,tau,nu,lambda: 2.000000,0.950000,0.460475,3.000000,1.480161
*****sigma: 2.756573
*****w (weights): 1.000000 ... 1.000000
*****nkeeptrain,nkeeptest,nkeeptestme,nkeeptreedraws: 1000,1000,1000,1000
*****printevery: 100
*****skiptr,skipte,skipteme,skiptreedraws: 1,1,1,1

MCMC
done 0 (out of 1100)
done 100 (out of 1100)
done 200 (out of 1100)
done 300 (out of 1100)
done 400 (out of 1100)
done 500 (out of 1100)
done 600 (out of 1100)
done 700 (out of 1100)
done 800 (out of 1100)
done 900 (out of 1100)
done 1000 (out of 1100)
time: 2s
check counts
trcnt,tecnt,temecnt,treedrawscnt: 1000,0,0,1000
y        Ey        lm      bart
y    1.0000000 0.9847984 0.8841787 0.9977909
Ey   0.9847984 1.0000000 0.9009389 0.9889719
lm   0.8841787 0.9009389 1.0000000 0.8999736
bart 0.9977909 0.9889719 0.8999736 1.0000000
```

BART documentation built on Jan. 5, 2021, 9:06 a.m.