The $diagnose() method of a CmdStanModel object
runs Stan's basic diagnostic feature that will calculate the gradients
of the initial state and compare them with gradients calculated by
finite differences. Discrepancies between the two indicate that there is
a problem with the model or initial states or else there is a bug in Stan.
Unlike other CmdStan methods, $diagnose() does not expose show_messages
or show_exceptions arguments. CmdStan's standard output is not printed
during execution, while standard error is always displayed. The captured
console output can be inspected with the returned object's
$output() method.
Usage
diagnose(
data = NULL,
seed = NULL,
init = NULL,
output_dir = getOption("cmdstanr_output_dir"),
output_basename = NULL,
epsilon = NULL,
error = NULL
)Arguments
- data
(multiple options) The data to use for the variables specified in the data block of the Stan program. One of the following:
A named list of R objects with the names corresponding to variables declared in the data block of the Stan program. Internally this list is then written to JSON for CmdStan using
write_stan_json(). Seewrite_stan_json()for details on the conversions performed on R objects before they are passed to Stan.A path to a data file compatible with CmdStan (JSON or R dump). See the appendices in the CmdStan guide for details on using these formats.
NULLor an empty list if the Stan program has no data block.
- seed
(non-negative integer(s)) A seed for the (P)RNG to pass to CmdStan. In the case of multi-chain sampling the single
seedwill automatically be augmented by the run (chain) ID so that each chain uses a different seed. The exception is the transformed data block, which defaults to using same seed for all chains so that the same data is generated for all chains if RNG functions are used. The only timeseedshould be specified as a vector (one element per chain) is if RNG functions are used in transformed data and the goal is to generate different data for each chain.- init
(multiple options) The initialization method to use for the variables declared in the parameters block of the Stan program. One of the following:
A real number
x > 0. This initializes all parameters randomly between[-x, x]on the unconstrained parameter space.The number
0. This initializes all parameters to0on the unconstrained parameter space.A character vector of paths to JSON or Rdump files containing initial values for all or some parameters. For MCMC and Pathfinder, if only a single file is provided it will be reused for all chains and paths. See
write_stan_json()to write R objects to JSON files compatible with CmdStan.A list of lists containing initial values for all or some parameters. For MCMC the list should contain a sublist for each chain, and for Pathfinder it should contain a sublist for each path. For other model fitting methods there should be just one sublist. The sublists should have named elements corresponding to the parameters for which you are specifying initial values. See Examples. A tuple parameter is given as an unnamed list of its elements and a complex parameter as an R complex value, see
write_stan_json().A function that returns a single list with names corresponding to the parameters for which you are specifying initial values. The function can take no arguments or a single argument
chain_id. For MCMC and Pathfinder, the function is called once for each chain or path. If the function has thechain_idargument, it receives the chain or path number, starting at 1. See Examples.A
CmdStanMCMC,CmdStanMLE,CmdStanVB,CmdStanPathfinder, orCmdStanLaplacefit object. If the fit object's parameters are only a subset of the model parameters then the other parameters will be drawn by Stan's default initialization. The fit object must have at least some parameters that are the same name and dimensions as the current Stan model. For thesampleandpathfindermethods, which need one initialization per chain or path, the inits are drawn from the fit object without replacement, so it must contain at least as many draws as the number of chains/paths. ForCmdStanVB,CmdStanLaplace, andCmdStanPathfinderfit objects the draws must additionally be distinct. ACmdStanMLEfit object is the exception: its single draw (the mode) is used to initialize every chain or path. When aCmdStanPathfinderfit object is used as the init, if CmdStan actually performed PSIS resampling (which requiresnum_paths > 1,psis_resample = TRUE, andcalculate_lp = TRUE), CmdStanR selects from the returned draws using uniform weights to avoid applying importance weights again. If CmdStan did not PSIS-resample the output andcalculate_lp = TRUE, CmdStanR selects draws using Pareto-smoothed importance weights. This includes single-path fits withpsis_resample = TRUE, because CmdStan does not PSIS-resample single-path output. Ifcalculate_lp = FALSE, uniform weights are used because importance weights cannot be calculated. PSIS resampling is used to select the draws forCmdStanVB, andCmdStanLaplacefit objects.A type inheriting from
posterior::draws. If the draws object has fewer draws than the number of requested chains/paths, the draws are reused in their existing order until each chain/path has an initialization. If there are more draws than requested chains/paths, draws are selected uniformly without replacement. If the draws object's parameters are only a subset of the model parameters then the other parameters will be drawn by Stan's default initialization. The draws object must have at least some parameters that are the same name and dimensions as the current Stan model.
- output_dir
(string) A path to a directory where CmdStan should write its output CSV files. For MCMC there will be one file per chain; for other methods there will be a single file. For interactive use this can typically be left at
NULL(temporary directory) since CmdStanR makes the CmdStan output (posterior draws and diagnostics) available in R via methods of the fitted model objects. This can be set for an entire R session usingoptions(cmdstanr_output_dir). The behavior ofoutput_diris as follows:If
NULL(the default), then the CSV files are written to a temporary directory and only saved permanently if the user calls one of the$save_*methods of the fitted model object (e.g.,$save_output_files()). These temporary files are removed when the fitted model object is garbage collected (manually or automatically).If a path, then the files are created in
output_dirwith names corresponding to the defaults used by$save_output_files().
- output_basename
(string) A string to use as a prefix for the names of the output CSV files of CmdStan. If
NULL(the default), the basename of the output CSV files is composed of the model name, timestamp, and a six-character random hexadecimal suffix.- epsilon
(positive real) The finite difference step size. Default value is 1e-6.
- error
(positive real) The error threshold. Default value is 1e-6.
Value
A CmdStanDiagnose object.
See also
The $gradients() method for accessing the
gradients and the $output() method for displaying
the captured console output.
The CmdStanR website (mc-stan.org/cmdstanr) for online documentation and tutorials.
The Stan and CmdStan documentation:
Stan documentation: mc-stan.org/users/documentation
CmdStan User’s Guide: mc-stan.org/docs/cmdstan-guide
Other CmdStanModel methods:
model-method-build_info,
model-method-check_syntax,
model-method-cmdstan_defaults,
model-method-expose_functions,
model-method-format,
model-method-generate-quantities,
model-method-is_current,
model-method-laplace,
model-method-model-info,
model-method-optimize,
model-method-pathfinder,
model-method-sample,
model-method-sample_mpi,
model-method-variables,
model-method-variational
Examples
# \dontrun{
test <- cmdstanr_example("logistic", method = "diagnose")
# retrieve the gradients
test$gradients()
#> param_idx value model finite_diff error
#> 1 0 -0.255100 12.2161 12.2161 9.85229e-09
#> 2 1 0.690993 -22.2288 -22.2288 9.84959e-09
#> 3 2 -1.422010 19.7057 19.7057 4.31501e-09
#> 4 3 -0.254377 16.5155 16.5155 -2.06800e-09
# }