@@ -82,7 +82,7 @@ CmdStanFit <- R6::R6Class(
8282 invisible (self )
8383 },
8484 expose_functions = function (global = FALSE , verbose = FALSE ) {
85- expose_functions (self $ functions , global , verbose )
85+ expose_stan_functions (self $ functions , global , verbose )
8686 invisible (NULL )
8787 }
8888 ),
@@ -301,12 +301,18 @@ init <- function() {
301301CmdStanFit $ set(" public" , name = " init" , value = init )
302302
303303# ' Compile additional methods for accessing the model log-probability function
304- # ' and parameter constraining and unconstraining. This requires the `Rcpp` package.
304+ # ' and parameter constraining and unconstraining.
305305# '
306306# ' @name fit-method-init_model_methods
307307# ' @aliases init_model_methods
308- # ' @description The `$init_model_methods()` compiles and initializes the
309- # ' `log_prob`, `grad_log_prob`, `constrain_variables`, and `unconstrain_variables` functions.
308+ # '
309+ # ' @description The `$init_model_methods()` method compiles and initializes the
310+ # ' `log_prob`, `grad_log_prob`, `constrain_variables`, `unconstrain_variables`
311+ # ' and `unconstrain_draws` functions. These are then available as methods of
312+ # ' the fitted model object. This requires the `Rcpp` package.
313+ # '
314+ # ' Note: there may be many compiler warnings emitted during compilation but
315+ # ' these can be ignored so long as they are warnings and not errors.
310316# '
311317# ' @param seed (integer) The random seed to use when initializing the model.
312318# ' @param verbose (boolean) Whether to show verbose logging during compilation.
@@ -317,6 +323,9 @@ CmdStanFit$set("public", name = "init", value = init)
317323# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
318324# ' fit_mcmc$init_model_methods()
319325# ' }
326+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
327+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
328+ # ' [hessian()]
320329# '
321330init_model_methods <- function (seed = 0 , verbose = FALSE , hessian = FALSE ) {
322331 if (os_is_wsl()) {
@@ -358,9 +367,13 @@ CmdStanFit$set("public", name = "init_model_methods", value = init_model_methods
358367# ' \dontrun{
359368# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
360369# ' fit_mcmc$init_model_methods()
361- # ' fit_mcmc$log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2, 1.1 ))
370+ # ' fit_mcmc$log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
362371# ' }
363372# '
373+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
374+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
375+ # ' [hessian()]
376+ # '
364377log_prob <- function (unconstrained_variables , jacobian_adjustment = TRUE ) {
365378 if (is.null(private $ model_methods_env_ $ model_ptr )) {
366379 stop(" The method has not been compiled, please call `init_model_methods()` first" ,
@@ -383,18 +396,22 @@ CmdStanFit$set("public", name = "log_prob", value = log_prob)
383396# ' @description The `$grad_log_prob()` method provides access to the
384397# ' Stan model's `log_prob` function and its derivative
385398# '
386- # ' @param unconstrained_variables (numeric) A vector of unconstrained parameters to be passed
387- # ' to `grad_log_prob`
388- # ' @param jacobian_adjustment (bool) Whether to include the log-density adjustments from
389- # ' un/constraining variables
399+ # ' @param unconstrained_variables (numeric) A vector of unconstrained parameters
400+ # ' to be passed to `grad_log_prob`.
401+ # ' @param jacobian_adjustment (bool) Whether to include the log-density
402+ # ' adjustments from un/constraining variables.
390403# '
391404# ' @examples
392405# ' \dontrun{
393406# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
394407# ' fit_mcmc$init_model_methods()
395- # ' fit_mcmc$grad_log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2, 1.1 ))
408+ # ' fit_mcmc$grad_log_prob(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
396409# ' }
397410# '
411+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
412+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
413+ # ' [hessian()]
414+ # '
398415grad_log_prob <- function (unconstrained_variables , jacobian_adjustment = TRUE ) {
399416 if (is.null(private $ model_methods_env_ $ model_ptr )) {
400417 stop(" The method has not been compiled, please call `init_model_methods()` first" ,
@@ -417,18 +434,22 @@ CmdStanFit$set("public", name = "grad_log_prob", value = grad_log_prob)
417434# ' @description The `$hessian()` method provides access to the
418435# ' Stan model's `log_prob`, its derivative, and its hessian
419436# '
420- # ' @param unconstrained_variables (numeric) A vector of unconstrained parameters to be passed
421- # ' to `hessian`
422- # ' @param jacobian_adjustment (bool) Whether to include the log-density adjustments from
423- # ' un/constraining variables
437+ # ' @param unconstrained_variables (numeric) A vector of unconstrained parameters
438+ # ' to be passed to `hessian`.
439+ # ' @param jacobian_adjustment (bool) Whether to include the log-density
440+ # ' adjustments from un/constraining variables.
424441# '
425442# ' @examples
426443# ' \dontrun{
427- # ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
428- # ' fit_mcmc$init_model_methods()
429- # ' fit_mcmc$hessian(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2, 1.1 ))
444+ # ' # fit_mcmc <- cmdstanr_example("logistic", method = "sample")
445+ # ' # fit_mcmc$init_model_methods(hessian = TRUE )
446+ # ' # fit_mcmc$hessian(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
430447# ' }
431448# '
449+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
450+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
451+ # ' [hessian()]
452+ # '
432453hessian <- function (unconstrained_variables , jacobian_adjustment = TRUE ) {
433454 if (is.null(private $ model_methods_env_ $ model_ptr )) {
434455 stop(" The method has not been compiled, please call `init_model_methods()` first" ,
@@ -450,8 +471,8 @@ CmdStanFit$set("public", name = "hessian", value = hessian)
450471# ' @description The `$unconstrain_variables()` method transforms input parameters to
451472# ' the unconstrained scale
452473# '
453- # ' @param variables (list) A list of parameter values to transform, in the same format as
454- # ' provided to the `init` argument of the `$sample()` method
474+ # ' @param variables (list) A list of parameter values to transform, in the same
475+ # ' format as provided to the `init` argument of the `$sample()` method.
455476# '
456477# ' @examples
457478# ' \dontrun{
@@ -460,6 +481,10 @@ CmdStanFit$set("public", name = "hessian", value = hessian)
460481# ' fit_mcmc$unconstrain_variables(list(alpha = 0.5, beta = c(0.7, 1.1, 0.2)))
461482# ' }
462483# '
484+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
485+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
486+ # ' [hessian()]
487+ # '
463488unconstrain_variables <- function (variables ) {
464489 if (is.null(private $ model_methods_env_ $ model_ptr )) {
465490 stop(" The method has not been compiled, please call `init_model_methods()` first" ,
@@ -521,6 +546,10 @@ CmdStanFit$set("public", name = "unconstrain_variables", value = unconstrain_var
521546# ' unconstrained_draws <- fit_mcmc$unconstrain_draws(draws = fit_mcmc$draws())
522547# ' }
523548# '
549+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
550+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
551+ # ' [hessian()]
552+ # '
524553unconstrain_draws <- function (files = NULL , draws = NULL ) {
525554 if (! is.null(files ) || ! is.null(draws )) {
526555 if (! is.null(files ) && ! is.null(draws )) {
@@ -565,6 +594,7 @@ unconstrain_draws <- function(files = NULL, draws = NULL) {
565594 self $ unconstrain_variables(variables = par_list )
566595 })
567596 })
597+ unconstrained
568598}
569599CmdStanFit $ set(" public" , name = " unconstrain_draws" , value = unconstrain_draws )
570600
@@ -587,6 +617,10 @@ CmdStanFit$set("public", name = "unconstrain_draws", value = unconstrain_draws)
587617# ' fit_mcmc$variable_skeleton()
588618# ' }
589619# '
620+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
621+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
622+ # ' [hessian()]
623+ # '
590624variable_skeleton <- function (transformed_parameters = TRUE , generated_quantities = TRUE ) {
591625 if (is.null(private $ model_methods_env_ $ model_ptr )) {
592626 stop(" The method has not been compiled, please call `init_model_methods()` first" ,
@@ -607,19 +641,24 @@ CmdStanFit$set("public", name = "variable_skeleton", value = variable_skeleton)
607641# ' @description The `$constrain_variables()` method transforms input parameters to
608642# ' the constrained scale
609643# '
610- # ' @param unconstrained_variables (numeric) A vector of unconstrained parameters to constrain
611- # ' @param transformed_parameters (boolean) Whether to return transformed parameters
612- # ' implied by newly-constrained parameters (defaults to TRUE)
644+ # ' @param unconstrained_variables (numeric) A vector of unconstrained parameters
645+ # ' to constrain.
646+ # ' @param transformed_parameters (boolean) Whether to return transformed
647+ # ' parameters implied by newly-constrained parameters (defaults to TRUE).
613648# ' @param generated_quantities (boolean) Whether to return generated quantities
614- # ' implied by newly-constrained parameters (defaults to TRUE)
649+ # ' implied by newly-constrained parameters (defaults to TRUE).
615650# '
616651# ' @examples
617652# ' \dontrun{
618653# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
619654# ' fit_mcmc$init_model_methods()
620- # ' fit_mcmc$constrain_variables(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2, 1.1 ))
655+ # ' fit_mcmc$constrain_variables(unconstrained_variables = c(0.5, 1.2, 1.1, 2.2))
621656# ' }
622657# '
658+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
659+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
660+ # ' [hessian()]
661+ # '
623662constrain_variables <- function (unconstrained_variables , transformed_parameters = TRUE ,
624663 generated_quantities = TRUE ) {
625664 if (is.null(private $ model_methods_env_ $ model_ptr )) {
@@ -1233,6 +1272,20 @@ CmdStanFit$set("public", name = "code", value = code)
12331272# ' [`$time()`][fit-method-time] | Report total and chain-specific run times. |
12341273# ' [`$return_codes()`][fit-method-return_codes] | Return the return codes from the CmdStan runs. |
12351274# '
1275+ # ' ## Expose Stan functions and additional methods to R
1276+ # '
1277+ # ' |**Method**|**Description**|
1278+ # ' |:----------|:---------------|
1279+ # ' [`$expose_functions()`][fit-method-expose_functions] | Expose Stan functions for use in R. |
1280+ # ' [`$init_model_methods()`][fit-method-init_model_methods] | Expose methods for log-probability, gradients, parameter constraining and unconstraining. |
1281+ # ' [`$log_prob()`][fit-method-log_prob] | Calculate log-prob. |
1282+ # ' [`$grad_log_prob()`][fit-method-grad_log_prob] | Calculate log-prob and gradient. |
1283+ # ' [`$hessian()`][fit-method-hessian] | Calculate log-prob, gradient, and hessian. |
1284+ # ' [`$constrain_variables()`][fit-method-constrain_variables] | Transform a set of unconstrained parameter values to the constrained scale. |
1285+ # ' [`$unconstrain_variables()`][fit-method-unconstrain_variables] | Transform a set of parameter values to the unconstrained scale. |
1286+ # ' [`$unconstrain_draws()`][fit-method-unconstrain_draws] | Transform all parameter draws to the unconstrained scale. |
1287+ # ' [`$variable_skeleton()`][fit-method-variable_skeleton] | Helper function to re-structure a vector of constrained parameter values. |
1288+ # '
12361289CmdStanMCMC <- R6 :: R6Class(
12371290 classname = " CmdStanMCMC" ,
12381291 inherit = CmdStanFit ,
@@ -1718,6 +1771,20 @@ CmdStanMCMC$set("public", name = "num_chains", value = num_chains)
17181771# ' [`$output()`][fit-method-output] | Pretty print the output that was printed to the console. |
17191772# ' [`$return_codes()`][fit-method-return_codes] | Return the return codes from the CmdStan runs. |
17201773# '
1774+ # ' ## Expose Stan functions and additional methods to R
1775+ # '
1776+ # ' |**Method**|**Description**|
1777+ # ' |:----------|:---------------|
1778+ # ' [`$expose_functions()`][fit-method-expose_functions] | Expose Stan functions for use in R. |
1779+ # ' [`$init_model_methods()`][fit-method-init_model_methods] | Expose methods for log-probability, gradients, parameter constraining and unconstraining. |
1780+ # ' [`$log_prob()`][fit-method-log_prob] | Calculate log-prob. |
1781+ # ' [`$grad_log_prob()`][fit-method-grad_log_prob] | Calculate log-prob and gradient. |
1782+ # ' [`$hessian()`][fit-method-hessian] | Calculate log-prob, gradient, and hessian. |
1783+ # ' [`$constrain_variables()`][fit-method-constrain_variables] | Transform a set of unconstrained parameter values to the constrained scale. |
1784+ # ' [`$unconstrain_variables()`][fit-method-unconstrain_variables] | Transform a set of parameter values to the unconstrained scale. |
1785+ # ' [`$unconstrain_draws()`][fit-method-unconstrain_draws] | Transform all parameter draws to the unconstrained scale. |
1786+ # ' [`$variable_skeleton()`][fit-method-variable_skeleton] | Helper function to re-structure a vector of constrained parameter values. |
1787+ # '
17211788CmdStanMLE <- R6 :: R6Class(
17221789 classname = " CmdStanMLE" ,
17231790 inherit = CmdStanFit ,
@@ -1821,6 +1888,20 @@ CmdStanMLE$set("public", name = "mle", value = mle)
18211888# ' [`$output()`][fit-method-output] | Pretty print the output that was printed to the console. |
18221889# ' [`$return_codes()`][fit-method-return_codes] | Return the return codes from the CmdStan runs. |
18231890# '
1891+ # ' ## Expose Stan functions and additional methods to R
1892+ # '
1893+ # ' |**Method**|**Description**|
1894+ # ' |:----------|:---------------|
1895+ # ' [`$expose_functions()`][fit-method-expose_functions] | Expose Stan functions for use in R. |
1896+ # ' [`$init_model_methods()`][fit-method-init_model_methods] | Expose methods for log-probability, gradients, parameter constraining and unconstraining. |
1897+ # ' [`$log_prob()`][fit-method-log_prob] | Calculate log-prob. |
1898+ # ' [`$grad_log_prob()`][fit-method-grad_log_prob] | Calculate log-prob and gradient. |
1899+ # ' [`$hessian()`][fit-method-hessian] | Calculate log-prob, gradient, and hessian. |
1900+ # ' [`$constrain_variables()`][fit-method-constrain_variables] | Transform a set of unconstrained parameter values to the constrained scale. |
1901+ # ' [`$unconstrain_variables()`][fit-method-unconstrain_variables] | Transform a set of parameter values to the unconstrained scale. |
1902+ # ' [`$unconstrain_draws()`][fit-method-unconstrain_draws] | Transform all parameter draws to the unconstrained scale. |
1903+ # ' [`$variable_skeleton()`][fit-method-variable_skeleton] | Helper function to re-structure a vector of constrained parameter values. |
1904+ # '
18241905CmdStanVB <- R6 :: R6Class(
18251906 classname = " CmdStanVB" ,
18261907 inherit = CmdStanFit ,
0 commit comments