Skip to contents

While the BARTSerialization and BCFSerialization topics focus on JSON serialization / deserialization for entire bartmodel and bcfmodel objects, this function group provides an interface for a more focused use case: loading a single RandomEffectSamples container from a broader BART / BCF model (which may include forests and other parametric terms).

loadRandomEffectSamplesJson converts a CppJson object representing a BART or BCF model into a RandomEffectSamples container by extracting the JSON indexed by an integer label (i.e. 0) and deserializing it into a RandomEffectSamples object.

Both loadRandomEffectSamplesJson and loadRandomEffectSamplesCombinedJson operate similarly, but on a list of CppJson or JSON string representations of BART / BCF models with the same structure.

These functions are intended for advanced use cases in which users require detailed control of sampling algorithms and data structures. Minimal input validation and error checks are performed – users are responsible for providing the correct inputs. For tutorials on the "proper" usage of the stochtree's advanced workflow, we provide several vignettes at https://stochtree.ai/

Usage

loadRandomEffectSamplesJson(json_object, json_rfx_num)

loadRandomEffectSamplesCombinedJson(json_object_list, json_rfx_num)

loadRandomEffectSamplesCombinedJsonString(json_string_list, json_rfx_num)

Arguments

json_object

Object of class CppJson

json_rfx_num

Integer index indicating the position of the random effects term to be unpacked (must exist in every json object in a list if a list is provided)

json_object_list

List of objects of class CppJson

json_string_list

List of objects of class CppJson

Value

Each of the functions in this group returns a RandomEffectSamples object.

Examples

n <- 100
p <- 10
X <- matrix(runif(n*p), ncol = p)
rfx_group_ids <- sample(1:2, size = n, replace = TRUE)
rfx_basis <- rep(1.0, n)
y <- (-5 + 10*(X[,1] > 0.5)) + (-2*(rfx_group_ids==1)+2*(rfx_group_ids==2))
bart_model <- bart(X_train=X, y_train=y, rfx_group_ids_train=rfx_group_ids,
                   rfx_basis_train = rfx_basis, num_gfr=0, num_mcmc=10)
bart_json <- saveBARTModelToJson(bart_model)
bart_json_string <- saveBARTModelToJsonString(bart_model)
bart_json_list <- list(bart_json)
bart_json_string_list <- list(bart_json_string)
rfx_container <- loadRandomEffectSamplesJson(bart_json, 0)
rfx_container <- loadRandomEffectSamplesCombinedJson(bart_json_list, 0)
rfx_container <- loadRandomEffectSamplesCombinedJsonString(bart_json_string_list, 0)