14.14 Step 8: Save the Model to File

We save the model, together with the dataset and other variables, into a binary R file.

mddate  <- "_" %s+% format(Sys.time(), "%Y%m%d_%H%M%S")
mdrdata <- dsname %s+% "_" %s+% mtype %s+% mddate %s+% ".RData"
save(ds, dsname,
     tr, te, actual_tr, risk_tr, actual_te, risk_te,
     per, con, model, mtype,
     file=mdrdata)

The dataset, its meta-data and the model are now stored in *. We can then load this later and replicate the process.

(load(mdrdata))
##  [1] "ds"        "dsname"    "tr"        "te"        "actual_tr" "risk_tr"  
##  [7] "actual_te" "risk_te"   "per"       "con"       "model"     "mtype"

Note that by using generic variable names we can load different model files and perform common operations on them without changing the names within a script. However, do note that each time we load such a saved model file we overwrite any other variables of the same name.

We have also defined some additional meta-data variables relating to modelling and these will be useful to include in the data file with the dataset, separate to the saved model. We augment the original data file with the modelling meta data and save that to a new file.

mddate  <- "_" %s+% format(Sys.time(), "%Y%m%d_%H%M%S")
mdrdata <- dsname %s+% "_meta" %s+% mddate %s+% ".RData"
save(ds, dsname,
     tr, te, actual_tr, risk_tr, actual_te, risk_te,
     per, con,
     file=mdrdata)

The dataset and its meta-data are now stored on our computer in the current folder in a file with the name *.



Your donation will support ongoing development and give you access to the PDF version of this book. Desktop Survival Guides include Data Science, GNU/Linux, and MLHub. Books available on Amazon include Data Mining with Rattle and Essentials of Data Science. Popular open source software includes rattle, wajig, and mlhub. Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 1995-2021 Graham.Williams@togaware.com Creative Commons Attribution-ShareAlike 4.0.