Strip Linear Model Components
stripLMC.RdStrips linear model ("lm" or "glm") object components
down the bare essentials to reduce size. Used typically
when building thousands of models, e.g. during
cross-validation to manage memory.
Value
A stripped down (size) "lm" or "glm" object.
For "lm" class, removes or sets -> 0 these elements:
- model
- fitted
- assign
- effects
- xlevels
For "glm" class, removes or sets to 0 these elements:
- linear.predictors
- terms
- formula
- family (family, link, linkfun, and linkinv remain)
Examples
fat_lm <- function() {
junk <- runif(1e5)
stats::lm(vs ~ ., data = mtcars)
}
fat_glm <- function() {
junk <- runif(1e5) # excessive junk in envir
stats::glm(vs ~ wt + disp, data = mtcars, family = "binomial")
}
# LM
lobstr::obj_size(fat_lm())
#> 823 kB
lobstr::obj_size(stripLMC(fat_lm()))
#> 17.94 kB
# GLM
lobstr::obj_size(fat_glm())
#> 883.91 kB
lobstr::obj_size(stripLMC(fat_glm()))
#> 37.74 kB