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 u
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.
R> library(RcppArmadillo)
Loading required package: Rcpp
R> flm <- fastLm(Volume ~ Girth, data=trees)
R> predict(flm, newdata=trees[1:5,]) ## can predict as with lm()
[1] 5.10315 6.62291 7.63608 16.24803 17.26120
R> object.size(flm) ## tiny object size ...
3608 bytes
R> stdlm <- lm(Volume ~ Girth, data=trees)
R> object.size(stdlm) ## ... compared to what lm() has
20264 bytes
R> stdlm <- lm(Volume ~ Girth, data=trees, model=FALSE)
R> object.size(stdlm) ## ... even when model=FALSE
15424 bytes
R>