dbarts icon indicating copy to clipboard operation
dbarts copied to clipboard

Interaction constraints

Open noamross opened this issue 7 years ago • 2 comments

I was wondering if it would be possible to enable interaction constraints to dbarts. These have recently been added to xgboost (link). Having interaction constraints would be very handy for fitting models where some variables are held out from the whole response surface, e.g, y ~ f(x1) + f(x2, x3, x4, x5,...).

noamross avatar Sep 21 '18 19:09 noamross

Technically, yes. It's a bit messy, but it would look something like:

# have to do one chain b/c response object of sampler is shared across all chains
# and we use that to residualize the predictions of the each model
control <- dbartsControl(n.chains = 1, n.burn = 0, n.samples = 1)

sigma.est <- summary(lm(y ~ x1 + x2 + x3 + x4 + x5))$sigma
sampler1 <- dbarts(y ~ x1, control = control, sigma = sigma.est)
sampler2 <- dbarts(y ~ x2 + x3 + x4 + x5, control = control, sigma = sigma.est)

n.samples <- 1000

samp1 <- sampler1$run(updateState = TRUE)
sampler2$setResponse(y - samp1$train)
samp2 <- sampler2$run()
# sampler1 stores its own sigma, but we force it to use the sample from sampler2
sampler1$state[[1]]@sigma <- samp2$sigma[1] / diff(range(y - samp1$train))
sampler1$setState(sampler1$state)

# run the same loop below here for burn-in if you want

totalFits <- matrix(0, length(y), n.samples)
for (i in seq_len(n.samples)) {
  sampler1$setResponse(y - samp2$train)
  samp1 <- sampler1$run()
  sampler2$setResponse(y - samp1$train)
  samp2 <- sampler2$run()
  
  sampler1$state[[1]]@sigma <- samp2$sigma[1] / diff(range(y - samp1$train))
  sampler1$setState(sampler1$state)
  
  totalFits[,i] <- samp1$train + samp2$train
}

vdorie avatar Sep 23 '18 18:09 vdorie

Thanks! I will give this a try.

noamross avatar Sep 24 '18 14:09 noamross