Title: | Discrete Bayesian Additive Regression Trees Sampler |
---|---|
Description: | Fits Bayesian additive regression trees (BART; Chipman, George, and McCulloch (2010) <doi:10.1214/09-AOAS285>) while allowing the updating of predictors or response so that BART can be incorporated as a conditional model in a Gibbs/Metropolis-Hastings sampler. Also serves as a drop-in replacement for package 'BayesTree'. |
Authors: | Vincent Dorie [aut, cre] , Hugh Chipman [aut], Robert McCulloch [aut], Armon Dadgar [ctb] (adaptive radix tree), R Core Team [ctb] (basis of RNG), Guido U Draheim [ctb] (ax_check_compile_flag.m4), Maarten Bosmans [ctb] (ax_check_compile_flag.m4), Christophe Tournayre [ctb] (ax_compiler_ext.m4, ax_ext.m4), Michael Petch [ctb] (ax_compiler_ext.m4, ax_ext.m4, ax_gcc_x86_avx_xgetbv.m4, ax_gcc_x86_cpuid.m4), Rafael de Lucena Valle [ctb] (ax_compiler_ext.m4, ax_ext.m4), Steven G. Johnson [ctb] (ax_compiler_vendor.m4, ax_gcc_x86_cpuid.m4, ax_pthread.m4, <https://orcid.org/0000-0001-7327-4967>), Matteo Frigo [ctb] (ax_compiler_vendor.m4, ax_gcc_x86_cpuid.m4), John Zaitseff [ctb] (ax_compiler_vendor.m4), Todd Veldhuizen [ctb] (ax_cxx_namespace_std.m4), Luc Maisonobe [ctb] (ax_cxx_namespace_std.m4), Scott Pakin [ctb] (ax_func_posix_memalign.m4, <https://orcid.org/0000-0002-5220-1985>), Daniel Richard G. [ctb] (ax_pthread.m4) |
Maintainer: | Vincent Dorie <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.9-28 |
Built: | 2024-11-21 18:27:23 UTC |
Source: | https://github.com/vdorie/dbarts |
BART is a Bayesian “sum-of-trees” model in which each tree is constrained by a prior to be a weak learner.
For numeric response , where
.
For binary response ,
, where
denotes the standard normal cdf (probit link).
bart( x.train, y.train, x.test = matrix(0.0, 0, 0), sigest = NA, sigdf = 3, sigquant = 0.90, k = 2.0, power = 2.0, base = 0.95, splitprobs = 1 / numvars, binaryOffset = 0.0, weights = NULL, ntree = 200, ndpost = 1000, nskip = 100, printevery = 100, keepevery = 1, keeptrainfits = TRUE, usequants = FALSE, numcut = 100, printcutoffs = 0, verbose = TRUE, nchain = 1, nthread = 1, combinechains = TRUE, keeptrees = FALSE, keepcall = TRUE, sampleronly = FALSE, seed = NA_integer_, proposalprobs = NULL, keepsampler = keeptrees) bart2( formula, data, test, subset, weights, offset, offset.test = offset, sigest = NA_real_, sigdf = 3.0, sigquant = 0.90, k = NULL, power = 2.0, base = 0.95, split.probs = 1 / num.vars, n.trees = 75L, n.samples = 500L, n.burn = 500L, n.chains = 4L, n.threads = min(dbarts::guessNumCores(), n.chains), combineChains = FALSE, n.cuts = 100L, useQuantiles = FALSE, n.thin = 1L, keepTrainingFits = TRUE, printEvery = 100L, printCutoffs = 0L, verbose = TRUE, keepTrees = FALSE, keepCall = TRUE, samplerOnly = FALSE, seed = NA_integer_, proposal.probs = NULL, keepSampler = keepTrees, ...) ## S3 method for class 'bart' plot( x, plquants = c(0.05, 0.95), cols = c('blue', 'black'), ...) ## S3 method for class 'bart' predict( object, newdata, offset, weights, type = c("ev", "ppd", "bart"), combineChains = TRUE, ...) extract(object, ...) ## S3 method for class 'bart' extract( object, type = c("ev", "ppd", "bart", "trees"), sample = c("train", "test"), combineChains = TRUE, ...) ## S3 method for class 'bart' fitted( object, type = c("ev", "ppd", "bart"), sample = c("train", "test"), ...) ## S3 method for class 'bart' residuals(object, ...)
bart( x.train, y.train, x.test = matrix(0.0, 0, 0), sigest = NA, sigdf = 3, sigquant = 0.90, k = 2.0, power = 2.0, base = 0.95, splitprobs = 1 / numvars, binaryOffset = 0.0, weights = NULL, ntree = 200, ndpost = 1000, nskip = 100, printevery = 100, keepevery = 1, keeptrainfits = TRUE, usequants = FALSE, numcut = 100, printcutoffs = 0, verbose = TRUE, nchain = 1, nthread = 1, combinechains = TRUE, keeptrees = FALSE, keepcall = TRUE, sampleronly = FALSE, seed = NA_integer_, proposalprobs = NULL, keepsampler = keeptrees) bart2( formula, data, test, subset, weights, offset, offset.test = offset, sigest = NA_real_, sigdf = 3.0, sigquant = 0.90, k = NULL, power = 2.0, base = 0.95, split.probs = 1 / num.vars, n.trees = 75L, n.samples = 500L, n.burn = 500L, n.chains = 4L, n.threads = min(dbarts::guessNumCores(), n.chains), combineChains = FALSE, n.cuts = 100L, useQuantiles = FALSE, n.thin = 1L, keepTrainingFits = TRUE, printEvery = 100L, printCutoffs = 0L, verbose = TRUE, keepTrees = FALSE, keepCall = TRUE, samplerOnly = FALSE, seed = NA_integer_, proposal.probs = NULL, keepSampler = keepTrees, ...) ## S3 method for class 'bart' plot( x, plquants = c(0.05, 0.95), cols = c('blue', 'black'), ...) ## S3 method for class 'bart' predict( object, newdata, offset, weights, type = c("ev", "ppd", "bart"), combineChains = TRUE, ...) extract(object, ...) ## S3 method for class 'bart' extract( object, type = c("ev", "ppd", "bart", "trees"), sample = c("train", "test"), combineChains = TRUE, ...) ## S3 method for class 'bart' fitted( object, type = c("ev", "ppd", "bart"), sample = c("train", "test"), ...) ## S3 method for class 'bart' residuals(object, ...)
x.train |
Explanatory variables for training (in sample) data. May be a matrix or a data frame, with 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 |
y.train |
Dependent variable for training (in sample) data. If |
x.test |
Explanatory variables for test (out of sample) data. Should have same column structure as |
sigest |
For continuous response models, an estimate of the error variance, |
sigdf |
Degrees of freedom for error variance prior. Not applicable when |
sigquant |
The quantile of the error variance prior that the rough estimate ( |
k |
For numeric |
power |
Power parameter for tree prior. |
base |
Base parameter for tree prior. |
splitprobs , split.probs
|
Prior and transition probabilities of variables used to generate splits. Can be missing/empty/ |
binaryOffset |
Used for binary |
weights |
An optional vector of weights to be used in the fitting process. When present, BART fits a model with observations |
ntree , n.trees
|
The number of trees in the sum-of-trees formulation. |
ndpost , n.samples
|
The number of posterior draws after burn in, |
nskip , n.burn
|
Number of MCMC iterations to be treated as burn in. |
printevery , printEvery
|
As the MCMC runs, a message is printed every |
keepevery , n.thin
|
Every |
keeptrainfits , keepTrainingFits
|
If |
usequants , useQuantiles
|
When |
numcut , n.cuts
|
The maximum number of possible values used in decision rules (see |
printcutoffs , printCutoffs
|
The number of cutoff rules to printed to screen before the MCMC is run. Given a single integer, the same value will be used for all variables. If 0, nothing is printed. |
verbose |
Logical; if |
nchain , n.chains
|
Integer specifying how many independent tree sets and fits should be calculated. |
nthread , n.threads
|
Integer specifying how many threads to use. Depending on the CPU architecture, using more than the number of chains can degrade performance for small/medium data sets. As such some calculations may be executed single threaded regardless. |
combinechains , combineChains
|
Logical; if |
keeptrees , keepTrees
|
Logical; must be |
keepcall , keepCall
|
Logical; if |
seed |
Optional integer specifying the desired pRNG seed. It should not be needed when running single-threaded - |
proposalprobs , proposal.probs
|
Named numeric vector or |
keepsampler , keepSampler
|
Logical that can be used to save the underlying |
formula |
The same as |
data |
The same as |
test |
The same as |
subset |
A vector of logicals or indicies used to subset of the data. Can be missing. |
offset |
The same as |
offset.test |
A vector of offsets to be used with test data, in case it is different than the training offset. If |
object |
An object of class |
newdata |
Test data for prediction. Obeys all the same rules as |
sampleronly , samplerOnly
|
Builds the sampler from its arguments and returns it without running it. Useful to use the |
x |
Object of class |
plquants |
In the plots, beliefs about |
cols |
Vector of two colors. First color is used to plot the median of |
type |
The quantity to be returned by generic functions. Options are |
sample |
Either |
... |
Additional arguments passed on to |
BART is an Bayesian MCMC method. At each MCMC interation, we produce a draw from the joint posterior in the numeric
case and just
in the binary
case.
Thus, unlike a lot of other modeling methods in R, bart
does not produce a single model object from which fits and summaries may be extracted. The output consists of values (and
in the numeric case) where * denotes a particular draw. The
is either a row from the training data (
x.train
) or the test data (x.test
).
Decision rules for any tree are of the form vs.
for each ‘
’ corresponding to a column of
x.train
. usequants
determines the means by which the set of possible is determined. If
usequants
is TRUE
, then the are a subset of the values interpolated half-way between the unique, sorted values obtained from the corresponding column of
x.train
. If usequants
is FALSE
, the cutoffs are equally spaced across the range of values taken on by the corresponding column of x.train
.
The number of possible values of is determined by
numcut
. 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 for a variable the minimum of numcut
and one less than the number of unique elements for that variable are used.
k
The amount of shrinkage of the node parameters is controlled by k
. k
can be given as either a fixed, positive number, or as any value that can be used to build a supported hyperprior. At present, only priors are supported, where
is a degrees of freedom and
is a scale. Both values must be positive, however the scale can be infinite which yields an improper prior, which is interpretted as just the polynomial part of the distribution. If
is 1 and
is
, the prior is “flat”.
For BART on binary outcomes, the degree of overfitting can be highly sensitive to k
so it is encouraged to consider a number of values. The default hyperprior for binary BART, chi(1.25, Inf)
, has been shown to work well in a large number of datasets, however crossvalidation may be helpful. Running for a short time with a flat prior may be helpful to see the range of values of k
that are consistent with the data.
bart
and rbart_vi
support fitted
to return the posterior mean of a predicted quantity, as well as predict
to return a set of posterior samples for a different sample. In addition, the extract
generic can be used to obtain the posterior samples for the training data or test data supplied during the initial fit.
Using predict
with a bart
object requires that it be fitted with the option keeptrees
/keepTrees
as TRUE
. Keeping the trees for a fit can require a sizeable amount of memory and is off by default.
All generics return values on the scale of expected value of the response by default. This means that predict
, extract
, and fitted
for binary outcomes return probabilities unless specifically the sum-of-trees component is requested (type = "bart"
). This is in contrast to yhat.train
/yhat.test
that are returned with the fitted model.
save
ing and load
ing fitted BART objects for use with predict
requires that R's serialization mechanism be able to access the underlying trees, in addition to being fit with keeptrees
/keepTrees
as TRUE
. For memory purposes, the trees are not stored as R objects unless specifically requested. To do this, one must “touch” the sampler's state object before saving, e.g. for a fitted object bartFit
, execute invisible(bartFit$fit$state)
.
Behavior differs when running multi- and single-threaded, as the pseudo random number generators (pRNG) used by R are not thread safe. When single-threaded, R's built-in generator is used; if set at the start, the global .Random.seed
will be used and its value updated as samples are drawn. When multi-threaded, the default behavior is to draw new random seeds for each thread using the clock and use thread-specific pRNGs.
This behavior can be modified by setting seed
, or by using ...
to pass arguments to dbartsControl
. For the single-threaded case, a new pRNG is built using that seed that is separate from R's native generator. As such, the global state will not be modified by subsequent calls to the generator. For multi-threaded, the seeds for threads are drawn sequentially using the supplied seed, and will again be separate from R's native generator.
Consequently, the seed
argument is not needed when running single-threaded - set.seed
will suffice. However, when multi-threaded the seed
argument can be used to obtain reproducible results.
When a model is fit with keeptrees
(bart
) or keepTrees
(bart2
) equal to TRUE
, the generic extract
can be used to retrieve a data frame containing the tree fit information. In this case, extract
will accept the additional, optional arguments: chainNums
, sampleNums
, and treeNums
. Each should be an integer vector detailing the desired trees to be returned.
The result of extract
will be a data frame with columns:
sample
, chain
, tree
- index variables
n
- number of observations in node
var
- either the index of the variable used for splitting or -1 if the node is a leaf
value
- either the value such that observations less than or equal to it are sent down the left path of the tree or the predicted value for a leaf node
The order of nodes in the result corresponds to a depth-first traversal, going down the left-side first. The names of variables used in splitting can be recovered by examining the column names of the fit$data@x
element of a fitted bart
or bart2
model. See the package vignette “Working with dbarts Saved Trees”.
bart
and bart2
return lists assigned the class bart
. For applicable quantities, ndpost / keepevery
samples are returned. In the numeric case, the list has components:
yhat.train |
A array/matrix of posterior samples. The |
yhat.test |
Same as |
yhat.train.mean |
Vector of means of |
yhat.test.mean |
Vector of means of |
sigma |
Matrix of posterior samples of |
first.sigma |
Burn-in draws of |
varcount |
A matrix with number of rows equal to the number of kept draws and each column corresponding to a training variable. Contains the total count of the number of times that variable is used in a tree decision rule (over all trees). |
sigest |
The rough error standard deviation ( |
y |
The input dependent vector of values for the dependent variable. This is used in |
fit |
Optional sampler object which stores the values of the tree splits. Required for using |
n.chains |
Information that can be lost if |
k |
Optional matrix of posterior samples of |
first.k |
Burn-in draws of |
In the binary 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 , case
yhat.train
and yhat.test
are . For draws of the probability
, apply the normal cdf (
pnorm
) to these values.
The plot
method sets mfrow
to c(1, 2)
and makes two plots. The first plot is the sequence of kept draws of including the burn-in draws. Initially these draws will decline as BART finds a good fit and then level off when the MCMC has burnt in. The second plot has
on the horizontal axis and posterior intervals for the corresponding
on the vertical axis.
Hugh Chipman: [email protected], Robert McCulloch: [email protected], Vincent Dorie: [email protected].
Chipman, H., George, E., and McCulloch, R. (2009) BART: Bayesian Additive Regression Trees.
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.
both of the above at: https://www.rob-mcculloch.org
Friedman, J.H. (1991) Multivariate adaptive regression splines. The Annals of Statistics, 19, 1–67.
## simulate data (example from Friedman MARS paper) ## y = f(x) + epsilon , epsilon ~ N(0, sigma) ## x consists of 10 variables, only first 5 matter f <- function(x) { 10 * sin(pi * x[,1] * x[,2]) + 20 * (x[,3] - 0.5)^2 + 10 * x[,4] + 5 * x[,5] } set.seed(99) sigma <- 1.0 n <- 100 x <- matrix(runif(n * 10), n, 10) Ey <- f(x) y <- rnorm(n, Ey, sigma) ## run BART set.seed(99) bartFit <- bart(x, y) plot(bartFit) ## compare BART fit to linear matter and truth = Ey lmFit <- lm(y ~ ., data.frame(x, y)) fitmat <- cbind(y, Ey, lmFit$fitted, bartFit$yhat.train.mean) colnames(fitmat) <- c('y', 'Ey', 'lm', 'bart') print(cor(fitmat))
## simulate data (example from Friedman MARS paper) ## y = f(x) + epsilon , epsilon ~ N(0, sigma) ## x consists of 10 variables, only first 5 matter f <- function(x) { 10 * sin(pi * x[,1] * x[,2]) + 20 * (x[,3] - 0.5)^2 + 10 * x[,4] + 5 * x[,5] } set.seed(99) sigma <- 1.0 n <- 100 x <- matrix(runif(n * 10), n, 10) Ey <- f(x) y <- rnorm(n, Ey, sigma) ## run BART set.seed(99) bartFit <- bart(x, y) plot(bartFit) ## compare BART fit to linear matter and truth = Ey lmFit <- lm(y ~ ., data.frame(x, y)) fitmat <- cbind(y, Ey, lmFit$fitted, bartFit$yhat.train.mean) colnames(fitmat) <- c('y', 'Ey', 'lm', 'bart') print(cor(fitmat))
Creates a sampler object for a given problem which fits a Bayesian Additive Regreesion Trees model. Internally stores state in such a way as to be mutable.
dbarts( formula, data, test, subset, weights, offset, offset.test = offset, verbose = FALSE, n.samples = 800L, tree.prior = cgm, node.prior = normal, resid.prior = chisq, proposal.probs = c( birth_death = 0.5, swap = 0.1, change = 0.4, birth = 0.5), control = dbarts::dbartsControl(), sigma = NA_real_)
dbarts( formula, data, test, subset, weights, offset, offset.test = offset, verbose = FALSE, n.samples = 800L, tree.prior = cgm, node.prior = normal, resid.prior = chisq, proposal.probs = c( birth_death = 0.5, swap = 0.1, change = 0.4, birth = 0.5), control = dbarts::dbartsControl(), sigma = NA_real_)
formula |
An object of class |
data |
An optional data frame, list, or environment containing predictors to be used with the model. For backwards compatibility, can also be the |
test |
An optional matrix or data frame with the same number of predictors as |
subset |
An optional vector specifying a subset of observations to be used in the fitting process. |
weights |
An optional vector of weights to be used in the fitting process. When present, BART fits a model with observations |
offset |
An optional vector specifying an offset from 0 for the relationship between the underyling function, |
offset.test |
The equivalent of |
verbose |
A logical determining if additional output is printed to the console. See |
n.samples |
A positive integer setting the default number of posterior samples to be returned for each run of the sampler. Can be overriden at run-time. See |
tree.prior |
An expression of the form |
node.prior |
An expression of the form |
resid.prior |
An expression of the form |
proposal.probs |
Named numeric vector or |
control |
An object inheriting from |
sigma |
A positive numeric estimate of the residual standard deviation. If |
“Discrete sampler” refers to that dbarts
is implemented using ReferenceClasses, so that there exists a mutable object constructed in C++ that is largely obscured from R. The dbarts
function is the primary way of creating a dbartsSampler
, for which a variety of methods exist.
A reference object of dbartsSampler
.
Convenience function to create a control object for use with a dbarts
sampler.
dbartsControl( verbose = FALSE, keepTrainingFits = TRUE, useQuantiles = FALSE, keepTrees = FALSE, n.samples = NA_integer_, n.cuts = 100L, n.burn = 200L, n.trees = 75L, n.chains = 4L, n.threads = dbarts::guessNumCores(), n.thin = 1L, printEvery = 100L, printCutoffs = 0L, rngKind = "default", rngNormalKind = "default", rngSeed = NA_integer_, updateState = TRUE)
dbartsControl( verbose = FALSE, keepTrainingFits = TRUE, useQuantiles = FALSE, keepTrees = FALSE, n.samples = NA_integer_, n.cuts = 100L, n.burn = 200L, n.trees = 75L, n.chains = 4L, n.threads = dbarts::guessNumCores(), n.thin = 1L, printEvery = 100L, printCutoffs = 0L, rngKind = "default", rngNormalKind = "default", rngSeed = NA_integer_, updateState = TRUE)
verbose |
Logical controlling sampler output to console. |
keepTrainingFits |
Logical controlling whether or not training fits are returned when the sampler runs. These are always computed as part of the fitting procedure, so disabling will not substantially impact running time. |
useQuantiles |
Logical to determine if the empirical quantiles of a columns of predictors should be used to determine the tree decision rules. If |
keepTrees |
A logical that determines whether or not trees are cached as they are sampled. In all cases, the current state of the sampler is stored as a single set of |
n.samples |
A non-negative integer giving the default number of samples to return each time the sampler is run. Generally specified by |
n.cuts |
A positive integer or integer vector giving the number of decision rules to be used for each given predictor. If of length less than the number of predictors, earlier values are recycled. If for any predictor more values are specified than are coherent, fewer may be used. See details for more information. |
n.burn |
A non-negative integer determining how many samples, if any, are thrown away at the beginning of a run of the sampler. |
n.trees |
A positive integer giving the number of trees used in the sum-of-trees formulation. |
n.chains |
A positive integer detailing the number of independent chains for the sampler to use. |
n.threads |
A positive integer controlling how many threads will be used for various internal calculations, as well as the number of chains. Internal calculations are highly optimized so that single-threaded performance tends to be superior unless the number of observations is very large (>10k), so that it is often not necessary to have the number of threads exceed the number of chains. |
n.thin |
A positive integer determining how many iterations the MCMC chain should jump on the decision trees alone before recording a sample. Serves to “thin” the samples against serial correlation. |
printEvery |
If |
printCutoffs |
A non-negative integer specifying how many of the decision rules for a variable are printed in verbose mode. |
rngKind |
Random number generator kind, as used in |
rngNormalKind |
Random number generator normal kind, as used in |
rngSeed |
Random number generator seed, as used in |
updateState |
Logical setting the default behavior for many sampler methods with regards to the immediate updating of the cached state of the object. A current, cached state is only useful when saving/loading the sampler. |
An object of class dbartControl
.
Convenience function to create a data object for use with a dbarts
sampler.
dbartsData( formula, data, test, subset, weights, offset, offset.test = offset)
dbartsData( formula, data, test, subset, weights, offset, offset.test = offset)
formula , data , test , subset , weights , offset , offset.test
|
As in |
An object of class dbartData
.
A reference class object that contains a Bayesian Additive Regression Trees sampler in such a way that it can be modified, stopped, and started all while maintaining its own state.
## S4 method for signature 'dbartsSampler' run(numBurnIn, numSamples, updateState = NA) ## S4 method for signature 'dbartsSampler' sampleTreesFromPrior(updateState = NA) ## S4 method for signature 'dbartsSampler' sampleNodeParametersFromPrior(updateState = NA) ## S4 method for signature 'dbartsSampler' copy(shallow = FALSE) ## S4 method for signature 'dbartsSampler' show() ## S4 method for signature 'dbartsSampler' predict(x.test, offset.test) ## S4 method for signature 'dbartsSampler' setControl(control) ## S4 method for signature 'dbartsSampler' setModel(model) ## S4 method for signature 'dbartsSampler' setData(data) ## S4 method for signature 'dbartsSampler' setResponse(y, updateState = NA) ## S4 method for signature 'dbartsSampler' setOffset(offset, updateScale = FALSE, updateState = NA) ## S4 method for signature 'dbartsSampler' setSigma(sigma, updateState = NA) ## S4 method for signature 'dbartsSampler' setPredictor(x, column, updateState = NA) ## S4 method for signature 'dbartsSampler' setTestPredictor(x.test, column, updateState = NA) ## S4 method for signature 'dbartsSampler' setTestPredictorAndOffset(x.test, offset.test, updateState = NA) ## S4 method for signature 'dbartsSampler' setTestOffset(offset.test, updateState = NA) ## S4 method for signature 'dbartsSampler' printTrees(treeNums) ## S4 method for signature 'dbartsSampler' plotTree( treeNum, treePlotPars = c( nodeHeight = 12, nodeWidth = 40, nodeGap = 8), ...)
## S4 method for signature 'dbartsSampler' run(numBurnIn, numSamples, updateState = NA) ## S4 method for signature 'dbartsSampler' sampleTreesFromPrior(updateState = NA) ## S4 method for signature 'dbartsSampler' sampleNodeParametersFromPrior(updateState = NA) ## S4 method for signature 'dbartsSampler' copy(shallow = FALSE) ## S4 method for signature 'dbartsSampler' show() ## S4 method for signature 'dbartsSampler' predict(x.test, offset.test) ## S4 method for signature 'dbartsSampler' setControl(control) ## S4 method for signature 'dbartsSampler' setModel(model) ## S4 method for signature 'dbartsSampler' setData(data) ## S4 method for signature 'dbartsSampler' setResponse(y, updateState = NA) ## S4 method for signature 'dbartsSampler' setOffset(offset, updateScale = FALSE, updateState = NA) ## S4 method for signature 'dbartsSampler' setSigma(sigma, updateState = NA) ## S4 method for signature 'dbartsSampler' setPredictor(x, column, updateState = NA) ## S4 method for signature 'dbartsSampler' setTestPredictor(x.test, column, updateState = NA) ## S4 method for signature 'dbartsSampler' setTestPredictorAndOffset(x.test, offset.test, updateState = NA) ## S4 method for signature 'dbartsSampler' setTestOffset(offset.test, updateState = NA) ## S4 method for signature 'dbartsSampler' printTrees(treeNums) ## S4 method for signature 'dbartsSampler' plotTree( treeNum, treePlotPars = c( nodeHeight = 12, nodeWidth = 40, nodeGap = 8), ...)
numBurnIn |
A non-negative integer determining how many iterations the sampler should skip before storing results. If missing or |
numSamples |
A positive integer determining how many posterior samples should be returned. If missing or |
updateState |
A logical determining if the local cache of the sampler's state should be updated after the completion of the run. If |
shallow |
A logical determining if the copy should retain the underlying data of the sampler ( |
control |
An object inheriting from |
model |
An object inheriting from |
data |
An object inheriting from |
y |
A numeric response vector of length equal to that with which the sampler was created. |
x |
A numeric predictor vector of length equal to that with which the sampler was created. Can be of a distinct number of rows for |
x.test |
A new matrix of test predictors, of the number of columns equal to that in the current model. |
offset |
A numeric vector of length equal to that with which the sampler was created, or |
updateScale |
Logical indicating whether BART's internal scale should update with the new offset. Should only be |
offset.test |
A numeric vector of length equal to that of the test matrix, or |
sigma |
Numeric vector of residual standard deviations, one for each chain. |
column |
An integer or character string vector specifying which column/columns of the predictor matrix is to be replaced. If missing, the entire matrix is substituted. |
treeNums |
An integer vector listing the indices of the trees to print. |
treeNum |
An integer listing the indices of the tree to plot. |
treePlotPars |
A named numeric vector containing the quantities |
... |
Extra arguments to |
A dbartsSampler
is a mutable object which contains information pertaining to fitting a Bayesian additive regression tree model. The sampler is first created and then, in a separate instruction, run or modified. In this way, MCMC samplers can be constructed with BART components filling arbitrary roles.
save
-ing and load
ing a dbarts
sampler for future use requires that R's serialization mechanism be able to access the state of the sampler which, for memory purposes, is only made available to R on request. To do this, one must “touch” the sampler's state object before saving, e.g. for the object sampler
, execute invisible(sampler$state)
. This is in addition to guaranteeing that the state
object is not NULL
, which can be done by setting the sampler's control to an object with updateState
as TRUE
or passing TRUE
as the updateState
argument to any of the sampler's applicable methods.
For run
, a named-list with contents sigma
, train
, test
, and varcount
.
For setPredictor
, TRUE
/FALSE
depending on whether or not the operation was successful. The operation can fail if the new predictor results in a tree with an empty leaf-node. If only single columns were replaced, on the update is rolled-back so that the sampler remains in a valid state.
predict
keeps the current test matrix in place and uses the current set of tree splits. This function has two use cases. The first is when keepTrees
of dbartsControl
is TRUE
, in which case the sampler should be run to completion and the function can be used to interrogate the existing fit. When keepTrees
is FALSE
, the function can be used to obtain the likelihood as part of a proposed new set of covariates in a Metropolis-Hastings step in a full-Bayes sampler. This would typically be followed by a call to setPredictor
if the step is accepted.
Attempts to guess the number of CPU ‘cores’, both physical and logical.
guessNumCores(logical = FALSE)
guessNumCores(logical = FALSE)
logical |
A logical value. When |
Because of different definitions of cores used by different manufacturers, the distinction between logical and physical cores is not universally recognized. This function will attempt to use operating system definitions when available, which should usually match the CPU itself.
An integer, or NA if no clear answer was obtained.
Vincent Dorie: [email protected].
Converts a data frame with numeric and factor contents into a matrix, suitable for use with bart
. Unlike in linear regression, factors containing more than two levels result in dummy variables being created for each level.
makeModelMatrixFromDataFrame(x, drop = TRUE) makeind(x, all = TRUE) makeTestModelMatrix(data, newdata)
makeModelMatrixFromDataFrame(x, drop = TRUE) makeind(x, all = TRUE) makeTestModelMatrix(data, newdata)
x |
Data frame of explanatory variables. |
drop |
Logical or list controling whether or not columns that are constants or factor levels with no instances are omitted from the result. When a list, must be of length equal to
|
all |
Not currently implemented. |
data |
An existing |
newdata |
Test data frame. |
Character vectors are included as factors. If you have numeric data coded as characters, convert it using as.numeric
first.
Note that if you have train and test data frames, it may be best to rbind
the two together, apply makeModelMatrixFromDataFrame
to the result, and then pull them back apart. Alternatively, save the drop attribute used in creating the training data and use it when creating a matrix from the test data, as in the example given below.
Use of these functions is not required when using bart
, bart2
, or dbartsSampler
; they exist to allow the user finer control and to assist with writing packages that separate the creation of training from test data.
A matrix with columns corresponding to the elements of the data frame. If drop = TRUE
or is a list, the attribute drop
on the result is set to the list used when creating the matrix.
Vincent Dorie: [email protected].
iv <- 1:10 rv <- runif(10) f <- factor(rep(seq.int(3), c(4L, 4L, 2L)), labels = c("alice", "bob", "charlie")) df <- data.frame(iv, rv, f) mm <- makeModelMatrixFromDataFrame(df) ## create test and train matrices with disjoint factor levels train.df <- df[1:8,] test.df <- df[9:10,] train.mm <- makeModelMatrixFromDataFrame(train.df) test.mm <- makeModelMatrixFromDataFrame(test.df, attr(train.mm, "drop"))
iv <- 1:10 rv <- runif(10) f <- factor(rep(seq.int(3), c(4L, 4L, 2L)), labels = c("alice", "bob", "charlie")) df <- data.frame(iv, rv, f) mm <- makeModelMatrixFromDataFrame(df) ## create test and train matrices with disjoint factor levels train.df <- df[1:8,] test.df <- df[9:10,] train.mm <- makeModelMatrixFromDataFrame(train.df) test.mm <- makeModelMatrixFromDataFrame(test.df, attr(train.mm, "drop"))
Run bart
at test observations constructed so that a plot can be created displaying the effect of a single variable (pdbart
) or pair of variables (pd2bart
). Note that if is a binary with
,
the standard normal cdf, then the plots are all on the
scale.
pdbart( x.train, y.train, xind = NULL, levs = NULL, levquants = c(0.05, seq(0.1, 0.9, 0.1), 0.95), pl = TRUE, plquants = c(0.05, 0.95), ...) ## S3 method for class 'pdbart' plot( x, xind = seq_len(length(x$fd)), plquants = c(0.05, 0.95), cols = c('black', 'blue'), ...) pd2bart( x.train, y.train, xind = NULL, levs = NULL, levquants = c(0.05, seq(0.1, 0.9, 0.1), 0.95), pl = TRUE, plquants = c(0.05, 0.95), ...) ## S3 method for class 'pd2bart' plot( x, plquants = c(0.05, 0.95), contour.color = 'white', justmedian = TRUE, ...)
pdbart( x.train, y.train, xind = NULL, levs = NULL, levquants = c(0.05, seq(0.1, 0.9, 0.1), 0.95), pl = TRUE, plquants = c(0.05, 0.95), ...) ## S3 method for class 'pdbart' plot( x, xind = seq_len(length(x$fd)), plquants = c(0.05, 0.95), cols = c('black', 'blue'), ...) pd2bart( x.train, y.train, xind = NULL, levs = NULL, levquants = c(0.05, seq(0.1, 0.9, 0.1), 0.95), pl = TRUE, plquants = c(0.05, 0.95), ...) ## S3 method for class 'pd2bart' plot( x, plquants = c(0.05, 0.95), contour.color = 'white', justmedian = TRUE, ...)
x.train |
Explanatory variables for training (in sample) data. Can be any valid input to |
y.train |
Dependent variable for training (in sample) data. Can be a numeric vector or, when passing |
xind |
Integer, character vector, or the right-hand side of a formula indicating which variables are to be plotted. In |
levs |
Gives the values of a variable at which the plot is to be constructed. Must be a list, where the |
levquants |
If |
pl |
For |
plquants |
In the plots, beliefs about |
... |
Additional arguments. In |
x |
For |
cols |
Vector of two colors. The first color is for the median of |
contour.color |
Color for contours plotted on top of the image. |
justmedian |
A logical where if |
We divide the predictor vector into a subgroup of interest,
and the complement
. A prediction
can then be written as
. To estimate the effect of
on the prediction, Friedman suggests the partial dependence function
where is the
th observation of
in the data. Note that
will generally not be one of the observed data points. Using BART it is straightforward to then estimate and even obtain uncertainty bounds for
. A draw of
from the induced BART posterior on
is obtained by simply computing
as a byproduct of each MCMC draw
. The median (or average) of these MCMC draws
then yields an estimate of
, and lower and upper quantiles can be used to obtain intervals for
.
In pdbart
consists of a single variable in
and in
pd2bart
it is a pair of variables.
This is a computationally intensive procedure. For example, in pdbart
, to compute the partial dependence plot for 5 values, we need to compute
for all possible
and there would be
of these where
is the sample size. All of that computation would be done for each kept BART draw. For this reason running BART with
keepevery
larger than 1 (eg. 10) makes the procedure much faster.
The plot methods produce the plots and don't return anything.
pdbart
and pd2bart
return lists with components given below. The list returned by pdbart
is assigned class pdbart
and the list returned by pd2bart
is assigned class pd2bart
.
fd |
A matrix whose For For |
levs |
The list of levels used, each component corresponding to a variable. If argument |
xlbs |
A vector of character strings which are the plotting labels used for the variables. |
The remaining components returned in the list are the same as in the value of bart
. They are simply passed on from the BART run used to create the partial dependence plot. The function plot.bart
can be applied to the object returned by pdbart
or pd2bart
to examine the BART run.
Hugh Chipman: [email protected].
Robert McCulloch: [email protected].
Chipman, H., George, E., and McCulloch, R. (2006) BART: Bayesian Additive Regression Trees.
Chipman, H., George, E., and McCulloch R. (2006) Bayesian Ensemble Learning.
both of the above at: https://www.rob-mcculloch.org/
Friedman, J.H. (2001) Greedy function approximation: A gradient boosting machine. The Annals of Statistics, 29, 1189–1232.
## Not run: ## simulate data f <- function(x) return(0.5 * x[,1] + 2 * x[,2] * x[,3]) sigma <- 0.2 n <- 100 set.seed(27) x <- matrix(2 * runif(n * 3) - 1, ncol = 3) colnames(x) <- c('rob', 'hugh', 'ed') Ey <- f(x) y <- rnorm(n, Ey, sigma) ## first two plot regions are for pdbart, third for pd2bart par(mfrow = c(1, 3)) ## pdbart: one dimensional partial dependence plot set.seed(99) pdb1 <- pdbart( x, y, xind = c(1, 2), levs = list(seq(-1, 1, 0.2), seq(-1, 1, 0.2)), pl = FALSE, keepevery = 10, ntree = 100 ) plot(pdb1, ylim = c(-0.6, 0.6)) ## pd2bart: two dimensional partial dependence plot set.seed(99) pdb2 <- pd2bart( x, y, xind = c(2, 3), levquants = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95), pl = FALSE, ntree = 100, keepevery = 10, verbose = FALSE) plot(pdb2) ## compare BART fit to linear model and truth = Ey lmFit <- lm(y ~ ., data.frame(x, y)) fitmat <- cbind(y, Ey, lmFit$fitted, pdb1$yhat.train.mean) colnames(fitmat) <- c('y', 'Ey', 'lm', 'bart') print(cor(fitmat)) ## example showing the use of a pre-fitted model df <- data.frame(y, x) set.seed(99) bartFit <- bart( y ~ rob + hugh + ed, df, keepevery = 10, ntree = 100, keeptrees = TRUE) pdb1 <- pdbart(bartFit, xind = rob + ed, pl = FALSE) ## End(Not run)
## Not run: ## simulate data f <- function(x) return(0.5 * x[,1] + 2 * x[,2] * x[,3]) sigma <- 0.2 n <- 100 set.seed(27) x <- matrix(2 * runif(n * 3) - 1, ncol = 3) colnames(x) <- c('rob', 'hugh', 'ed') Ey <- f(x) y <- rnorm(n, Ey, sigma) ## first two plot regions are for pdbart, third for pd2bart par(mfrow = c(1, 3)) ## pdbart: one dimensional partial dependence plot set.seed(99) pdb1 <- pdbart( x, y, xind = c(1, 2), levs = list(seq(-1, 1, 0.2), seq(-1, 1, 0.2)), pl = FALSE, keepevery = 10, ntree = 100 ) plot(pdb1, ylim = c(-0.6, 0.6)) ## pd2bart: two dimensional partial dependence plot set.seed(99) pdb2 <- pd2bart( x, y, xind = c(2, 3), levquants = c(0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95), pl = FALSE, ntree = 100, keepevery = 10, verbose = FALSE) plot(pdb2) ## compare BART fit to linear model and truth = Ey lmFit <- lm(y ~ ., data.frame(x, y)) fitmat <- cbind(y, Ey, lmFit$fitted, pdb1$yhat.train.mean) colnames(fitmat) <- c('y', 'Ey', 'lm', 'bart') print(cor(fitmat)) ## example showing the use of a pre-fitted model df <- data.frame(y, x) set.seed(99) bartFit <- bart( y ~ rob + hugh + ed, df, keepevery = 10, ntree = 100, keeptrees = TRUE) pdb1 <- pdbart(bartFit, xind = rob + ed, pl = FALSE) ## End(Not run)
Fits a varying intercept/random effect BART model.
rbart_vi( formula, data, test, subset, weights, offset, offset.test = offset, group.by, group.by.test, prior = cauchy, sigest = NA_real_, sigdf = 3.0, sigquant = 0.90, k = 2.0, power = 2.0, base = 0.95, n.trees = 75L, n.samples = 1500L, n.burn = 1500L, n.chains = 4L, n.threads = min(dbarts::guessNumCores(), n.chains), combineChains = FALSE, n.cuts = 100L, useQuantiles = FALSE, n.thin = 5L, keepTrainingFits = TRUE, printEvery = 100L, printCutoffs = 0L, verbose = TRUE, keepTrees = TRUE, keepCall = TRUE, seed = NA_integer_, keepSampler = keepTrees, keepTestFits = TRUE, callback = NULL, ...) ## S3 method for class 'rbart' plot( x, plquants = c(0.05, 0.95), cols = c('blue', 'black'), ...) ## S3 method for class 'rbart' fitted( object, type = c("ev", "ppd", "bart", "ranef"), sample = c("train", "test"), ...) ## S3 method for class 'rbart' extract( object, type = c("ev", "ppd", "bart", "ranef", "trees"), sample = c("train", "test"), combineChains = TRUE, ...) ## S3 method for class 'rbart' predict( object, newdata, group.by, offset, type = c("ev", "ppd", "bart", "ranef"), combineChains = TRUE, ...) ## S3 method for class 'rbart' residuals(object, ...)
rbart_vi( formula, data, test, subset, weights, offset, offset.test = offset, group.by, group.by.test, prior = cauchy, sigest = NA_real_, sigdf = 3.0, sigquant = 0.90, k = 2.0, power = 2.0, base = 0.95, n.trees = 75L, n.samples = 1500L, n.burn = 1500L, n.chains = 4L, n.threads = min(dbarts::guessNumCores(), n.chains), combineChains = FALSE, n.cuts = 100L, useQuantiles = FALSE, n.thin = 5L, keepTrainingFits = TRUE, printEvery = 100L, printCutoffs = 0L, verbose = TRUE, keepTrees = TRUE, keepCall = TRUE, seed = NA_integer_, keepSampler = keepTrees, keepTestFits = TRUE, callback = NULL, ...) ## S3 method for class 'rbart' plot( x, plquants = c(0.05, 0.95), cols = c('blue', 'black'), ...) ## S3 method for class 'rbart' fitted( object, type = c("ev", "ppd", "bart", "ranef"), sample = c("train", "test"), ...) ## S3 method for class 'rbart' extract( object, type = c("ev", "ppd", "bart", "ranef", "trees"), sample = c("train", "test"), combineChains = TRUE, ...) ## S3 method for class 'rbart' predict( object, newdata, group.by, offset, type = c("ev", "ppd", "bart", "ranef"), combineChains = TRUE, ...) ## S3 method for class 'rbart' residuals(object, ...)
group.by |
Grouping factor. Can be an integer vector/factor, or a reference to such in |
group.by.test |
Grouping factor for test data, of the same type as |
prior |
A function or symbolic reference to built-in priors. Determines the prior over the standard deviation of the random effects. Supplied functions take two arguments, |
n.thin |
The number of tree jumps taken for every stored sample, but also the number of samples from the posterior of the standard deviation of the random effects before one is kept. |
keepTestFits |
Logical where, if false, test fits are obtained while running but not returned. Useful with |
callback |
Optional function of |
formula , data , test , subset , weights , offset , offset.test , sigest , sigdf , sigquant , k , power , base , n.trees , n.samples , n.burn , n.chains , n.threads , combineChains , n.cuts , useQuantiles , keepTrainingFits , printEvery , printCutoffs , verbose , keepTrees , keepCall , seed , keepSampler , ...
|
Same as in |
object |
A fitted |
newdata |
Same as |
type |
One of |
sample |
One of |
x , plquants , cols
|
Same as in |
Fits a BART model with additive random intercepts, one for each factor level of group.by
. For continuous responses:
.
For binary outcomes the response model is changed to .
indexes observations,
is the group index of observation
,
and
come from a BART model, and
are the independent and identically distributed random intercepts. Draws from the posterior of
are made using a slice sampler, with a width dynamically determined by assessing the curvature of the posterior distribution at its mode.
Predicting random effects for groups not in the training sample is supported by sampling from their posterior predictive distribution, that is a draw is taken from . For out-of-sample groups in the test data, these random effect draws can be kept with the saved object. For those supplied to
predict
, they cannot and may change for subsequent calls.
See the generics section of bart
.
An object of class rbart
. Contains all of the same elements of an object of class bart
, as well as the elements:
ranef |
Samples from the posterior of the random effects. A array/matrix of posterior samples. The |
ranef.mean |
Posterior mean of random effects, derived by taking mean across group index of samples. |
tau |
Matrix of posterior samples of |
first.tau |
Burn-in draws of |
callback |
Optional results of |
Vincent Dorie: [email protected]
f <- function(x) { 10 * sin(pi * x[,1] * x[,2]) + 20 * (x[,3] - 0.5)^2 + 10 * x[,4] + 5 * x[,5] } set.seed(99) sigma <- 1.0 n <- 100 x <- matrix(runif(n * 10), n, 10) Ey <- f(x) y <- rnorm(n, Ey, sigma) n.g <- 10 g <- sample(n.g, length(y), replace = TRUE) sigma.b <- 1.5 b <- rnorm(n.g, 0, sigma.b) y <- y + b[g] df <- as.data.frame(x) colnames(df) <- paste0("x_", seq_len(ncol(x))) df$y <- y df$g <- g ## low numbers to reduce run time rbartFit <- rbart_vi(y ~ . - g, df, group.by = g, n.samples = 40L, n.burn = 10L, n.thin = 2L, n.chains = 1L, n.trees = 25L, n.threads = 1L)
f <- function(x) { 10 * sin(pi * x[,1] * x[,2]) + 20 * (x[,3] - 0.5)^2 + 10 * x[,4] + 5 * x[,5] } set.seed(99) sigma <- 1.0 n <- 100 x <- matrix(runif(n * 10), n, 10) Ey <- f(x) y <- rnorm(n, Ey, sigma) n.g <- 10 g <- sample(n.g, length(y), replace = TRUE) sigma.b <- 1.5 b <- rnorm(n.g, 0, sigma.b) y <- y + b[g] df <- as.data.frame(x) colnames(df) <- paste0("x_", seq_len(ncol(x))) df$y <- y df$g <- g ## low numbers to reduce run time rbartFit <- rbart_vi(y ~ . - g, df, group.by = g, n.samples = 40L, n.burn = 10L, n.thin = 2L, n.chains = 1L, n.trees = 25L, n.threads = 1L)
Fits the BART model against varying k
, power
, base
, and ntree
parameters using -fold or repeated random subsampling crossvalidation, sharing burn-in between parameter settings. Results are given an array of evalulations of a loss functions on the held-out sets.
xbart( formula, data, subset, weights, offset, verbose = FALSE, n.samples = 200L, method = c("k-fold", "random subsample"), n.test = c(5, 0.2), n.reps = 40L, n.burn = c(200L, 150L, 50L), loss = c("rmse", "log", "mcr"), n.threads = dbarts::guessNumCores(), n.trees = 75L, k = NULL, power = 2, base = 0.95, drop = TRUE, resid.prior = chisq, control = dbarts::dbartsControl(), sigma = NA_real_, seed = NA_integer_)
xbart( formula, data, subset, weights, offset, verbose = FALSE, n.samples = 200L, method = c("k-fold", "random subsample"), n.test = c(5, 0.2), n.reps = 40L, n.burn = c(200L, 150L, 50L), loss = c("rmse", "log", "mcr"), n.threads = dbarts::guessNumCores(), n.trees = 75L, k = NULL, power = 2, base = 0.95, drop = TRUE, resid.prior = chisq, control = dbarts::dbartsControl(), sigma = NA_real_, seed = NA_integer_)
formula |
An object of class |
data |
An optional data frame, list, or environment containing predictors to be used with the model. For backwards compatibility, can also be the |
subset |
An optional vector specifying a subset of observations to be used in the fitting process. |
weights |
An optional vector of weights to be used in the fitting process. When present, BART fits a model with observations |
offset |
An optional vector specifying an offset from 0 for the relationship between the underyling function, |
verbose |
A logical determining if additional output is printed to the console. |
n.samples |
A positive integer, setting the number of posterior samples drawn for each fit of training data and used by the loss function. |
method |
Character string, either |
n.test |
For each fit, the test sample size or proportion. For method |
n.reps |
A positive integer setting the number of cross validation steps that will be taken. For |
n.burn |
Between one and three positive integers, specifying the 1) initial burn-in, 2) burn-in when moving from one parameter setting to another, and 3) the burn-in between each random subsample replication. The third parameter is also the burn in when moving between folds in |
loss |
Either a one of the pre-set loss functions as character-strings ( |
n.threads |
Across different sets of parameters ( |
n.trees |
A vector of positive integers setting the BART hyperparameter for the number of trees in the sum-of-trees formulation. See |
k |
A vector of positive real numbers, setting the BART hyperparameter for the node-mean prior standard deviation. If |
power |
A vector of real numbers greater than one, setting the BART hyperparameter for the tree prior's growth probability, given by |
base |
A vector of real numbers in |
drop |
Logical, determining if dimensions with a single value are dropped from the result. |
resid.prior |
An expression of the form |
control |
An object inheriting from |
sigma |
A positive numeric estimate of the residual standard deviation. If |
seed |
Optional integer specifying the desired pRNG seed. It should not be needed when running single-threaded - |
Crossvalidates n.reps
replications against the crossproduct of given hyperparameter vectors n.trees
k
power
base
. For each fit, either one fold is withheld as test data and n.test - 1
folds are used as training data or n * n.test
observations are withheld as test data and n * (1 - n.test)
used as training. A replication corresponds to fitting all folds in
"k-fold"
crossvalidation or a single fit with "random subsample"
. The training data is used to fit a model and make predictions on the test data which are used together with the test data itself to evaluate the loss
function.
loss
functions are either the default of average negative log-loss for binary outcomes and root-mean-squared error for continuous outcomes, missclassification rates for binary outcomes, or a function
with arguments y.test
and y.test.hat
. y.test.hat
is of dimensions equal to length(y.test)
n.samples
. A third option is to pass a list of list(function, evaluationEnvironment)
, so as to provide default bindings. RMSE is a monotonic transformation of the average log-loss for continuous outcomes, so specifying log-loss in that case calculates RMSE instead.
An array of dimensions n.reps
length(n.trees)
length(k)
length(power)
length(base)
. If drop
is TRUE
, dimensions of length 1 are omitted. If all hyperparameters are of length 1, then the result will be a vector of length n.reps
. When the result is an array, the dimnames
of the result shall be set to the corresponding hyperparameters.
For method "k-fold"
, each element is an average across the fits. For
"random subsample"
, each element represents a single fit.
Vincent Dorie: [email protected]
f <- function(x) { 10 * sin(pi * x[,1] * x[,2]) + 20 * (x[,3] - 0.5)^2 + 10 * x[,4] + 5 * x[,5] } set.seed(99) sigma <- 1.0 n <- 100 x <- matrix(runif(n * 10), n, 10) Ey <- f(x) y <- rnorm(n, Ey, sigma) mad <- function(y.train, y.train.hat, weights) { # note, weights are ignored mean(abs(y.train - apply(y.train.hat, 1L, mean))) } ## low iteration numbers to to run quickly xval <- xbart(x, y, n.samples = 15L, n.reps = 4L, n.burn = c(10L, 3L, 1L), n.trees = c(5L, 7L), k = c(1, 2, 4), power = c(1.5, 2), base = c(0.75, 0.8, 0.95), n.threads = 1L, loss = mad)
f <- function(x) { 10 * sin(pi * x[,1] * x[,2]) + 20 * (x[,3] - 0.5)^2 + 10 * x[,4] + 5 * x[,5] } set.seed(99) sigma <- 1.0 n <- 100 x <- matrix(runif(n * 10), n, 10) Ey <- f(x) y <- rnorm(n, Ey, sigma) mad <- function(y.train, y.train.hat, weights) { # note, weights are ignored mean(abs(y.train - apply(y.train.hat, 1L, mean))) } ## low iteration numbers to to run quickly xval <- xbart(x, y, n.samples = 15L, n.reps = 4L, n.burn = c(10L, 3L, 1L), n.trees = c(5L, 7L), k = c(1, 2, 4), power = c(1.5, 2), base = c(0.75, 0.8, 0.95), n.threads = 1L, loss = mad)