Found a similar question here, but it is not full.
My question is split in 2 :
- I want to store a "slim" version of an R lm() object as text in a DBMS.
- I want to be able to produce predictions out of the text object I saved.
By "slim" I mean with just the right amount of data that the predict() function won't fail. I want to store the model becuase learning sometimes takes a lot of time, for example :
lmFull <- lm(Volume~Girth+Height,data=trees)
lmSlim <- lmFull
lmSlim$fitted.values <- lmSlim$qr$qr <- lmSlim$residuals <- lmSlim$model <- lmSlim$effects <- NULL
pred1 <- predict(lmFull,newdata=data.frame(Girth=c(1,2,3),Height=c(2,3,4)))
pred2 <- predict(lmSlim,newdata=data.frame(Girth=c(1,2,3),Height=c(2,3,4)))
identical(pred1,pred2)
[1] True
What I have done to store as text is take the lmSlim object and deparse it :
lmTxt <- deparse(lmSlim)
lmTxt <- paste0(lmTxt,collapse="")
Storing this in the the DB is easy, but when I want to reuse it again :
lmRst <- eval(parse(text=lmTxt))
class(lmRst)
[1] "lm"
predict(lmRst,newdata=data.frame(Girth=c(1,2,3),Height=c(2,3,4)))
Error in eval(expr, envir, enclos) : object 'Volume' not found
Any suggestions?