Skip to contents

save_model() writes a fitted 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; for a network its feature scaling and every weight; for a Bayesian model its draws and sampler diagnostics), with the formula terms, factor levels and other settings 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(), and LOO and the posterior predictive checks of a bayes_glm_fit() model, need the original fit.

Usage

save_model(model, file)

load_model(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