我正在使用 rstanarm 在函数中拟合 stan_glm 模型。遇到一个问题,即保存的 stanfit 对象的大小在保存到 .rds 时会爆炸,但仅当模型适合函数时。问题似乎是 stanfit 对象正在存储本地环境的副本,然后使用 write_rds 将其保存到磁盘?手动删除函数内的大对象或多或少地解决了这个问题,但这是一个相当笨拙的解决方案,所以想知道是否有人建议以更优雅的方式解决这个问题?下面的玩具代表(警告这会将一些 .rds 文件写入磁盘,我在示例结束时将它们删除但请注意)
library(readr)
library(rstanarm)
#> Loading required package: Rcpp
#> rstanarm (Version 2.19.3, packaged: 2020-02-11 05:16:41 UTC)
#> - Do not expect the default priors to remain the same in future rstanarm versions.
#> Thus, R scripts should specify priors explicitly, even if they are just the defaults.
#> - For execution on a local, multicore CPU with excess RAM we recommend calling
#> options(mc.cores = parallel::detectCores())
#> - bayesplot theme set to bayesplot::theme_default()
#> * Does _not_ affect other ggplot2 plots
#> * See ?bayesplot_theme_set for details on theme setting
library(gapminder)
# create a largeish object
test <- matrix(data = rnorm(10000), nrow = 10000/2, ncol = 10000/2)
# fit model in the global environment
a = stan_glm(lifeExp ~ gdpPercap, data = gapminder, refresh =0)
print(object.size(a), unit = "Mb")
#> 1.4 Mb
# fit model inside function , passing but not using largeish object
memfoo <- function(gap, testy, clean = FALSE){
d <- testy
if (clean){
rm(d,testy)
}
a <- stan_glm(lifeExp ~ gdpPercap, data = gap, refresh = 0)
}
b <- memfoo(gapminder, test)
# fit model again, but removing large obects from the environment before running
d <- memfoo(gapminder, test, clean = TRUE)
print(object.size(a), unit = "Mb")
#> 1.4 Mb
print(object.size(b), unit = "Mb")
#> 1.4 Mb
print(object.size(d), unit = "Mb")
#> 1.4 Mb
# all same size in memory
# write to .rds
write_rds(a,"a.rds")
write_rds(b,"b.rds")
write_rds(d,"d.rds")
# rstan object run in function with largeish object in environment is 45 times bigger than same regression
# fit outside function!
file.size("a.rds")
#> [1] 9026456
file.size("b.rds")
#> [1] 410011317
file.size("d.rds")
#> [1] 10011197
file.size("b.rds") / file.size("a.rds")
#> [1] 45.42329
file.remove(c("a.rds", "b.rds", "d.rds"))
#> [1] TRUE TRUE TRUE
由reprex 包于 2020-03-11 创建(v0.3.0)