Skip to contents

save_model() writes a glm_fit(), gam_fit() or elastic_net_fit() model to an RDS file: the Rust fit as a versioned JSON artifact (spec, estimates, covariance, fit statistics, fitted values, and provenance with the package version and a hash of the training data; for a GAM also the spline knots and constraints; for an elastic net one artifact per lambda), with the formula terms and factor levels that stats::predict() needs. load_model() reads it back; the estimates round-trip exactly. A loaded model predicts and simulates but has no training data, so robust_vcov() needs the original fit.

Usage

save_model(model, file)

load_model(file)

Arguments

model

A glm_model, gam_model or elastic_net_model.

file

Path of the RDS file.

Value

save_model(): file, invisibly. load_model(): a model of the class saved.

Examples

d <- data.frame(claims = c(1, 2, 4, 2, 1, 3), region = c("N", "N", "S", "S", "W", "W"))
m <- glm_fit(claims ~ region, d, family = "poisson")
f <- tempfile(fileext = ".rds")
save_model(m, f)
m2 <- load_model(f)
identical(coef(m2), coef(m))
#> [1] TRUE