### Launch ShinyStan Demo Examples Source: https://mc-stan.org/shinystan/reference/launch_shinystan_demo.html Examples showing how to launch the demo with or without saving the resulting S4 shinystan object. ```R if (FALSE) { # \dontrun{ # launch demo but don't save a shinystan object launch_shinystan_demo() # launch demo and save the shinystan object for the demo sso_demo <- launch_shinystan_demo() } # } ``` -------------------------------- ### Example Deployment Usage Source: https://mc-stan.org/shinystan/reference/deploy_shinystan.html Demonstrates how to deploy a shinystan object with and without specifying the account name. ```R if (FALSE) { # \dontrun{ # For this example assume sso is the name of the \code{shinystan} object for # the model you want to use. Assume also that you want to name your app # 'my-model' and that your shinyapps.io username is 'username'. deploy_shinystan(sso, appName = "my-model", account = "username") # If you only have one ShinyApps account configured then you can also omit # the 'account' argument. deploy_shinystan(sso, appName = "my-model") } # } ``` -------------------------------- ### Install ShinyStan Development Version from GitHub Source: https://mc-stan.org/shinystan/index.html Installs the development version of the shinystan package from GitHub. Requires the devtools package to be installed first. Builds vignettes during installation. ```r if (!require("devtools")) { install.packages("devtools") } devtools::install_github("stan-dev/shinystan", build_vignettes = TRUE) ``` -------------------------------- ### Launch ShinyStan Demo Source: https://mc-stan.org/shinystan/index.html Loads the shinystan library and launches an interactive demo of the ShinyStan application. This is useful for exploring its features after installation. ```r library("shinystan") launch_shinystan_demo() ``` -------------------------------- ### Launch ShinyStan with a stanfit object Source: https://mc-stan.org/shinystan/reference/launch_shinystan.html Launches the ShinyStan app with a stanfit object. The resulting shinystan object can then be used with the examples from Example 1. ```R # Just launch shinystan launch_shinystan(sf) # Launch shinystan and save the resulting shinystan object sf_sso <- launch_shinystan(sf) ``` -------------------------------- ### Example usage of shinystan metadata functions Source: https://mc-stan.org/shinystan/reference/shinystan-metadata.html Demonstrates how to view and update model code, notes, and names for a shinystan object. ```R # use eight_schools example object sso <- eight_schools ################ ### sso_info ### ################ sso_info(sso) ################## ### model_code ### ################## # view model code in example shinystan object 'eight_schools' cat(model_code(sso)) # change the model code in sso # some jags style code my_code <- " model { for (i in 1:length(Y)) { Y[i] ~ dpois(lambda[i]) log(lambda[i]) <- inprod(X[i,], theta[]) } for (j in 1:J) { theta[j] ~ dt(0.0, 1.0, 1.0) } } " sso <- model_code(sso, my_code) cat(model_code(sso)) ############# ### notes ### ############# # view existing notes notes(sso) # add a note to the existing notes sso <- notes(sso, "New note") notes(sso) cat(notes(sso)) # replace existing notes sso <- notes(sso, "replacement note", replace = TRUE) notes(sso) ################## ### model_name ### ################## # view model name model_name(sso) # change model name sso <- model_name(sso, "some other name") identical(model_name(sso), "some other name") ``` -------------------------------- ### Launch ShinyStan with stanfit objects Source: https://mc-stan.org/shinystan/articles/shinystan-package.html Use launch_shinystan to start the app with a stanfit object. Saving the returned object allows for further interaction. ```R library(shinystan) my_sso <- launch_shinystan(my_stanfit) ``` ```R launch_shinystan(my_stanfit) ``` -------------------------------- ### Install ShinyStan from CRAN Source: https://mc-stan.org/shinystan/index.html Installs the latest stable release of the shinystan package from the Comprehensive R Archive Network (CRAN). ```r install.packages("shinystan") ``` -------------------------------- ### Example: Drop Scalar Parameters Source: https://mc-stan.org/shinystan/reference/drop_parameters.html Demonstrates removing scalar parameters 'mu' and 'tau' from the 'eight_schools' shinystan object. The parameter names are printed before and after the operation. ```R # Using example shinystan object 'eight_schools' print(eight_schools@param_names) # Remove the scalar parameters mu and tau sso <- drop_parameters(eight_schools, pars = c("mu", "tau")) print(sso@param_names) ``` -------------------------------- ### Example: Drop All Elements of a Parameter Vector Source: https://mc-stan.org/shinystan/reference/drop_parameters.html Demonstrates removing all elements of the parameter vector 'theta' from a shinystan object. This shows how to handle non-scalar parameters. ```R # Remove all elements of the parameter vector theta sso <- drop_parameters(sso, pars = "theta") print(sso@param_names) ``` -------------------------------- ### Get Proportion of Divergent Iterations Source: https://mc-stan.org/shinystan/reference/retrieve.html Calculates the proportion of divergent iterations for each chain. The 'inc_warmup' argument can be set to TRUE to include warmup iterations. ```R retrieve(sso, "prop_divergent") ``` ```R retrieve(sso, "prop_divergent", inc_warmup = TRUE) ``` -------------------------------- ### Get Posterior Means Source: https://mc-stan.org/shinystan/reference/retrieve.html Extracts posterior means from a shinystan object. Specify parameters using the 'pars' argument. ```R retrieve(sso, "mean", pars = c('theta[1]', 'mu')) ``` -------------------------------- ### Get Rhat Statistics Source: https://mc-stan.org/shinystan/reference/retrieve.html Extracts Rhat statistics from a shinystan object. Use the 'pars' argument to specify parameters. ```R retrieve(sso, "rhat") ``` -------------------------------- ### Get Maximum Tree Depth Source: https://mc-stan.org/shinystan/reference/retrieve.html Retrieves the maximum treedepth for each chain. This can also be accessed using 'depth' or 'tree' as the 'what' argument. ```R retrieve(sso, "max_treedepth") # equivalent to retrieve(sso, "depth"), retrieve(sso, "tree"), etc. ``` -------------------------------- ### Get Posterior Quantiles Source: https://mc-stan.org/shinystan/reference/retrieve.html Extracts posterior quantiles (2.5%, 25%, 50%, 75%, 97.5%) from a shinystan object. Use 'pars' to specify parameters. ```R retrieve(sso, "quantiles") ``` -------------------------------- ### Launch ShinyStan Demo Usage Source: https://mc-stan.org/shinystan/reference/launch_shinystan_demo.html Basic syntax for invoking the demo function with default parameters. ```R launch_shinystan_demo( demo_name = "eight_schools", rstudio = getOption("shinystan.rstudio"), ... ) ``` -------------------------------- ### Launch ShinyStan Demo Source: https://mc-stan.org/shinystan/reference/launch_shinystan_demo.html Launches a demonstration of the ShinyStan application with a specified demo model. ```APIDOC ## POST /launch_shinystan_demo ### Description Launches a demonstration of the ShinyStan application. Currently, only the "eight_schools" demo is available. ### Method POST ### Endpoint /launch_shinystan_demo ### Parameters #### Query Parameters - **demo_name** (string) - Optional - The name of the demo to launch. Defaults to "eight_schools". - **rstudio** (boolean) - Optional - If TRUE, launches the app in the RStudio Viewer. Defaults to the value of the global option `shinystan.rstudio`. - **...** (any) - Optional - Additional arguments passed to `runApp`. ### Request Example ```json { "demo_name": "eight_schools", "rstudio": false } ``` ### Response #### Success Response (200) - **shinystan_object** (S4 object) - An S4 object representing the ShinyStan interface. #### Response Example ```json { "shinystan_object": "" } ``` ``` -------------------------------- ### Create and Launch ShinyStan with mcmc.list, array, or list of matrices Source: https://mc-stan.org/shinystan/reference/launch_shinystan.html Demonstrates the initial step of creating a shinystan object from an mcmc.list, array, or list of matrices before launching the ShinyStan app. Refer to ?as.shinystan for full details. ```R # First create shinystan object (see ?as.shinystan) for full details) ``` -------------------------------- ### Launch ShinyStan App Source: https://mc-stan.org/shinystan/reference/launch_shinystan.html Launches the ShinyStan application in the default web browser or RStudio Viewer. Can accept objects of class 'shinystan', 'stanfit', or 'stanreg'. Optional arguments can be passed to runApp. ```R launch_shinystan(object, ...) # Default S3 method launch_shinystan(object, ..., rstudio = getOption("shinystan.rstudio")) # S3 method for class 'shinystan' launch_shinystan(object, ..., rstudio = getOption("shinystan.rstudio")) ``` -------------------------------- ### Configure ShinyApps account Source: https://mc-stan.org/shinystan/articles/deploy_shinystan.html Set up local credentials for the rsconnect package using account details from the shinyapps.io dashboard. ```R rsconnect::setAccountInfo(name, token, secret) ``` -------------------------------- ### Launch ShinyStan Interface Source: https://mc-stan.org/shinystan/reference/shinystan-package.html Functions to initiate the interactive ShinyStan application for model diagnostics. ```R launch_shinystan_demo() ``` ```R launch_shinystan() ``` -------------------------------- ### Launch ShinyStan Interface Source: https://mc-stan.org/shinystan/reference/launch_shinystan_demo.html Launches the ShinyStan interface for a given shinystan object. ```APIDOC ## POST /launch_shinystan ### Description Launches the ShinyStan interface using a pre-existing shinystan object. ### Method POST ### Endpoint /launch_shinystan ### Parameters #### Request Body - **shinystan_object** (S4 object) - Required - The shinystan object to visualize. - **rstudio** (boolean) - Optional - If TRUE, launches the app in the RStudio Viewer. Defaults to the value of the global option `shinystan.rstudio`. - **...** (any) - Optional - Additional arguments passed to `runApp`. ### Request Example ```json { "shinystan_object": "", "rstudio": true } ``` ### Response #### Success Response (200) - **status** (string) - Indicates the success of launching the interface. #### Response Example ```json { "status": "ShinyStan interface launched successfully." } ``` ``` -------------------------------- ### Deploy ShinyStan Application Source: https://mc-stan.org/shinystan/reference/deploy_shinystan.html Function signature for deploying a shinystan object to a web server. ```R deploy_shinystan(sso, appName, account = NULL, ..., deploy = TRUE) ``` -------------------------------- ### launch_shinystan Source: https://mc-stan.org/shinystan/reference/launch_shinystan.html Launches the ShinyStan app in the default web browser or RStudio Viewer. ```APIDOC ## launch_shinystan ### Description Launches the 'ShinyStan' app in the default web browser. 'RStudio' users also have the option of launching the app in the pop-up Viewer. ### Parameters #### Arguments - **object** (shinystan, stanfit, or stanreg) - Required - The object to use for launching the app. - **...** (dots) - Optional - Optional arguments passed to runApp. - **rstudio** (boolean) - Optional - If TRUE, launches the app in the RStudio Viewer instead of the default web browser. Defaults to getOption("shinystan.rstudio"). ### Response - **shinystan** (S4 object) - Returns an instance of the S4 class "shinystan". ``` -------------------------------- ### launch_shinystan Source: https://mc-stan.org/shinystan/reference/index.html Functions for launching the ShinyStan graphical user interface. ```APIDOC ## launch_shinystan() ### Description Launch the 'ShinyStan' app for a specific model. ### Method Function Call ## launch_shinystan_demo() ### Description Launch a demonstration of the 'ShinyStan' app. ``` -------------------------------- ### Deploy ShinyStan application Source: https://mc-stan.org/shinystan/articles/deploy_shinystan.html Upload a ShinyStan object to the shinyapps.io service. The account argument is mandatory if multiple accounts are configured locally. ```R deploy_shinystan(my_sso, appName = "MyModel", account = "username") ``` ```R deploy_shinystan(my_sso, appName = "MyModel") ``` -------------------------------- ### Deploy ShinyStan App Source: https://mc-stan.org/shinystan/reference/deploy_shinystan.html Deploys a ShinyStan object to shinyapps.io. Requires a shinyapps.io account. ```APIDOC ## POST /deploy_shinystan ### Description Deploys a ShinyStan object to shinyapps.io using the `deploy_shinystan` function. This function requires a shinyapps.io account. ### Method POST ### Endpoint /deploy_shinystan ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body - **sso** (shinystan object) - Required - A shinystan object to deploy. - **appName** (string) - Required - The name for the application. Must be at least four characters long and may only contain letters, numbers, dashes and underscores. - **account** (string) - Optional - Your shinyapps.io account username. Only required if more than one account is configured. - **deploy** (boolean) - Optional - Defaults to TRUE. If FALSE, the app is preprocessed but not deployed. Useful for checking preprocessing success. - **ppcheck_data** (vector) - Optional - Observations for graphical posterior predictive checking. - **ppcheck_yrep** (string) - Optional - Name of the parameter in `sso` containing posterior predictive simulations. ### Request Example ```json { "sso": "your_shinystan_object", "appName": "my-shiny-app", "account": "your_username", "deploy": true, "ppcheck_data": [1, 2, 3], "ppcheck_yrep": "generated_quantities" } ``` ### Response #### Success Response (200) - **status** (boolean) - TRUE if deployment succeeded or preprocessing is complete when deploy is FALSE. #### Response Example ```json { "status": true } ``` ``` -------------------------------- ### Launch ShinyStan with a shinystan object Source: https://mc-stan.org/shinystan/reference/launch_shinystan.html Launches the ShinyStan app with an existing shinystan object. The app can be launched to simply view the object, or to update the object in place or save changes to a new object. ```R # Just launch shinystan launch_shinystan(sso) # Launch shinystan and replace sso with an updated version of itself # if any changes are made to sso while using the app sso <- launch_shinystan(sso) # Launch shinystan but save any changes made to sso while running the app # in a new shinystan object sso2. sso will remained unchanged. sso2 <- launch_shinystan(sso) ``` -------------------------------- ### Convert mcmc.list objects for ShinyStan Source: https://mc-stan.org/shinystan/articles/shinystan-package.html Convert mcmc.list objects to a shinystan object using as.shinystan before launching. Specify the warmup argument if warmup iterations are included in the object. ```R my_sso <- launch_shinystan(as.shinystan(my_mcmc, model_name = "my_model")) ``` ```R my_sso <- launch_shinystan(as.shinystan(my_mcmc, model_name = "my_model", warmup = 100)) ``` -------------------------------- ### Deploy with posterior predictive check data Source: https://mc-stan.org/shinystan/articles/deploy_shinystan.html Include observation data and optional posterior predictive replication parameters to pre-configure graphical checks in the deployed app. ```R deploy_shinystan(my_sso, appName = "MyModel", ppcheck_data = y) ``` ```R deploy_shinystan(my_sso, appName = "MyModel", ppcheck_data = y, ppcheck_yrep = "yRep") ``` -------------------------------- ### deploy_shinystan Source: https://mc-stan.org/shinystan/reference/index.html Function for deploying ShinyStan applications to the web. ```APIDOC ## deploy_shinystan() ### Description Deploy a 'ShinyStan' app on the web using 'shinyapps.io' by 'RStudio'. ``` -------------------------------- ### as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Creates a shinystan object from various input types including arrays, lists, stanfit, stanreg, and mcmc.list objects. ```APIDOC ## as.shinystan ### Description Creates a shinystan object that can be used with launch_shinystan and other functions in the shinystan package. ### Parameters #### Request Body - **X** (object) - Required - The object to be converted (array, list, stanfit, stanreg, or mcmc.list). - **model_name** (string) - Optional - A name for the model. - **warmup** (integer) - Optional - Number of iterations to treat as warmup. - **burnin** (integer) - Optional - Deprecated, use warmup instead. - **param_dims** (list) - Optional - Named list giving dimensions for parameters. - **model_code** (string) - Optional - Character string with the model code. - **note** (string) - Optional - Text to display on the Notepad page. - **sampler_params** (list) - Optional - List of matrices containing sampler information. - **algorithm** (string) - Optional - Either "NUTS" or "HMC". - **max_treedepth** (integer) - Optional - Maximum allowed treedepth. - **pars** (vector) - Optional - Character vector specifying parameters to include (for stanfit/CmdStanMCMC). - **ppd** (logical) - Optional - Whether to draw from posterior predictive distribution (for stanreg). - **seed** (integer) - Optional - Seed for pp_check (for stanreg). ### Response #### Success Response (200) - **shinystan object** (S4 class) - An instance of the S4 class "shinystan". ``` -------------------------------- ### Deploy ShinyStan Application Source: https://mc-stan.org/shinystan/reference/shinystan-package.html Function to deploy a ShinyStan app online using the shinyapps.io service. ```R deploy_shinystan() ``` -------------------------------- ### S4 method for CmdStanMCMC input to as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Converts a 'CmdStanMCMC' object into a shinystan object. Allows specifying parameters to include, model name, and notes. ```R # S4 method for class 'CmdStanMCMC' as.shinystan(X, pars = NULL, model_name = NULL, note = NULL, ...) ``` -------------------------------- ### Convert list of matrices for ShinyStan Source: https://mc-stan.org/shinystan/articles/shinystan-package.html Combine separate chain matrices into a list to create a shinystan object. ```R # Generate some fake data chain1 <- cbind(beta1 = rnorm(100), beta2 = rnorm(100), sigma = rexp(100)) chain2 <- cbind(beta1 = rnorm(100), beta2 = rnorm(100), sigma = rexp(100)) chain_list <- list(chain1, chain2) my_sso <- launch_shinystan(as.shinystan(X = list(chain1, chain2), model_name = "my_model")) ``` -------------------------------- ### S4 method for CmdStanMCMC_CSV input to as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Converts a 'CmdStanMCMC_CSV' object into a shinystan object. Allows specifying parameters to include, model name, and notes. ```R # S4 method for class 'CmdStanMCMC_CSV' as.shinystan(X, pars = NULL, model_name = NULL, note = NULL, ...) ``` -------------------------------- ### Create Shinystan Object from List of Matrices Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Convert a list of matrices (each representing a chain) into a shinystan object. Ensure all matrices have the same number of iterations and parameters, with consistent parameter names and order. ```R if (FALSE) { # \dontrun{ ######################## ### list of matrices ### ######################## # Generate some fake data chain1 <- cbind(beta1 = rnorm(100), beta2 = rnorm(100), sigma = rexp(100)) chain2 <- cbind(beta1 = rnorm(100), beta2 = rnorm(100), sigma = rexp(100)) sso <- as.shinystan(list(chain1, chain2)) launch_shinystan(sso) # We can also specify some or all of the optional arguments # note: in order to use param_dims we need to rename 'beta1' and 'beta2' # to 'beta[1]' and 'beta[2]' colnames(chain1) <- colnames(chain2) <- c(paste0("beta[",1:2,"]"), "sigma") sso2 <- as.shinystan(list(chain1, chain2), model_name = "Example", warmup = 0, param_dims = list(beta = 2, sigma = 0)) launch_shinystan(sso2) } # } ``` -------------------------------- ### Create ShinyStan Object Source: https://mc-stan.org/shinystan/reference/launch_shinystan_demo.html Creates a shinystan object from posterior draws. ```APIDOC ## POST /as_shinystan ### Description Creates a shinystan object from posterior draws, which can then be used to launch the ShinyStan interface. ### Method POST ### Endpoint /as_shinystan ### Parameters #### Request Body - **posterior_draws** (object) - Required - An object containing posterior draws (e.g., from rstan, rstanarm, etc.). - **...** (any) - Optional - Additional arguments passed to the underlying object conversion functions. ### Request Example ```json { "posterior_draws": "" } ``` ### Response #### Success Response (200) - **shinystan_object** (S4 object) - An S4 object representing the ShinyStan interface. #### Response Example ```json { "shinystan_object": "" } ``` ``` -------------------------------- ### S4 method for mcmc.list input to as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Converts an 'mcmc.list' object (from the coda package) into a shinystan object. Allows setting model name, warmup, burnin, parameter dimensions, model code, and notes. ```R # S4 method for class 'mcmc.list' as.shinystan( X, model_name = "unnamed model", warmup = 0, burnin = 0, param_dims = list(), model_code = NULL, note = NULL, ... ) ``` -------------------------------- ### Retrieve Function API Source: https://mc-stan.org/shinystan/reference/retrieve.html This section documents the `retrieve` function, its arguments, and the different types of information it can extract from a shinystan object. ```APIDOC ## GET /retrieve ### Description Retrieves various statistics and diagnostics from a shinystan object. ### Method GET ### Endpoint `/retrieve` ### Parameters #### Query Parameters - **sso** (shinystan object) - Required - The shinystan object to retrieve data from. - **what** (string) - Required - Specifies what data to retrieve. Can be one of the following: - `"rhat"`, `"Rhat"`, `"r_hat"`, or `"R_hat"`: Rhat statistics. - `"N_eff"`, `"n_eff"`, `"neff"`, `"Neff"`, or `"ess"` or `"ESS"`: Effective sample sizes. - `"mean"`: Posterior means. - `"sd"`: Posterior standard deviations. - `"se_mean"` or `"mcse"`: Monte Carlo standard error. - `"median"`: Posterior medians. - `"quantiles"` or any string with `"quant"` in it (case-insensitive): Posterior quantiles (2.5%, 25%, 50%, 75%, 97.5%). - `"avg_accept_stat"` or any string with `"accept"` in it (case-insensitive): Average acceptance probability over the NUTS subtree. - `"prop_divergent"` or any string with `"diverg"` in it (case-insensitive): Proportion of divergent iterations. - `"max_treedepth"` or any string with `"tree"` or `"depth"` in it (case-insensitive): Maximum treedepth. - `"avg_stepsize"` or any string with `"step"` in it (case-insensitive): Average stepsize. - **pars** (array of strings) - Optional - Specifies parameter names to filter the results. Applicable for `"rhat"`, `"N_eff"`, `"mean"`, `"sd"`, `"se_mean"`, and `"median"`. - **inc_warmup** (boolean) - Optional - Whether to include warmup iterations. Applicable for sampler diagnostics like `"avg_accept_stat"`, `"prop_divergent"`, `"max_treedepth"`, and `"avg_stepsize"`. Defaults to `FALSE`. ### Request Example ```json { "sso": "eight_schools_object", "what": "rhat" } ``` ```json { "sso": "eight_schools_object", "what": "mean", "pars": ["theta[1]", "mu"] } ``` ```json { "sso": "eight_schools_object", "what": "prop_divergent", "inc_warmup": true } ``` ### Response #### Success Response (200) - **result** (object or array) - The requested statistics or diagnostics. #### Response Example ```json { "result": [ 1.05, 1.02, 1.03 ] } ``` ```json { "result": { "theta[1]": 5.2, "mu": 0.5 } } ``` ``` -------------------------------- ### Create Shinystan Object from Stanfit Object Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Generate a shinystan object directly from an rstan `stanfit` object. Optional arguments are limited as most information is automatically extracted from the `stanfit` object. ```R if (FALSE) { # \dontrun{ ###################### ### stanfit object ### ###################### library("rstan") fit <- stan_demo("eight_schools") sso <- as.shinystan(fit, model_name = "example") } # } ``` -------------------------------- ### Create Shinystan Object from Array Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Use this method to create a shinystan object from a 3-D array of simulations. The array dimensions must be iterations, chains, and parameters, in that order. ```R if (FALSE) { # \dontrun{ sso <- as.shinystan(X, ...) # replace ... with optional arguments or omit it launch_shinystan(sso) } # } ``` -------------------------------- ### S4 method for stanfit input to as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Converts an 'stanfit' object (from the rstan package) into a shinystan object. Automatically retrieves the model name and allows specifying parameters to include and optional notes. ```R # S4 method for class 'stanfit' as.shinystan(X, pars, model_name = X@model_name, note = NULL, ...) ``` -------------------------------- ### Generic as.shinystan and is.shinystan functions Source: https://mc-stan.org/shinystan/reference/as.shinystan.html These are the generic functions for creating and testing shinystan objects. They dispatch to specific methods based on the input object type. ```R as.shinystan(X, ...) is.shinystan(X) ``` -------------------------------- ### shinystan object management Source: https://mc-stan.org/shinystan/reference/index.html Functions for creating, inspecting, and modifying shinystan S4 objects. ```APIDOC ## as.shinystan() / is.shinystan() ### Description Create and test shinystan objects. ## sso_info() / model_code() / notes() / model_name() ### Description View or change metadata associated with a shinystan object. ## retrieve() ### Description Get summary statistics from a shinystan object. ## drop_parameters() ### Description Drop parameters from a shinystan object. ## update_sso() ### Description Update an object created by a previous version of shinystan. ## generate_quantity() ### Description Add a new quantity to a shinystan object. ``` -------------------------------- ### as.shinystan Function Overloads Source: https://mc-stan.org/shinystan/reference/as.shinystan.html The `as.shinystan` function can accept data in multiple formats, including arrays, lists of matrices, and objects from other R packages like rstan and rstanarm. ```APIDOC ## as.shinystan ### Description Creates a `shinystan` object from various input data formats. ### Method Function Overloads ### Endpoints N/A (R function) ### Parameters #### Input Data Types - **array** (3-D array) - Required - Dimensions: iterations, chains, parameters. - **list** (list of matrices) - Required - Each matrix represents a chain. All matrices must have the same number of iterations (rows) and parameters (columns). Parameter names and order must be consistent. - **mcmc.list** (coda object) - Required - **stanfit** (rstan object) - Required - **stanreg** (rstanarm object) - Required - **CmdStanMCMC** (cmdstanr object) - Required - **CmdStanMCMC_CSV** (cmdstanr object) - Required - Created using `cmdstanr::as_cmdstan_fit()`. #### Optional Arguments (for array and list inputs) - **model_name** (string) - Optional - Name of the model. - **warmup** (integer) - Optional - Number of warmup iterations to discard. - **param_dims** (list) - Optional - Specifies dimensions for parameters (e.g., `list(beta = 2)`). ### Request Example ```R # From a 3-D array sso <- as.shinystan(sim_array, model_name="MyModel") # From a list of matrices chain1 <- cbind(beta1 = rnorm(100), beta2 = rnorm(100)) chain2 <- cbind(beta1 = rnorm(100), beta2 = rnorm(100)) sso <- as.shinystan(list(chain1, chain2)) # From a stanfit object library(rstan) fit <- stan_demo("eight_schools") sso <- as.shinystan(fit, model_name="EightSchools") ``` ### Response - **shinystan object** - A `shinystan` object containing the simulation data and metadata. ``` -------------------------------- ### S4 method for list input to as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Converts a list into a shinystan object. Similar to the array method, it allows customization of model name, warmup, burnin, parameter dimensions, model code, notes, sampler parameters, algorithm, and max treedepth. ```R # S4 method for class 'list' as.shinystan( X, model_name = "unnamed model", warmup = 0, burnin = 0, param_dims = list(), model_code = NULL, note = NULL, sampler_params = NULL, algorithm = NULL, max_treedepth = NULL, ... ) ``` -------------------------------- ### S4 shinystan Object Structure Source: https://mc-stan.org/shinystan/reference/shinystan-class.html Details the structure and slots of the 'shinystan' S4 object. ```APIDOC ## S4 `shinystan` objects Source: `R/shinystan-objects.R` `shinystan-class.Rd` See `as.shinystan` for documentation on creating `shinystan` objects and `eight_schools` for an example object. ### Slots `model_name` (`"character"`) Model name. `param_names` (`"character"`) Parameter names. `param_dims` (`"list"`) Parameter dimensions. `posterior_sample` (`"array"`) MCMC sample. `summary` (`"matrix"`) Summary stats for `posterior_sample`. `sampler_params` (`"list"`) Sampler parameters (for certain Stan models only). `n_chain` (`"integer"`) Number of chains. `n_iter` (`"integer"`) Number of iterations per chain. `n_warmup` (`"integer"`) Number of warmup iterations per chain. `user_model_info` (`"character"`) Notes to display on the **Notepad** page in the 'ShinyStan' GUI. `model_code` (`"character"`) Model code to display on the **Model Code** page in the 'ShinyStan' GUI. `misc` (`"list"`) Miscellaneous, for internal use. ### References Muth, C., Oravecz, Z., and Gabry, J. (2018) User-friendly Bayesian regression modeling: A tutorial with rstanarm and shinystan. _The Quantitative Methods for Psychology_. 14(2), 99–119. https://www.tqmp.org/RegularArticles/vol14-2/p099/p099.pdf ### See also `as.shinystan` for creating `shinystan` objects. `drop_parameters` to remove parameters from a `shinystan` object. `generate_quantity` to add a new quantity to a `shinystan` object. `shinystan-metadata` to view or change metadata associated with a `shinystan` object. ``` -------------------------------- ### View or Change Metadata Source: https://mc-stan.org/shinystan/reference/shinystan-metadata.html Functions to interact with shinystan object metadata. ```APIDOC ## sso_info() ### Description Prints basic metadata including number of parameters, chains, iterations, warmup iterations, etc. Does not return anything. ### Usage ```R sso_info(sso) ``` ### Arguments - **sso** (shinystan object) - The shinystan object to inspect. ### Examples ```R sso_info(sso) ``` ## model_code() ### Description Returns or replaces model code stored in a `shinystan` object. If `code` is `NULL`, returns existing model code. If `code` is specified, returns an updated `shinystan` object with the new code. ### Usage ```R model_code(sso, code = NULL) ``` ### Arguments - **sso** (shinystan object) - The shinystan object. - **code** (string) - Model code to be added. Can be used as an argument to `cat`. ### Value - Returns model code as a character string if `code` is `NULL`. - Returns an updated `shinystan` object if `code` is specified. ### Examples ```R # View model code cat(model_code(sso)) # Change model code my_code <- "\n model {\n for (i in 1:length(Y)) {\n Y[i] ~ dpois(lambda[i])\n log(lambda[i]) <- inprod(X[i,], theta[])\n }\n for (j in 1:J) {\n theta[j] ~ dt(0.0, 1.0, 1.0)\n }\n }\n" sso <- model_code(sso, my_code) cat(model_code(sso)) ``` ## notes() ### Description Returns, amends, or replaces notes stored in a `shinystan` object. If `note` is `NULL`, returns existing notes. If `note` is specified, returns an updated `shinystan` object with notes added or replaced. ### Usage ```R notes(sso, note = NULL, replace = FALSE) ``` ### Arguments - **sso** (shinystan object) - The shinystan object. - **note** (string) - A note to add or replace existing notes. - **replace** (boolean) - If `TRUE`, overwrites existing notes. If `FALSE` (default), appends to existing notes. ### Value - Returns existing notes as a character string if `note` is `NULL`. - Returns an updated `shinystan` object if `note` is specified. ### Examples ```R # View existing notes notes(sso) # Add a note sso <- notes(sso, "New note") cat(notes(sso)) # Replace existing notes sso <- notes(sso, "replacement note", replace = TRUE) notes(sso) ``` ## model_name() ### Description Returns or replaces the model name associated with a `shinystan` object. If `name` is `NULL`, returns the current model name. If `name` is specified, returns `sso` with an updated model name. ### Usage ```R model_name(sso, name = NULL) ``` ### Arguments - **sso** (shinystan object) - The shinystan object. - **name** (string) - The new model name to use. ### Value - Returns the current model name if `name` is `NULL`. - Returns an updated `shinystan` object if `name` is specified. ### Examples ```R # View model name model_name(sso) # Change model name sso <- model_name(sso, "some other name") identical(model_name(sso), "some other name") ``` ``` -------------------------------- ### S4 method for array input to as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Converts an array into a shinystan object. Useful for custom data structures. Requires specifying model name and optionally warmup, burnin, parameter dimensions, model code, notes, sampler parameters, algorithm, and max treedepth. ```R # S4 method for class 'array' as.shinystan( X, model_name = "unnamed model", warmup = 0, burnin = 0, param_dims = list(), model_code = NULL, note = NULL, sampler_params = NULL, algorithm = NULL, max_treedepth = NULL, ... ) ``` -------------------------------- ### Create Shinystan Object from Stanreg Object Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Create a shinystan object from an rstanarm `stanreg` object. This method automatically extracts necessary information from the `stanreg` object. ```R if (FALSE) { # \dontrun{ ###################### ### stanreg object ### ###################### library("rstanarm") example("example_model") sso <- as.shinystan(example_model) launch_shinystan(sso) } # } ``` -------------------------------- ### Update Shinystan Object Source: https://mc-stan.org/shinystan/reference/update_sso.html Use update_sso to ensure compatibility of shinystan objects with the current package version. If the object is already up-to-date, it will be returned as is. ```R sso_new <- update_sso(sso) ``` -------------------------------- ### S4 method for stanreg input to as.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Converts an 'stanreg' object (from the rstanarm package) into a shinystan object. Optionally draws from the posterior predictive distribution (default TRUE) and allows setting a seed, model name, and notes. ```R # S4 method for class 'stanreg' as.shinystan(X, ppd = TRUE, seed = 1234, model_name = NULL, note = NULL, ...) ``` -------------------------------- ### Update SSO Object Source: https://mc-stan.org/shinystan/reference/update_sso.html This function updates an existing shinystan object (sso) to be compatible with the current version of shinystan. If the object is already compatible, it is returned as is with a message. If an error occurs during the update, the original object might not be returned. ```APIDOC ## POST /update_sso ### Description Updates an existing shinystan object (sso) to be compatible with the current version of shinystan. If the object is already compatible, it is returned as is with a message. If an error occurs during the update, the original object might not be returned. ### Method POST ### Endpoint /update_sso ### Parameters #### Request Body - **sso** (shinystan object) - Required - The shinystan object to update. ### Request Example { "sso": "" } ### Response #### Success Response (200) - **updated_sso** (shinystan object) - The updated shinystan object, or the original object if it was already up-to-date. #### Response Example { "updated_sso": "" } ``` -------------------------------- ### is.shinystan Source: https://mc-stan.org/shinystan/reference/as.shinystan.html Tests if an object is a valid shinystan object. ```APIDOC ## is.shinystan ### Description Tests if an object is a valid shinystan object. ### Parameters #### Request Body - **X** (object) - Required - The object to test. ### Response #### Success Response (200) - **boolean** (logical) - Returns TRUE if the object is a shinystan object, FALSE otherwise. ``` -------------------------------- ### Generate new quantities in ShinyStan Source: https://mc-stan.org/shinystan/articles/shinystan-package.html Add derived parameters to a shinystan object using generate_quantity with functions of one or two existing parameters. ```R inv_logit <- function(x) 1/(1 + exp(-x)) sso <- generate_quantity(sso, fun = inv_logit, param1 = "beta", new_name = "gamma") ``` ```R sso <- generate_quantity(sso, fun = function(x,y) (x-y)^2, param1 = "alpha", param2 = "beta", new_name = "delta") ``` -------------------------------- ### Add Model Code to Shinystan Object Source: https://mc-stan.org/shinystan/articles/shinystan-package.html Use the `model_code()` function to add custom model code to an existing shinystan object. This is useful for models not fit with rstan. ```r my_code <- " model { for (i in 1:length(Y)) { Y[i] ~ dpois(lambda[i]) log(lambda[i]) <- inprod(X[i,], theta[]) } for (j in 1:J) { theta[j] ~ dt(0.0, 1.0, 1.0) } } " # Add the code to a shinystan object sso sso <- model_code(sso, code = my_code) ``` -------------------------------- ### generate_quantity Source: https://mc-stan.org/shinystan/reference/generate_quantity.html Adds a new parameter to a shinystan object as a function of one or two existing parameters. ```APIDOC ## generate_quantity ### Description Add to shinystan object a new parameter as a function of one or two existing parameters. ### Method N/A (R function) ### Endpoint N/A (R function) ### Parameters #### Arguments - **sso** (shinystan object) - A `shinystan object`. - **param1** (character string) - Name of first parameter as character string. - **param2** (character string) - Optional. Name of second parameter as character string. - **fun** (function) - Function to call, i.e. `function(param1)` or `function(param1,param2)`. See Examples, below. - **new_name** (character string) - Name for the new parameter as character string. ### Request Example ```R # Using example shinystan object 'eight_schools' sso <- eight_schools sso <- generate_quantity(sso, fun = function(x) x^2, param1 = "tau", new_name = "tau_sq") sso <- generate_quantity(sso, fun = "-", param1 = "theta[1]", param2 = "theta[2]", new_name = "theta1minus2") ``` ### Response #### Success Response (Updated shinystan object) - **sso** (shinystan object) - The updated shinystan object with the new quantity. #### Response Example ```R # sso object updated with new quantities ``` ### See also `drop_parameters` to remove parameters from a `shinystan` object. ``` -------------------------------- ### Manage shinystan object metadata Source: https://mc-stan.org/shinystan/reference/shinystan-metadata.html Functions to access or modify metadata properties of a shinystan object. ```R sso_info(sso) model_code(sso, code = NULL) notes(sso, note = NULL, replace = FALSE) model_name(sso, name = NULL) ``` -------------------------------- ### Add New Quantity to ShinyStan Object Source: https://mc-stan.org/shinystan/reference/generate_quantity.html Use this function to add a new parameter to a shinystan object, calculated as a function of existing parameters. Specify the shinystan object, the parameter(s) to use, the function to apply, and the name for the new parameter. ```R sso <- eight_schools sso <- generate_quantity(sso, fun = function(x) x^2, param1 = "tau", new_name = "tau_sq") sso <- generate_quantity(sso, fun = "-", param1 = "theta[1]", param2 = "theta[2]", new_name = "theta1minus2") ``` -------------------------------- ### Rename a Model in Shinystan Source: https://mc-stan.org/shinystan/articles/shinystan-package.html Use the `model_name()` function to change the name of a model associated with a shinystan object. This name is displayed on the home page of ShinyStan. ```r sso <- model_name(sso, "new_model_name") ``` -------------------------------- ### Drop Parameters from ShinyStan Object Source: https://mc-stan.org/shinystan/reference/drop_parameters.html Use this function to remove parameters from a shinystan object. This is useful for reducing object size and improving performance. Ensure parameter names are correctly specified. ```R drop_parameters(sso, pars) ``` -------------------------------- ### drop_parameters Source: https://mc-stan.org/shinystan/reference/drop_parameters.html Removes selected parameters from a shinystan object. ```APIDOC ## drop_parameters(sso, pars) ### Description Remove selected parameters from a shinystan object. This is useful if you have a very large shinystan object when you only want to look at a subset of parameters. With a smaller shinystan object, launch_shinystan will be faster and you should experience better performance (responsiveness) after launching when using the 'ShinyStan' app. ### Parameters - **sso** (shinystan object) - Required - A shinystan object. - **pars** (character vector) - Required - A character vector of parameter names. If the name of a non-scalar (e.g. vector, matrix) parameter is included in pars all of its elements will be removed. ### Value Returns the shinystan object (sso) with the specified parameters dropped. ### Request Example # Remove the scalar parameters mu and tau sso <- drop_parameters(eight_schools, pars = c("mu", "tau")) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.