Learn R Programming

shinymrp (version 0.10.0)

MRPWorkflow: MRPWorkflow objects

Description

A MRPWorkflow object is an R6 object created by the mrp_workflow() function. This class provides methods for all steps in the workflow, from data preparation and visualization to model fitting.

Arguments

Methods

MRPWorkflow objects have the following associated methods with their own (linked) documentation pages:

Data preparation

MethodDescription
$preprocess()Preprocess sample data.
$preprocessed_data()Return preprocessed sample data.
$link_acs()Link sample data to ACS data.
$load_pstrat()Load custom poststratification data.

Model fitting & diagnostics

MethodDescription
$create_model()Create a MRPModel object.
$pp_check()Perform posterior predictive check.
$compare_models()Compare models using LOO-CV.

Visualization

MethodDescription
$demo_bars()Create demographic comparison bar plots.
$covar_hist()Create geographic covariate distribution histograms.
$sample_size_map()Create sample size map.
$outcome_plot()Create summary plots of raw outcome measure.
$outcome_map()Visualize raw outcome measure by geography.
$estimate_plot()Visualize estimates for demographic groups.
$estimate_map()Visualize estimates for geographic areas.

Examples

Run this code
if (FALSE) { # requireNamespace("cmdstanr", quietly = TRUE)
 # \donttest{
   library(shinymrp)

   # Initialize the MRP workflow
   workflow <- mrp_workflow()

   # Load example data
   sample_data <- example_sample_data()

   ### DATA PREPARATION

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

   # Link data to the ACS
   # and obtain poststratification data
   workflow$link_acs(
     link_geo = "zip",
     acs_year = 2021
   )

   ### DESCRIPTIVE STATISTICS

   # Visualize demographic distribution of data
   sex_bars <- workflow$demo_bars(demo = "sex")

   # Visualize geographic distribution of data
   ss_map <- workflow$sample_size_map()

   # Visualize outcome measure
   raw_outcome_plot <- workflow$outcome_plot()

   ### MODEL BUILDING

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

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

   # Estimates summary and diagnostics
   model$summary()

   # Sampling diagnostics
   model$diagnostics()

   # Posterior predictive check
   workflow$pp_check(model)

   ### VISUALIZE RESULTS

   # Plots of overall estimates, estimates for demographic groups, and geographic areas
   workflow$estimate_plot(model, group = "sex")

   # Choropleth map of estimates for geographic areas
   workflow$estimate_map(model, geo = "county")
 # }
}

Run the code above in your browser using DataLab