Skip to contents

rpf_marshal() converts an rpf object into a plain R list safe for saveRDS(); rpf_unmarshal() reverses it. The C++ forest behind an rpf object does not survive R serialization, so this explicit round-trip is required to store or transfer fitted models.

Usage

rpf_marshal(x, include_data = FALSE)

rpf_unmarshal(blob)

Arguments

x

[rpf | rpf_pruned]: A fitted rpf model, or a pruned one from prune_components().

include_data

[logical(1): FALSE]: Store training data in the blob, enabling purify() after restoring.

blob

[rpf_marshaled | rpf_pruned_marshaled]: Object created by rpf_marshal().

Value

rpf_marshal() returns a list of class rpf_marshaled (rpf_pruned_marshaled for a pruned forest); rpf_unmarshal() returns an object of class rpf (rpf_pruned).

Details

Training data is only included with include_data = TRUE; without it, a restored forest can predict (including purified prediction if the forest was purified before marshaling) but can never be purified afterwards. With it, the in-bag counts of the bootstrap samples are stored too, so that prune_components() can choose out of bag after restoring.

A pruned forest from prune_components() or rpf() (class rpf_pruned) is marshaled the same way: its forest as above, and the pruning (rule, coefficients, components, the table of the choice, the stored kept components, the display's coarsened components) as it is. The restored model predicts as before, from its stored components, so it needs no training data for that. A pruned model without its forest (the default of rpf() and prune_components(), keep_forest = FALSE) is plain R data: saveRDS() stores it as it is, and its blob is the model without the caches of its built indices (include_data has nothing to include).

See also

rpf_is_valid(); bundle() for the same through the bundle package.

Examples

fit <- rpf(mpg ~ wt + cyl, data = mtcars, n_families = 10, prune = FALSE)
blob <- rpf_marshal(fit)
tmp <- tempfile(fileext = ".rds")
saveRDS(blob, tmp)
restored <- rpf_unmarshal(readRDS(tmp))
all.equal(predict(fit, mtcars), predict(restored, mtcars))
#> [1] TRUE
unlink(tmp)