Skip to contents

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

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

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

Model fitting

MethodDescription
$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

MethodDescription
$summary()Return posterior summary table.
$diagnostics()Return sampling diagnostics.

Post-processing

MethodDescription
$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

MethodDescription
$save()Save model object to file.

Examples

 # \donttest{
   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"
   )
#> Preprocessing sample data...

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

   # 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)
#> Running MCMC with 2 parallel chains, with 1 thread(s) per chain...
#> 
#> Chain 1 Iteration:   1 / 500 [  0%]  (Warmup) 
#> Chain 2 Iteration:   1 / 500 [  0%]  (Warmup) 
#> Chain 1 Iteration:  50 / 500 [ 10%]  (Warmup) 
#> Chain 2 Iteration:  50 / 500 [ 10%]  (Warmup) 
#> Chain 1 Iteration: 100 / 500 [ 20%]  (Warmup) 
#> Chain 2 Iteration: 100 / 500 [ 20%]  (Warmup) 
#> Chain 1 Iteration: 150 / 500 [ 30%]  (Warmup) 
#> Chain 2 Iteration: 150 / 500 [ 30%]  (Warmup) 
#> Chain 1 Iteration: 200 / 500 [ 40%]  (Warmup) 
#> Chain 2 Iteration: 200 / 500 [ 40%]  (Warmup) 
#> Chain 1 Iteration: 250 / 500 [ 50%]  (Warmup) 
#> Chain 1 Iteration: 251 / 500 [ 50%]  (Sampling) 
#> Chain 1 Iteration: 300 / 500 [ 60%]  (Sampling) 
#> Chain 2 Iteration: 250 / 500 [ 50%]  (Warmup) 
#> Chain 2 Iteration: 251 / 500 [ 50%]  (Sampling) 
#> Chain 1 Iteration: 350 / 500 [ 70%]  (Sampling) 
#> Chain 2 Iteration: 300 / 500 [ 60%]  (Sampling) 
#> Chain 1 Iteration: 400 / 500 [ 80%]  (Sampling) 
#> Chain 2 Iteration: 350 / 500 [ 70%]  (Sampling) 
#> Chain 1 Iteration: 450 / 500 [ 90%]  (Sampling) 
#> Chain 2 Iteration: 400 / 500 [ 80%]  (Sampling) 
#> Chain 1 Iteration: 500 / 500 [100%]  (Sampling) 
#> Chain 1 finished in 1.8 seconds.
#> Chain 2 Iteration: 450 / 500 [ 90%]  (Sampling) 
#> Chain 2 Iteration: 500 / 500 [100%]  (Sampling) 
#> Chain 2 finished in 2.0 seconds.
#> 
#> Both chains finished successfully.
#> Mean chain execution time: 1.9 seconds.
#> Total execution time: 2.2 seconds.
#> 
#> Warning: 4 of 500 (1.0%) transitions ended with a divergence.
#> See https://mc-stan.org/misc/warnings for details.

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

   # Sampling diagnostics
   model_diagnostics <- model$diagnostics()
 # }