Description
In the R version of stochtree to save and reload a BCF model, when using saveBCFModelToJsonString() and then createBCFModelFromJsonString(), I've found that 1) printing the object results in an error, as does attempting to use predict() on the loaded model. It appears that certain required parameters are not being reloaded by createBCFModelFromJsonString() (but are in the json string) - when I add them manually, everything works as intended. There may be other important parameters that aren't loaded as well, but my code caught these.
Reproducing
Given an object bcf_fit:
json_string_to_export <- saveBCFModelToJsonString(bcf_fit)
# Save string to file location, then read that string as string_bcf (omitted code here)
reloaded_bcf_model <- createBCFModelFromJsonString(string_bcf)
Error 1:
reloaded_bcf_model
ERROR while rich displaying an object: Error in if (x$model_params$binary_treatment) {: argument is of length zero
Traceback:
1. sapply(x, f, simplify = simplify)
2. lapply(X = X, FUN = FUN, ...)
3. FUN(X[[i]], ...)
4. tryCatch(withCallingHandlers({
. if (!mime %in% names(repr::mime2repr))
. stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
. rpr <- repr::mime2repr[[mime]](obj)
. if (is.null(rpr))
. return(NULL)
. prepare_content(is.raw(rpr), rpr)
. }, error = error_handler), error = outer_handler)
5. tryCatchList(expr, classes, parentenv, handlers)
6. tryCatchOne(expr, names, parentenv, handlers[[1L]])
7. doTryCatch(return(expr), name, parentenv, handler)
8. withCallingHandlers({
. if (!mime %in% names(repr::mime2repr))
. stop("No repr_* for mimetype ", mime, " in repr::mime2repr")
. rpr <- repr::mime2repr[[mime]](obj)
. if (is.null(rpr))
. return(NULL)
. prepare_content(is.raw(rpr), rpr)
. }, error = error_handler)
9. repr::mime2repr[[mime]](obj)
10. repr_text.default(obj)
11. paste(capture.output(print(obj)), collapse = "\n")
12. capture.output(print(obj))
13. withVisible(...elt(i))
14. print(obj)
15. print.bcfmodel(obj)
Error 2:
predict(reloaded_bcf_model, data, treatment)
Also results in an error (don't have the text at the moment).
The following code fixes the issue:
reloaded_bcf_model$model_params$binary_treatment <- TRUE
reloaded_bcf_model$bart_propensity_model$fit$numeric_vars <- vars_I_used
reloaded_bcf_model$bart_propensity_model$train_set_metadata$numeric_vars <- vars_I_used
Version: 0.4.1
Description
In the R version of stochtree to save and reload a BCF model, when using saveBCFModelToJsonString() and then createBCFModelFromJsonString(), I've found that 1) printing the object results in an error, as does attempting to use predict() on the loaded model. It appears that certain required parameters are not being reloaded by createBCFModelFromJsonString() (but are in the json string) - when I add them manually, everything works as intended. There may be other important parameters that aren't loaded as well, but my code caught these.
Reproducing
Given an object
bcf_fit:Error 1:
Error 2:
Also results in an error (don't have the text at the moment).
The following code fixes the issue:
Version: 0.4.1