# Save Models to Local Disk

#### Shixiang Wang

#### 2025-12-17

    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/1w/371ktd8j0fv8mp_dsgtvssnw0000gn/T/RtmphTFzDK'
    #> 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/1w/371ktd8j0fv8mp_dsgtvssnw0000gn/T/RtmphTFzDK/6c055ec2-db54-11f0-8dff-06344c000d01.qs
    #>
    #> $disp
    #> /var/folders/1w/371ktd8j0fv8mp_dsgtvssnw0000gn/T/RtmphTFzDK/6c1b67c6-db54-11f0-8dff-06344c000d01.qs
    #>
    #> $hp
    #> /var/folders/1w/371ktd8j0fv8mp_dsgtvssnw0000gn/T/RtmphTFzDK/6c2c0f22-db54-11f0-8dff-06344c000d01.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