-1
$\begingroup$

I am creating a bunch of randomForests through a call, and plan to use these objects in a later analysis. How can I save these objects in a way that I can use and get their parameters later?

Essentially, I would like to do something like the following, in a loop:

fit <- randomForest(...)

array[n] = fit

I am aware of the save() functionality, but that doesn't quite work here because I have many forests, the number of forests is not necessarily known, and I cannot name/save/load each individual one.

$\endgroup$
0

1 Answer 1

2
$\begingroup$

You can use saveRDS() in lapply. If you have multiple objects you want to save, put them all in a list, like:

rfs <- list(rf1, rf2, rf3, rf4)

Where each rf is a model.

Then name each part of the list as the file name you want:

names(rfs) <- c("model1", "model2", "model3", "model4")

Then use lapply to save them. Something like:

lapply(seq_along(rfs), function(x) saveRDS(rfs[[x]], paste0(names(rfs)[[x]], ".rds")))
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.