bart {BayesTree} | R Documentation |
Bayesian Additive Regression Trees
Description
BART is a Bayesian “sum-of-trees” model.
For numeric response y
, we have
y = f(x) + \epsilon
,
where \epsilon \sim N(0,\sigma^2)
.
For a binary response y
, P(Y=1 | x) = F(f(x))
, where F
denotes the standard normal cdf (probit 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
bart(
x.train, y.train, x.test=matrix(0.0,0,0),
sigest=NA, sigdf=3, sigquant=.90,
k=2.0,
power=2.0, base=.95,
binaryOffset=0,
ntree=200,
ndpost=1000, nskip=100,
printevery=100, keepevery=1, keeptrainfits=TRUE,
usequants=FALSE, numcut=100, printcutoffs=0,
verbose=TRUE)
## S3 method for class 'bart'
plot(
x,
plquants=c(.05,.95), cols =c('blue','black'),
...)
Arguments
x.train |
Explanatory variables for training (in sample) data. |
y.train |
Dependent variable for training (in sample) data. |
x.test |
Explanatory variables for test (out of sample) data. |
sigest |
The prior for the error variance ( |
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 ( |
k |
For numeric y,
k is the number of prior standard deviations |
power |
Power parameter for tree prior. |
base |
Base parameter for tree prior. |
binaryOffset |
Used for binary |
ntree |
The number of trees in the sum. |
ndpost |
The number of posterior draws after burn in, ndpost/keepevery will actually be 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. |
keeptrainfits |
If true the draws of |
usequants |
Decision rules in the tree are of the form
|
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 |
printcutoffs |
The number of cutoff rules c to printed to screen before the MCMC is run. Give a single integer, the same value will be used for all variables. If 0, nothing is printed. |
verbose |
Logical, if FALSE supress printing. |
x |
Value returned by |
plquants |
In the plots, beliefs about |
cols |
Vector of two colors. First color is used to plot the median of |
... |
Additional arguments passed on to plot. |
Details
BART is an 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).
Value
The plot
method sets mfrow to c(1,2) and makes two plots.
The first plot is the sequence of kept draws of \sigma
including the burn-in draws. Initially these draws will decline as BART finds fit
and then level off when the MCMC has burnt in.
The second plot has y
on the horizontal axis and posterior intervals for
the corresponding f(x)
on the vertical axis.
bart
returns a list assigned class ‘bart’.
In the numeric y
case, the list has components:
yhat.train |
A matrix with (ndpost/keepevery) rows and nrow(x.train) columns.
Each row corresponds to a draw |
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/keepevery. |
first.sigma |
burn-in draws of sigma. |
varcount |
a matrix with (ndpost/keepevery) 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 ( |
y |
The input dependent vector of values for the dependent variable. |
In the binary y
case, the returned list has the components
yhat.train, yhat.test, and varcount as above. In addition the list
has a binaryOffset component giving the value used.
Note that in the binary y
, case yhat.train and yhat.test are
f(x)
+ binaryOffset. If you want draws of the probability
P(Y=1 | x)
you need to apply the normal cdf (pnorm
)
to these values.
Note
There was a bug in BayesTree_0.1-0 (now fixed of course).
If the number of test observations was less than the number of trees
(200 is the default), the yhat.test and yhat.test.mean components were suspect.
Author(s)
Hugh Chipman: hugh.chipman@gmail.com
Robert McCulloch: robert.e.mcculloch@gmail.com.
References
Chipman, H., George, E., and McCulloch R. (2010) Bayesian Additive Regression Trees. The Annals of Applied Statistics, 4,1, 266-298.
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
Examples
##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
##run BART
set.seed(99)
bartFit = bart(x,y,ndpost=200) #default is ndpost=1000, this is to run example fast.
plot(bartFit) # plot bart fit
##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))