# rbart: Bayesian Additive Regression Trees with Random Effects In vdorie/dbarts: Discrete Bayesian Additive Regression Trees Sampler

## Description

Fits a varying intercept/random effect BART model.

For numeric response y_i = f(x_i) + α_{j[i]} + ε_i, where ε_i ~ N(0, σ_y^2) and α_j ~ N(0, σ_α^2).

## Usage

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19``` ```rbart_vi( formula, data, test, subset, weights, offset, offset.test = offset, group.by, 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(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, ...) ## S3 method for class 'rbart' plot(x, plquants = c(0.05, 0.95), cols = c('blue', 'black'), ...) ## S3 method for class 'rbart' predict(object, test, group.by, offset.test, combineChains, ...) ```

## Arguments

 `group.by` Grouping factor. Can be an integer vector/factor, or a reference to such in `data`. `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, `x` - the standard deviation, and `rel.scale` - the standard deviation of the response variable before random effects are fit. Built in priors are `cauchy` with a scale of 2.5 times the relative scale and `gamma` with a shape of 2.5 and scale of 2.5 times the relative scale. `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. ```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, ...``` Same as in `bart2`. `object` Same as in `predict.bart`. `x, plquants, cols` Same as in `plot.bart`.

## Details

Fits a BART model with additive random intercepts, one for each factor level of `group.by`. That is

• y_i = b_g[i] + f(x_i) + ε,

• b_j ~ N(0, τ^2).

where i indices observations, g[i] is the group index of observation i, f(x) and ε come from a BART model, and b_j are the independent and identically distributed random intercepts.

## Value

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 (i, j, k) value is the jth draw of the posterior of the random effect for group k (i.e. b*__k) corresponding to chain i. When `nchain` is one or `combineChains` is `TRUE`, the result is a collapsed down to a matrix. `ranef.mean` Posterior mean of random effects, derived by taking mean across group index of samples. `tau` Matrix of posterior samples of `tau`, the standard deviation of the random effects. Dimensions are equal to the number of chains times the numbers of samples unless `nchain` is one or `combineChains` is `TRUE`. `first.tau` Burn-in draws of `tau`.

## Author(s)

Vincent Dorie: [email protected]

`bart`, `dbarts`
 ``` 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``` ```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) ```