When it comes to serializing XGBoost models, it's possible to use R serializers such as
save() or saveRDS() to serialize an XGBoost model object, but XGBoost also provides
its own serializers with better compatibility guarantees, which allow loading
said models in other language bindings of XGBoost.
Note that an xgb.Booster object (as produced by xgb.train(), see rest of the doc
for objects produced by xgboost()), outside of its core components, might also keep:
Additional model configuration (accessible through xgb.config()), which includes
model fitting parameters like max_depth and runtime parameters like nthread.
These are not necessarily useful for prediction/importance/plotting.
Additional R specific attributes - e.g. results of callbacks, such as evaluation logs,
which are kept as a data.table object, accessible through
attributes(model)$evaluation_log if present.
The first one (configurations) does not have the same compatibility guarantees as
the model itself, including attributes that are set and accessed through
xgb.attributes() - that is, such configuration might be lost after loading the
booster in a different XGBoost version, regardless of the serializer that was used.
These are saved when using saveRDS(), but will be discarded if loaded into an
incompatible XGBoost version. They are not saved when using XGBoost's
serializers from its public interface including xgb.save() and xgb.save.raw().
The second ones (R attributes) are not part of the standard XGBoost model structure,
and thus are not saved when using XGBoost's own serializers. These attributes are
only used for informational purposes, such as keeping track of evaluation metrics as
the model was fit, or saving the R call that produced the model, but are otherwise
not used for prediction / importance / plotting / etc.
These R attributes are only preserved when using R's serializers.
In addition to the regular xgb.Booster objects produced by xgb.train(), the
function xgboost() produces objects with a different subclass xgboost (which
inherits from xgb.Booster), which keeps other additional metadata as R attributes
such as class names in classification problems, and which has a dedicated predict
method that uses different defaults and takes different argument names. XGBoost's
own serializers can work with this xgboost class, but as they do not keep R
attributes, the resulting object, when deserialized, is downcasted to the regular
xgb.Booster class (i.e. it loses the metadata, and the resulting object will use
predict.xgb.Booster() instead of predict.xgboost()) - for these xgboost objects,
saveRDS might thus be a better option if the extra functionalities are needed.
Note that XGBoost models in R starting from version 2.1.0 and onwards, and
XGBoost models before version 2.1.0; have a very different R object structure and
are incompatible with each other. Hence, models that were saved with R serializers
like saveRDS() or save() before version 2.1.0 will not work with latter
xgboost versions and vice versa. Be aware that the structure of R model objects
could in theory change again in the future, so XGBoost's serializers should be
preferred for long-term storage.
Furthermore, note that model objects from XGBoost might not be serializable with third-party
R packages like qs or qs2.