Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Predictions from serialized dbarts model fits #19

Open
jeremyrcoyle opened this issue Jan 16, 2020 · 1 comment
Open

Predictions from serialized dbarts model fits #19

jeremyrcoyle opened this issue Jan 16, 2020 · 1 comment

Comments

@jeremyrcoyle
Copy link

I'm trying to save and reload dbarts fits. I saw the section in the help explaining the necessary step of touching the state object:

Saving
saveing and loading 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).

However, saving and loading still breaks predictions even with this step. Here's an example:

# example from dbarts help
library(dbarts)
## 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, keeptrees=TRUE)

# generate predictions
preds <- predict(bartFit, x)


# touch the state object
invisible(bartFit$fit$state)

# attempt to save and load
tmp <- tempfile()
save(bartFit, file=tmp)
rm(bartFit)
load(tmp)

# new predictions are NaN
preds_after_save <- predict(bartFit, x)
all(is.nan(preds_after_save))

Are there additional steps necessary for this to work?

@vdorie
Copy link
Owner

vdorie commented Jan 17, 2020

Should be fixed with ead2786. Thanks for letting me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants