How to save glm result without data or only with coeffients for prediction?

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:28:07

Setting model = FALSE in your call to glm should prevent the model.frame from being returned. Also setting y = FALSE will prevent the response vector from being returned. x = FALSE is the default setting and prevents the model.matrix from being returned.

This combination should shrink the size of your glm object.

Of course, you can also extract the coefficients with coef(model_glm) or, with standard errors,

summary(model_glm)$coef
Levi Bowles

I had this issue where I was running the GLM as part of an R in production and the size of the GLM greatly slowed me down. I found I needed to kill off more than just the $data. Here is my post on it, with an example below.

> object.size(sg)
96499472 bytes
> sg$residuals <- NULL
> sg$weights <- NULL
> sg$fitted.values <- NULL
> sg$prior.weights <- NULL
> sg$na.action<- NULL
> sg$linear.predictors <- NULL
> sg$fitted.values <- NULL
> sg$effects <-NULL
> sg$data <- NULL
> object.size(sg)
3483976 bytes
> sg$qr$qr <- NULL
> object.size(sg)
79736 bytes

object.size() is misleading because it ignores the environment attributes. If you want to assess the true size, use:

length(serialize(model_glm, NULL))

Apart from the data stored, if you want to significantly reduce the size of your glm do:

rm(list=ls(envir = attr(model_glm$terms, ".Environment")),
     envir = attr(model_glm$terms,
              ".Environment"))

This comes from a well detailed article

Anon

You can NULL the data in the model object before saving it. I did a quick test and still generated predictions.

model_glm$data <- NULL
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!