I'm working in R 3.6 within a third-party environment (IBM Watson Studio) and need to be able to convert a model object into a raw binary or string buffer in order to save the model as indicated here. Briefly, I have to use a function that makes an API request that sends over the raw binary/string buffer data in order to save files. Similar to this question, I opted for converting the model into JSON using jsonlite::serializeJSON()
(and then to raw). It seemed to work, but similar to the person in that post, I run into the issue of serializeJSON()
not being able to convert complex lists in the model object appropriately. When I attempt to predict, it hits the "could not find function "list"" error. Since I have a GLM, even after implementing the suggested fix, I now encounter the error "object 'C_logit_linkinv' not found". Clearly, this method will not be very generalizable to varying models.
I'm posting as a new question because none of the solutions work in my case, and because I'd like to see if there's perhaps a more robust way of converting model objects to raw bytes or string buffers. I'm not posting to IBM boards because I felt this was a more general issue about model objects and conversion, but will do so if community feels that's more appropriate. Here's a reproducible example:
require(jsonlite)
# Generate some data
data <- data.frame(x1=rnorm(100),
y=rbinom(100,1,.6))
# Fit and convert model->JSON->Raw
fitted_model <- glm(y ~ 0 + .,
data = data,
family = binomial)
model_as_json <- jsonlite::serializeJSON(fitted_model)
model_as_raw <- charToRaw(model_as_json)
# Convert back to model
back_to_json <- rawToChar(model_as_raw)
back_to_model <- jsonlite::unserializeJSON(back_to_json)
# Score
scoring_data <- data.frame(x1=rnorm(5))
predict(object=back_to_model,
newdata = scoring_data,
type='response')
Specs:
- R version 3.6.1 (2019-07-05)
- Platform: x86_64-conda_cos6-linux-gnu (64-bit)
- Running under: Red Hat Enterprise Linux 8.2 (Ootpa)
- jsonlite_1.7.1