From 63ad54b117ea81dcd426bb674c5e251f600545d1 Mon Sep 17 00:00:00 2001 From: Andrew Johnson Date: Tue, 26 Sep 2023 07:58:44 +0300 Subject: [PATCH] Re-generate manuals --- NAMESPACE | 1 + man/CmdStanDiagnose.Rd | 1 + man/CmdStanGQ.Rd | 1 + man/CmdStanMCMC.Rd | 1 + man/CmdStanMLE.Rd | 1 + man/CmdStanModel.Rd | 1 + man/CmdStanPathfinder.Rd | 72 ++++++ man/CmdStanVB.Rd | 3 +- man/as_draws.CmdStanMCMC.Rd | 3 + man/cmdstanr_example.Rd | 4 + man/fit-method-lp.Rd | 2 + man/model-method-check_syntax.Rd | 1 + man/model-method-compile.Rd | 1 + man/model-method-diagnose.Rd | 1 + man/model-method-expose_functions.Rd | 1 + man/model-method-format.Rd | 1 + man/model-method-generate-quantities.Rd | 1 + man/model-method-optimize.Rd | 1 + man/model-method-pathfinder.Rd | 318 ++++++++++++++++++++++++ man/model-method-sample.Rd | 1 + man/model-method-sample_mpi.Rd | 1 + man/model-method-variables.Rd | 1 + man/model-method-variational.Rd | 1 + 23 files changed, 418 insertions(+), 1 deletion(-) create mode 100644 man/CmdStanPathfinder.Rd create mode 100644 man/model-method-pathfinder.Rd diff --git a/NAMESPACE b/NAMESPACE index 4ab6a168e..c8a6217dc 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -4,6 +4,7 @@ S3method(as_draws,CmdStanGQ) S3method(as_draws,CmdStanLaplace) S3method(as_draws,CmdStanMCMC) S3method(as_draws,CmdStanMLE) +S3method(as_draws,CmdStanPathfinder) S3method(as_draws,CmdStanVB) export(as_cmdstan_fit) export(as_draws) diff --git a/man/CmdStanDiagnose.Rd b/man/CmdStanDiagnose.Rd index 70930db17..8c786e88f 100644 --- a/man/CmdStanDiagnose.Rd +++ b/man/CmdStanDiagnose.Rd @@ -45,6 +45,7 @@ Other fitted model objects: \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanGQ.Rd b/man/CmdStanGQ.Rd index 6ebe2d9c6..73fab133a 100644 --- a/man/CmdStanGQ.Rd +++ b/man/CmdStanGQ.Rd @@ -106,6 +106,7 @@ Other fitted model objects: \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, \code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanMCMC.Rd b/man/CmdStanMCMC.Rd index c74ff41f2..2148b7a68 100644 --- a/man/CmdStanMCMC.Rd +++ b/man/CmdStanMCMC.Rd @@ -89,6 +89,7 @@ Other fitted model objects: \code{\link{CmdStanGQ}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanMLE.Rd b/man/CmdStanMLE.Rd index 141e45aa6..82c119106 100644 --- a/man/CmdStanMLE.Rd +++ b/man/CmdStanMLE.Rd @@ -79,6 +79,7 @@ Other fitted model objects: \code{\link{CmdStanGQ}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, +\code{\link{CmdStanPathfinder}}, \code{\link{CmdStanVB}} } \concept{fitted model objects} diff --git a/man/CmdStanModel.Rd b/man/CmdStanModel.Rd index 2644784e2..d3f769953 100644 --- a/man/CmdStanModel.Rd +++ b/man/CmdStanModel.Rd @@ -47,6 +47,7 @@ methods, many of which have their own (linked) documentation pages: \code{\link[=model-method-sample_mpi]{$sample_mpi()}} \tab Run CmdStan's \code{"sample"} method with \href{https://mc-stan.org/math/mpi.html}{MPI}, return \code{\link{CmdStanMCMC}} object. \cr \code{\link[=model-method-optimize]{$optimize()}} \tab Run CmdStan's \code{"optimize"} method, return \code{\link{CmdStanMLE}} object. \cr \code{\link[=model-method-variational]{$variational()}} \tab Run CmdStan's \code{"variational"} method, return \code{\link{CmdStanVB}} object. \cr + \code{\link[=model-method-pathfinder]{$pathfinder()}} \tab Run CmdStan's \code{"pathfinder"} method, return \code{\link{CmdStanPathfinder}} object. \cr \code{\link[=model-method-generate-quantities]{$generate_quantities()}} \tab Run CmdStan's \code{"generate quantities"} method, return \code{\link{CmdStanGQ}} object. \cr } diff --git a/man/CmdStanPathfinder.Rd b/man/CmdStanPathfinder.Rd new file mode 100644 index 000000000..fdfd7f771 --- /dev/null +++ b/man/CmdStanPathfinder.Rd @@ -0,0 +1,72 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/fit.R +\name{CmdStanPathfinder} +\alias{CmdStanPathfinder} +\title{CmdStanPathfinder objects} +\description{ +A \code{CmdStanPathfinder} object is the fitted model object returned by the +\code{\link[=model-method-pathfinder]{$pathfinder()}} method of a +\code{\link{CmdStanModel}} object. +} +\section{Methods}{ + \code{CmdStanPathfinder} objects have the following associated methods, +all of which have their own (linked) documentation pages. +\subsection{Extract contents of fitted model object}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-draws]{$draws()}} \tab Return approximate posterior draws as a \code{\link[posterior:draws_matrix]{draws_matrix}}. \cr + \code{\link[=fit-method-lp]{$lp()}} \tab Return the total log probability density (\code{target}) computed in the model block of the Stan program. \cr + \code{\link[=fit-method-lp]{$lp_approx()}} \tab Return the log density of the variational approximation to the posterior. \cr + \code{\link[=fit-method-init]{$init()}} \tab Return user-specified initial values. \cr + \code{\link[=fit-method-metadata]{$metadata()}} \tab Return a list of metadata gathered from the CmdStan CSV files. \cr + \code{\link[=fit-method-code]{$code()}} \tab Return Stan code as a character vector. \cr +} + +} + +\subsection{Summarize inferences}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-summary]{$summary()}} \tab Run \code{\link[posterior:draws_summary]{posterior::summarise_draws()}}. \cr + \code{\link[=fit-method-cmdstan_summary]{$cmdstan_summary()}} \tab Run and print CmdStan's \code{bin/stansummary}. \cr +} + +} + +\subsection{Save fitted model object and temporary files}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-save_object]{$save_object()}} \tab Save fitted model object to a file. \cr + \code{\link[=fit-method-save_output_files]{$save_output_files()}} \tab Save output CSV files to a specified location. \cr + \code{\link[=fit-method-save_data_file]{$save_data_file()}} \tab Save JSON data file to a specified location. \cr + \code{\link[=fit-method-save_latent_dynamics_files]{$save_latent_dynamics_files()}} \tab Save diagnostic CSV files to a specified location. \cr +} + +} + +\subsection{Report run times, console output, return codes}{\tabular{ll}{ + \strong{Method} \tab \strong{Description} \cr + \code{\link[=fit-method-time]{$time()}} \tab Report the total run time. \cr + \code{\link[=fit-method-output]{$output()}} \tab Pretty print the output that was printed to the console. \cr + \code{\link[=fit-method-return_codes]{$return_codes()}} \tab Return the return codes from the CmdStan runs. \cr +} + +} +} + +\seealso{ +The CmdStanR website +(\href{https://mc-stan.org/cmdstanr/}{mc-stan.org/cmdstanr}) for online +documentation and tutorials. + +The Stan and CmdStan documentation: +\itemize{ +\item Stan documentation: \href{https://mc-stan.org/users/documentation/}{mc-stan.org/users/documentation} +\item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} +} + +Other fitted model objects: +\code{\link{CmdStanDiagnose}}, +\code{\link{CmdStanGQ}}, +\code{\link{CmdStanMCMC}}, +\code{\link{CmdStanMLE}}, +\code{\link{CmdStanVB}} +} +\concept{fitted model objects} diff --git a/man/CmdStanVB.Rd b/man/CmdStanVB.Rd index 9c8cd80b2..323ec339b 100644 --- a/man/CmdStanVB.Rd +++ b/man/CmdStanVB.Rd @@ -82,6 +82,7 @@ Other fitted model objects: \code{\link{CmdStanGQ}}, \code{\link{CmdStanLaplace}}, \code{\link{CmdStanMCMC}}, -\code{\link{CmdStanMLE}} +\code{\link{CmdStanMLE}}, +\code{\link{CmdStanPathfinder}} } \concept{fitted model objects} diff --git a/man/as_draws.CmdStanMCMC.Rd b/man/as_draws.CmdStanMCMC.Rd index a63ae953f..f28d5f50c 100644 --- a/man/as_draws.CmdStanMCMC.Rd +++ b/man/as_draws.CmdStanMCMC.Rd @@ -7,6 +7,7 @@ \alias{as_draws.CmdStanLaplace} \alias{as_draws.CmdStanVB} \alias{as_draws.CmdStanGQ} +\alias{as_draws.CmdStanPathfinder} \title{Create a \code{draws} object from a CmdStanR fitted model object} \usage{ \method{as_draws}{CmdStanMCMC}(x, ...) @@ -18,6 +19,8 @@ \method{as_draws}{CmdStanVB}(x, ...) \method{as_draws}{CmdStanGQ}(x, ...) + +\method{as_draws}{CmdStanPathfinder}(x, ...) } \arguments{ \item{x}{A CmdStanR fitted model object.} diff --git a/man/cmdstanr_example.Rd b/man/cmdstanr_example.Rd index 3474811b9..a487515f5 100644 --- a/man/cmdstanr_example.Rd +++ b/man/cmdstanr_example.Rd @@ -7,7 +7,11 @@ \usage{ cmdstanr_example( example = c("logistic", "schools", "schools_ncp"), +<<<<<<< HEAD method = c("sample", "optimize", "laplace", "variational", "diagnose"), +======= + method = c("sample", "optimize", "variational", "pathfinder", "diagnose"), +>>>>>>> dbde1a6f (Re-generate manuals) ..., quiet = TRUE, force_recompile = getOption("cmdstanr_force_recompile", default = FALSE) diff --git a/man/fit-method-lp.Rd b/man/fit-method-lp.Rd index acda71b7c..686c55d2b 100644 --- a/man/fit-method-lp.Rd +++ b/man/fit-method-lp.Rd @@ -8,6 +8,8 @@ \usage{ lp() +lp_approx() + lp_approx() } \value{ diff --git a/man/model-method-check_syntax.Rd b/man/model-method-check_syntax.Rd index 64e93c02d..ab1fa8c3c 100644 --- a/man/model-method-check_syntax.Rd +++ b/man/model-method-check_syntax.Rd @@ -85,6 +85,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-compile.Rd b/man/model-method-compile.Rd index 7e1805747..52578854e 100644 --- a/man/model-method-compile.Rd +++ b/man/model-method-compile.Rd @@ -156,6 +156,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-diagnose.Rd b/man/model-method-diagnose.Rd index 15e7e21c4..413ad64b3 100644 --- a/man/model-method-diagnose.Rd +++ b/man/model-method-diagnose.Rd @@ -127,6 +127,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-expose_functions.Rd b/man/model-method-expose_functions.Rd index 066c7bad3..a62f7bb86 100644 --- a/man/model-method-expose_functions.Rd +++ b/man/model-method-expose_functions.Rd @@ -76,6 +76,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-format.Rd b/man/model-method-format.Rd index 294ad8cce..2aa34f186 100644 --- a/man/model-method-format.Rd +++ b/man/model-method-format.Rd @@ -105,6 +105,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-generate-quantities.Rd b/man/model-method-generate-quantities.Rd index adf2fd146..c9173c30e 100644 --- a/man/model-method-generate-quantities.Rd +++ b/man/model-method-generate-quantities.Rd @@ -176,6 +176,7 @@ Other CmdStanModel methods: \code{\link{model-method-format}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-optimize.Rd b/man/model-method-optimize.Rd index 06535f141..f01c7d4f6 100644 --- a/man/model-method-optimize.Rd +++ b/man/model-method-optimize.Rd @@ -308,6 +308,7 @@ Other CmdStanModel methods: \code{\link{model-method-format}}, \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, diff --git a/man/model-method-pathfinder.Rd b/man/model-method-pathfinder.Rd new file mode 100644 index 000000000..b95055b52 --- /dev/null +++ b/man/model-method-pathfinder.Rd @@ -0,0 +1,318 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/model.R +\name{model-method-pathfinder} +\alias{model-method-pathfinder} +\alias{pathfinder} +\title{Run Stan's Pathfinder Variational Inference Algorithm} +\usage{ +pathfinder( + data = NULL, + seed = NULL, + refresh = NULL, + init = NULL, + save_latent_dynamics = FALSE, + output_dir = NULL, + output_basename = NULL, + sig_figs = NULL, + threads = NULL, + opencl_ids = NULL, + num_threads = NULL, + init_alpha = NULL, + tol_obj = NULL, + tol_rel_obj = NULL, + tol_grad = NULL, + tol_rel_grad = NULL, + tol_param = NULL, + history_size = NULL, + num_psis_draws = NULL, + num_paths = NULL, + max_lbfgs_iters = NULL, + num_draws = NULL +) +} +\arguments{ +\item{data}{(multiple options) The data to use for the variables specified in +the data block of the Stan program. One of the following: +\itemize{ +\item 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 \code{\link[=write_stan_json]{write_stan_json()}}. See +\code{\link[=write_stan_json]{write_stan_json()}} for details on the conversions performed on \R objects +before they are passed to Stan. +\item 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. +\item \code{NULL} or an empty list if the Stan program has no data block. +}} + +\item{seed}{(positive integer(s)) A seed for the (P)RNG to pass to CmdStan. +In the case of multi-chain sampling the single \code{seed} will automatically be +augmented by the 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 time \code{seed} should be specified +as a vector (one element per chain) is if RNG functions are used in +transformed data and the goal is to generate \emph{different} data for each +chain.} + +\item{refresh}{(non-negative integer) The number of iterations between +printed screen updates. If \code{refresh = 0}, only error messages will be +printed.} + +\item{init}{(multiple options) The initialization method to use for the +variables declared in the parameters block of the Stan program. One of +the following: +\itemize{ +\item A real number \code{x>0}. This initializes \emph{all} parameters randomly between +\verb{[-x,x]} on the \emph{unconstrained} parameter space.; +\item The number \code{0}. This initializes \emph{all} parameters to \code{0}; +\item A character vector of paths (one per chain) to JSON or Rdump files +containing initial values for all or some parameters. See +\code{\link[=write_stan_json]{write_stan_json()}} to write \R objects to JSON files compatible with +CmdStan. +\item A list of lists containing initial values for all or some parameters. For +MCMC the list should contain a sublist for each chain. For optimization and +variational inference there should be just one sublist. The sublists should +have named elements corresponding to the parameters for which you are +specifying initial values. See \strong{Examples}. +\item 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 \code{chain_id}. For MCMC, if the function +has argument \code{chain_id} it will be supplied with the chain id (from 1 to +number of chains) when called to generate the initial values. See +\strong{Examples}. +}} + +\item{save_latent_dynamics}{(logical) Should auxiliary diagnostic information +about the latent dynamics be written to temporary diagnostic CSV files? +This argument replaces CmdStan's \code{diagnostic_file} argument and the content +written to CSV is controlled by the user's CmdStan installation and not +CmdStanR (for some algorithms no content may be written). The default +is \code{FALSE}, which is appropriate for almost every use case. To save the +temporary files created when \code{save_latent_dynamics=TRUE} see the +\code{\link[=fit-method-save_latent_dynamics_files]{$save_latent_dynamics_files()}} +method.} + +\item{output_dir}{(string) A path to a directory where CmdStan should write +its output CSV files. For interactive use this can typically be left at +\code{NULL} (temporary directory) since CmdStanR makes the CmdStan output +(posterior draws and diagnostics) available in \R via methods of the fitted +model objects. The behavior of \code{output_dir} is as follows: +\itemize{ +\item If \code{NULL} (the default), then the CSV files are written to a temporary +directory and only saved permanently if the user calls one of the \verb{$save_*} +methods of the fitted model object (e.g., +\code{\link[=fit-method-save_output_files]{$save_output_files()}}). These temporary +files are removed when the fitted model object is +\link[base:gc]{garbage collected} (manually or automatically). +\item If a path, then the files are created in \code{output_dir} with names +corresponding to the defaults used by \verb{$save_output_files()}. +}} + +\item{output_basename}{(string) A string to use as a prefix for the names of +the output CSV files of CmdStan. If \code{NULL} (the default), the basename of +the output CSV files will be comprised from the model name, timestamp, and +5 random characters.} + +\item{sig_figs}{(positive integer) The number of significant figures used +when storing the output values. By default, CmdStan represent the output +values with 6 significant figures. The upper limit for \code{sig_figs} is 18. +Increasing this value will result in larger output CSV files and thus an +increased usage of disk space.} + +\item{threads}{(positive integer) If the model was +\link[=model-method-compile]{compiled} with threading support, the number of +threads to use in parallelized sections (e.g., when using the Stan +functions \code{reduce_sum()} or \code{map_rect()}).} + +\item{opencl_ids}{(integer vector of length 2) The platform and +device IDs of the OpenCL device to use for fitting. The model must +be compiled with \code{cpp_options = list(stan_opencl = TRUE)} for this +argument to have an effect.} + +\item{tol_rel_obj}{(positive real) Convergence tolerance on the relative norm +of the objective.} + +\item{algorithm}{(string) The algorithm. Either \code{"meanfield"} or +\code{"fullrank"}.} + +\item{iter}{(positive integer) The \emph{maximum} number of iterations.} + +\item{grad_samples}{(positive integer) The number of samples for Monte Carlo +estimate of gradients.} + +\item{elbo_samples}{(positive integer) The number of samples for Monte Carlo +estimate of ELBO (objective function).} + +\item{eta}{(positive real) The step size weighting parameter for adaptive +step size sequence.} + +\item{adapt_engaged}{(logical) Do warmup adaptation?} + +\item{adapt_iter}{(positive integer) The \emph{maximum} number of adaptation +iterations.} + +\item{eval_elbo}{(positive integer) Evaluate ELBO every Nth iteration.} + +\item{output_samples}{(positive integer) Number of approximate posterior +samples to draw and save.} +} +\value{ +A \code{\link{CmdStanVB}} object. +} +\description{ +The \verb{$pathfinder()} method of a \code{\link{CmdStanModel}} object runs +Stan's Pathfinder algorithms. + +Any argument left as \code{NULL} will default to the default value used by the +installed version of CmdStan. See the +\href{https://mc-stan.org/docs/cmdstan-guide/}{CmdStan User’s Guide} +for more details. +} +\details{ +CmdStan can fit a variational approximation to the posterior. The +approximation is a Gaussian in the unconstrained variable space. Stan +implements two variational algorithms. The \code{algorithm="meanfield"} option +uses a fully factorized Gaussian for the approximation. The +\code{algorithm="fullrank"} option uses a Gaussian with a full-rank covariance +matrix for the approximation. + +-- \href{https://github.com/stan-dev/cmdstan/releases/latest}{\emph{CmdStan Interface User's Guide}} +} +\examples{ +\dontrun{ +library(cmdstanr) +library(posterior) +library(bayesplot) +color_scheme_set("brightblue") + +# Set path to CmdStan +# (Note: if you installed CmdStan via install_cmdstan() with default settings +# then setting the path is unnecessary but the default below should still work. +# Otherwise use the `path` argument to specify the location of your +# CmdStan installation.) +set_cmdstan_path(path = NULL) + +# Create a CmdStanModel object from a Stan program, +# here using the example model that comes with CmdStan +file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.stan") +mod <- cmdstan_model(file) +mod$print() + +# Data as a named list (like RStan) +stan_data <- list(N = 10, y = c(0,1,0,0,0,0,0,0,0,1)) + +# Run MCMC using the 'sample' method +fit_mcmc <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + parallel_chains = 2 +) + +# Use 'posterior' package for summaries +fit_mcmc$summary() + +# Get posterior draws +draws <- fit_mcmc$draws() +print(draws) + +# Convert to data frame using posterior::as_draws_df +as_draws_df(draws) + +# Plot posterior using bayesplot (ggplot2) +mcmc_hist(fit_mcmc$draws("theta")) + +# Call CmdStan's diagnose and stansummary utilities +fit_mcmc$cmdstan_diagnose() +fit_mcmc$cmdstan_summary() + +# For models fit using MCMC, if you like working with RStan's stanfit objects +# then you can create one with rstan::read_stan_csv() + +# stanfit <- rstan::read_stan_csv(fit_mcmc$output_files()) + + +# Run 'optimize' method to get a point estimate (default is Stan's LBFGS algorithm) +# and also demonstrate specifying data as a path to a file instead of a list +my_data_file <- file.path(cmdstan_path(), "examples/bernoulli/bernoulli.data.json") +fit_optim <- mod$optimize(data = my_data_file, seed = 123) + +fit_optim$summary() + + +# Run 'variational' method to approximate the posterior (default is meanfield ADVI) +fit_vb <- mod$variational(data = stan_data, seed = 123) + +fit_vb$summary() + +# Plot approximate posterior using bayesplot +mcmc_hist(fit_vb$draws("theta")) + + +# Specifying initial values as a function +fit_mcmc_w_init_fun <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + refresh = 0, + init = function() list(theta = runif(1)) +) +fit_mcmc_w_init_fun_2 <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + refresh = 0, + init = function(chain_id) { + # silly but demonstrates optional use of chain_id + list(theta = 1 / (chain_id + 1)) + } +) +fit_mcmc_w_init_fun_2$init() + +# Specifying initial values as a list of lists +fit_mcmc_w_init_list <- mod$sample( + data = stan_data, + seed = 123, + chains = 2, + refresh = 0, + init = list( + list(theta = 0.75), # chain 1 + list(theta = 0.25) # chain 2 + ) +) +fit_optim_w_init_list <- mod$optimize( + data = stan_data, + seed = 123, + init = list( + list(theta = 0.75) + ) +) +fit_optim_w_init_list$init() +} + +} +\seealso{ +The CmdStanR website +(\href{https://mc-stan.org/cmdstanr/}{mc-stan.org/cmdstanr}) for online +documentation and tutorials. + +The Stan and CmdStan documentation: +\itemize{ +\item Stan documentation: \href{https://mc-stan.org/users/documentation/}{mc-stan.org/users/documentation} +\item CmdStan User’s Guide: \href{https://mc-stan.org/docs/cmdstan-guide/}{mc-stan.org/docs/cmdstan-guide} +} + +Other CmdStanModel methods: +\code{\link{model-method-check_syntax}}, +\code{\link{model-method-compile}}, +\code{\link{model-method-diagnose}}, +\code{\link{model-method-expose_functions}}, +\code{\link{model-method-format}}, +\code{\link{model-method-generate-quantities}}, +\code{\link{model-method-optimize}}, +\code{\link{model-method-sample_mpi}}, +\code{\link{model-method-sample}}, +\code{\link{model-method-variables}}, +\code{\link{model-method-variational}} +} +\concept{CmdStanModel methods} diff --git a/man/model-method-sample.Rd b/man/model-method-sample.Rd index 1c415f6a6..3ae07c2dd 100644 --- a/man/model-method-sample.Rd +++ b/man/model-method-sample.Rd @@ -418,6 +418,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-variables}}, \code{\link{model-method-variational}} diff --git a/man/model-method-sample_mpi.Rd b/man/model-method-sample_mpi.Rd index 8c17a4dee..1ffcbda54 100644 --- a/man/model-method-sample_mpi.Rd +++ b/man/model-method-sample_mpi.Rd @@ -316,6 +316,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}, \code{\link{model-method-variational}} diff --git a/man/model-method-variables.Rd b/man/model-method-variables.Rd index b5b85f1c2..dc80ed9a6 100644 --- a/man/model-method-variables.Rd +++ b/man/model-method-variables.Rd @@ -45,6 +45,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variational}} diff --git a/man/model-method-variational.Rd b/man/model-method-variational.Rd index b04f8b7c1..d2485320e 100644 --- a/man/model-method-variational.Rd +++ b/man/model-method-variational.Rd @@ -314,6 +314,7 @@ Other CmdStanModel methods: \code{\link{model-method-generate-quantities}}, \code{\link{model-method-laplace}}, \code{\link{model-method-optimize}}, +\code{\link{model-method-pathfinder}}, \code{\link{model-method-sample_mpi}}, \code{\link{model-method-sample}}, \code{\link{model-method-variables}}