jsonlite icon indicating copy to clipboard operation
jsonlite copied to clipboard

Enabling serialization of prediction models

Open JocaPC opened this issue 8 years ago • 0 comments

When I try to serialize and unserialize a prediction model, I'm getting an error when I try to predict a value:

eruption.lm = lm(eruptions ~ waiting, data=faithful) 
json<-serializeJSON(eruption.lm)
lin.model<-unserializeJSON(json)

newdata = data.frame(waiting=80)
predict(lin.model, newdata)
# Error:
# Error in eval(expr, envir, enclos) : could not find function "list"

Prediction model can be successfully unserialized if I put the following line after unserialize;

attr(lin.model$terms, ".Environment") <- .GlobalEnv

It would be nice if this could work by default. Problem and solution is described here: http://stackoverflow.com/questions/37236806/r-serialize-r-models-as-json/37237530#37237530

JocaPC avatar May 15 '16 21:05 JocaPC