vdorie / dbarts

Discrete Bayesian Additive Regression Trees Sampler
56 stars 20 forks source link

Multiple random effects? #23

Open ignacio82 opened 4 years ago

ignacio82 commented 4 years ago

Is it possible to fit a model with multiple random effects? For example, I took your example and I tried to add another random effect but it did not work:

library(dbarts)

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)

n.j <- 20
j <- sample(n.j, length(y), replace = TRUE)
sigma.c <- 1.8
c <- rnorm(n.g, 0, sigma.c)

y <- y + b[g] + c[j] # I added c 

df <- as.data.frame(x)
colnames(df) <- paste0("x_", seq_len(ncol(x)))
df$y <- y
df$g <- g
df$j <- j

## This does not work
rbartFit <- rbart_vi(y ~ . - g - j, df, group.by = c(g, j),
                     n.samples = 40L, n.burn = 10L, n.thin = 2L, n.chains = 1L,
                     n.trees = 25L, n.threads = 1L)
vdorie commented 4 years ago

Not using rbart unfortunately. If you want the grouping factors to be independent it would be relatively easy to code up, however for random effects that covary (e.g. intercept and slope), a sampling mechanism is needed for their covariance. It's an annoying enough problem to solve that I've postponed tackling it and am working on using Stan to do it. We have a very early implementation for continuous responses here (the name will definitely change). It works, but only if you set the number of thinning iterations for Stan to something really high, which makes it prohibitively slow. It's an area of active development.