# Save Models to Local Disk

#### Shixiang Wang

#### 2025-09-22

    library(bregr)

Two global options have been introduced to control whether models are saved as local files (`bregr.save_model`, default is `FALSE`) and where they should be saved (`bregr.path`, default uses a temporary path).

    options(bregr.save_model = TRUE)
    # Set model save path if necessary
    # options(bregr.path = "/model/to/path")

    m <- breg(mtcars) |>
      br_set_y("mpg") |>
      br_set_x(colnames(mtcars)[2:4]) |>
      br_set_x2("vs") |>
      br_set_model("gaussian") |>
      br_run()
    #> model save is enabled with result path
    #> '/var/folders/h0/s_35svc15n1glmp65lbbkx9w0000gn/T/RtmpH4uFYF'
    #> This message is displayed once per session.

    options(bregr.save_model = FALSE)

In summary, the models have been saved to a unique path. We can verify this by examining the model objects:

    m@models
    #> $cyl
    #> /var/folders/h0/s_35svc15n1glmp65lbbkx9w0000gn/T/RtmpH4uFYF/e5ddc0d4-9795-11f0-9be5-9e303ab0d61c.qs
    #>
    #> $disp
    #> /var/folders/h0/s_35svc15n1glmp65lbbkx9w0000gn/T/RtmpH4uFYF/e5f2292a-9795-11f0-9be5-9e303ab0d61c.qs
    #>
    #> $hp
    #> /var/folders/h0/s_35svc15n1glmp65lbbkx9w0000gn/T/RtmpH4uFYF/e6050446-9795-11f0-9be5-9e303ab0d61c.qs

We can retrieve the saved models using the following commands:

    br_get_models(m, 1)
    #>
    #> Call:  stats::glm(formula = mpg ~ cyl + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)          cyl           vs
    #>     39.6250      -3.0907      -0.9391
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 306   AIC: 171.1
    br_get_models(m, c(1, 3))
    #> $cyl
    #>
    #> Call:  stats::glm(formula = mpg ~ cyl + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)          cyl           vs
    #>     39.6250      -3.0907      -0.9391
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 306   AIC: 171.1
    #>
    #> $hp
    #>
    #> Call:  stats::glm(formula = mpg ~ hp + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)           hp           vs
    #>    26.96300     -0.05453      2.57622
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 422.7     AIC: 181.4
    br_get_models(m, "cyl")
    #>
    #> Call:  stats::glm(formula = mpg ~ cyl + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)          cyl           vs
    #>     39.6250      -3.0907      -0.9391
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 306   AIC: 171.1
    br_get_models(m, c("cyl", "hp"))
    #> $cyl
    #>
    #> Call:  stats::glm(formula = mpg ~ cyl + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)          cyl           vs
    #>     39.6250      -3.0907      -0.9391
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 306   AIC: 171.1
    #>
    #> $hp
    #>
    #> Call:  stats::glm(formula = mpg ~ hp + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)           hp           vs
    #>    26.96300     -0.05453      2.57622
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 422.7     AIC: 181.4
    br_get_models(m)
    #> $cyl
    #>
    #> Call:  stats::glm(formula = mpg ~ cyl + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)          cyl           vs
    #>     39.6250      -3.0907      -0.9391
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 306   AIC: 171.1
    #>
    #> $disp
    #>
    #> Call:  stats::glm(formula = mpg ~ disp + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)         disp           vs
    #>     27.9493      -0.0369       1.4950
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 308.4     AIC: 171.3
    #>
    #> $hp
    #>
    #> Call:  stats::glm(formula = mpg ~ hp + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)           hp           vs
    #>    26.96300     -0.05453      2.57622
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 422.7     AIC: 181.4

The modeling results should remain consistent, regardless of whether they are saved or not.

Run without saving models.

    m2 <- breg(mtcars) |>
      br_set_y("mpg") |>
      br_set_x(colnames(mtcars)[2:4]) |>
      br_set_x2("vs") |>
      br_set_model("gaussian") |>
      br_run()
    m2@models
    #> $cyl
    #>
    #> Call:  stats::glm(formula = mpg ~ cyl + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)          cyl           vs
    #>     39.6250      -3.0907      -0.9391
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 306   AIC: 171.1
    #>
    #> $disp
    #>
    #> Call:  stats::glm(formula = mpg ~ disp + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)         disp           vs
    #>     27.9493      -0.0369       1.4950
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 308.4     AIC: 171.3
    #>
    #> $hp
    #>
    #> Call:  stats::glm(formula = mpg ~ hp + vs, family = stats::gaussian,
    #>     data = data)
    #>
    #> Coefficients:
    #> (Intercept)           hp           vs
    #>    26.96300     -0.05453      2.57622
    #>
    #> Degrees of Freedom: 31 Total (i.e. Null);  29 Residual
    #> Null Deviance:       1126
    #> Residual Deviance: 422.7     AIC: 181.4

    all.equal(m, m2)
    #> [1] TRUE