Is there a way to 'compress' an object of class lm, so that I can save it to the disk and load it up later for use with predict.lm?
I have an lm object that ends up being ~142mb upon saving, and I have a hard time believing that predict.lm needs all of the original observations / fitted values / residuals etc. to make a linear prediction. Can I remove information so that the saved model is smaller?
I have tried setting some of the variables (fitted.values, residuals, etc.) to NA, but it seems to have no effect on the saved file size.
You can use
biglm
to fit your models, abiglm
model object is smaller than a lm model object. You can usepredict.biglm
create a function that you can pass the newdata design matrix to, which returns the predicted values.Another option is to use
saveRDS
to save the files, which appear to be slightly smaller, as they have less overhead, being a single object, not like save which can save multiple objects.Turns out I solved my own problem. Using the following:
reduced the size of my model substantially.
A couple of things:
This question really is a duplicate.
In the narrow sense
model=FALSE
as was already answered in another question.In a wider sense,
predict(fit, newdata)
really just does a matrix-vector multiplication so you could save just the vector of predictions and multiply it with a matrix.There are alternate fitting functions. Below is an example from
fastLm()
in RcppArmadillo which also happens to be faster.See below for an illustration.