MRPModel: MRPModel objects

MRPModelR Documentation

MRPModel objects

Description

An MRPModel object is an R6 object created by the $create_model() method of an MRPWorkflow object. Each MRPModel object represents a multilevel regression model, providing methods for sampling, diagnostics, and poststratification.

Creates a new instance of the MRPModel class. This method is called by the ⁠$create_model()⁠ method of an MRPWorkflow object and does not need to be called directly by users.

Arguments

model_spec

List containing model effects specification, including intercept, fixed effects, varying effects, and interactions

mrp_data

List containing the MRP data structure with input sample data and new poststratification data

metadata

List containing metadata about the analysis, including family, time variables, and special cases

link_data

List containing information about data linking, including geography and ACS year

plot_data

List containing data prepared for visualization, including dates and GeoJSON objects

extra

List containing COVID test sensitivity and specificity

Value

A new MRPModel object initialized with the provided model specification and relevant data.

Methods

MRPModel objects have the following associated methods, many of which have their own (linked) documentation pages:

Data access

Method Description
$model_spec() Return model specification.
$formula() Return model formula.
$metadata() Return model metadata.
$stan_code() Return model Stan code.

Model fitting

Method Description
$fit() Fit multilevel regression model using CmdStanR.
$check_fit_exists() Check if model has been fitted.
$check_estimate_exists() Check if poststratification has been performed.

Posterior summary & diagnostics

Method Description
$summary() Return posterior summary table.
$diagnostics() Return sampling diagnostics.

Post-processing

Method Description
$ppc() Create input for posterior predictive check.
$log_lik() Create input for leave-one-out cross-validation.
$poststratify() Run poststratification to generate population estimates.

Saving model object

Method Description
$save() Save model object to file.

Examples


 
   library(shinymrp)

   # Initialize workflow
   workflow <- mrp_workflow()

   # Load example data
   sample_data <- example_sample_data()

   # Preprocess sample data
   workflow$preprocess(
     sample_data,
     is_timevar = TRUE,
     is_aggregated = TRUE,
     special_case = NULL,
     family = "binomial"
   )

   # Link to ACS data at ZIP code level
   workflow$link_acs(
     link_geo = "zip",
     acs_year = 2021
   )

   # Create and fit multiple models
   model <- workflow$create_model(
     intercept_prior = "normal(0, 4)",
     fixed = list(
       sex = "normal(0, 2)"
     ),
     varying = list(
       race = "normal(0, 2)",
       age = "normal(0, 2)",
       time = "normal(0, 2)"
     )
   )

   # Run MCMC
   model$fit(n_iter = 500, n_chains = 2, seed = 123)

   # Estimates summary and diagnostics
   posterior_summary <- model$summary()

   # Sampling diagnostics
   model_diagnostics <- model$diagnostics()
 


shinymrp documentation built on Dec. 4, 2025, 5:07 p.m.