Displaying 3 results from an estimated 3 matches for "normal_lm".
2020 Jan 29
2
Model object, when generated in a function, saves entire environment when saved
...;- function (object) {
tf <- tempfile(fileext = ".qs")
on.exit(unlink(tf))
qs::qsave(object, file = tf)
file.size(tf)
}
save_size_rds <- function (object) {
tf <- tempfile(fileext = ".rds")
on.exit(unlink(tf))
saveRDS(object, file = tf)
file.size(tf)
}
normal_lm <- function(){
junk <- 1:1e+08
lm(Sepal.Length ~ Sepal.Width, data = iris)
}
normal_ggplot <- function(){
junk <- 1:1e+08
ggplot2::ggplot()
}
clean_lm <- function () {
junk <- 1:1e+08
# Run the lm in its own environment
env <- new.env(parent = globalenv())
env...
2020 Jan 29
0
Model object, when generated in a function, saves entire environment when saved
...f that while qs::qsave doesn't. That's not a
very useful test, because environments typically aren't filled with long
sequence vectors. If you replace the line
junk <- 1:1e+08
with
junk <- runif(1e+08)
you'll see drastically different results:
> save_size_qs(normal_lm())
[1] 417953609
> #> [1] 848396
> save_size_rds(normal_lm())
[1] 532614827
> #> [1] 4163
> save_size_qs(normal_ggplot())
[1] 417967987
> #> [1] 857446
> save_size_rds(normal_ggplot())
[1] 532624477
> #> [1] 12895
Duncan Murdoch
2016 Jul 27
2
Model object, when generated in a function, saves entire environment when saved
Another solution is to only save the parts of the model object that
interest you. As long as they don't include the formula (which is
what drags along the environment it was created in), you will
save space. E.g.,
tfun2 <- function(subset) {
junk <- 1:1e6
list(subset=subset, lm(Sepal.Length ~ Sepal.Width, data=iris,
subset=subset)$coef)
}
saveSize(tfun2(1:4))
#[1] 152
Bill