tidypredict splits the translation process in two. It first parses the model to extract the needed components to produce the prediction. And second, it uses the object with the parsed information to produce the R formula. Thanks to this two step process, tidypredict does not need to parse the model every time. tidypredict’s functions also accept R objects that contained already models that have been parsed already. Additionally, because the parsed model object is made up of a list made up of basic variables, it is possible to save it in a file. Currently, the best file format is YAML.

For this article, we will use the following model:

model <- lm(mpg ~ (wt + disp) * cyl, data = mtcars)

Parse model

The parse_model() function allows to run the first step manually. It will return an R list object which contains all of the needed information to produce a prediction calculation. The structure of the parsed model varies based on what kind of model is being processed. In general, it is consistent in what kind of information it expects from each model type. For example, in the example the lm() model object will return variables such as sigma2, which would not be used in other model types, such as decision trees.

Usually, we pass an R model object to functions such as: tidypredict_fit(), and tidypredict_sql(). These functions also accept a previously parsed model.

Saving the model

Saving the model is quite easy, use the package such as yaml to write the model object as a YAML file. Any format that can persist a ragged list object should work as well.

library(yaml)

write_yaml(parsed, "my_model.yml")