@@ -82,7 +82,7 @@ CmdStanFit <- R6::R6Class(
82
82
invisible (self )
83
83
},
84
84
expose_functions = function (global = FALSE , verbose = FALSE ) {
85
- expose_functions (self $ functions , global , verbose )
85
+ expose_stan_functions (self $ functions , global , verbose )
86
86
invisible (NULL )
87
87
}
88
88
),
@@ -301,12 +301,18 @@ init <- function() {
301
301
CmdStanFit $ set(" public" , name = " init" , value = init )
302
302
303
303
# ' 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.
305
305
# '
306
306
# ' @name fit-method-init_model_methods
307
307
# ' @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.
310
316
# '
311
317
# ' @param seed (integer) The random seed to use when initializing the model.
312
318
# ' @param verbose (boolean) Whether to show verbose logging during compilation.
@@ -317,6 +323,9 @@ CmdStanFit$set("public", name = "init", value = init)
317
323
# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
318
324
# ' fit_mcmc$init_model_methods()
319
325
# ' }
326
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
327
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
328
+ # ' [hessian()]
320
329
# '
321
330
init_model_methods <- function (seed = 0 , verbose = FALSE , hessian = FALSE ) {
322
331
if (os_is_wsl()) {
@@ -358,9 +367,13 @@ CmdStanFit$set("public", name = "init_model_methods", value = init_model_methods
358
367
# ' \dontrun{
359
368
# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
360
369
# ' 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))
362
371
# ' }
363
372
# '
373
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
374
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
375
+ # ' [hessian()]
376
+ # '
364
377
log_prob <- function (unconstrained_variables , jacobian_adjustment = TRUE ) {
365
378
if (is.null(private $ model_methods_env_ $ model_ptr )) {
366
379
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)
383
396
# ' @description The `$grad_log_prob()` method provides access to the
384
397
# ' Stan model's `log_prob` function and its derivative
385
398
# '
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.
390
403
# '
391
404
# ' @examples
392
405
# ' \dontrun{
393
406
# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
394
407
# ' 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))
396
409
# ' }
397
410
# '
411
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
412
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
413
+ # ' [hessian()]
414
+ # '
398
415
grad_log_prob <- function (unconstrained_variables , jacobian_adjustment = TRUE ) {
399
416
if (is.null(private $ model_methods_env_ $ model_ptr )) {
400
417
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)
417
434
# ' @description The `$hessian()` method provides access to the
418
435
# ' Stan model's `log_prob`, its derivative, and its hessian
419
436
# '
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.
424
441
# '
425
442
# ' @examples
426
443
# ' \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))
430
447
# ' }
431
448
# '
449
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
450
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
451
+ # ' [hessian()]
452
+ # '
432
453
hessian <- function (unconstrained_variables , jacobian_adjustment = TRUE ) {
433
454
if (is.null(private $ model_methods_env_ $ model_ptr )) {
434
455
stop(" The method has not been compiled, please call `init_model_methods()` first" ,
@@ -450,8 +471,8 @@ CmdStanFit$set("public", name = "hessian", value = hessian)
450
471
# ' @description The `$unconstrain_variables()` method transforms input parameters to
451
472
# ' the unconstrained scale
452
473
# '
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.
455
476
# '
456
477
# ' @examples
457
478
# ' \dontrun{
@@ -460,6 +481,10 @@ CmdStanFit$set("public", name = "hessian", value = hessian)
460
481
# ' fit_mcmc$unconstrain_variables(list(alpha = 0.5, beta = c(0.7, 1.1, 0.2)))
461
482
# ' }
462
483
# '
484
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
485
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
486
+ # ' [hessian()]
487
+ # '
463
488
unconstrain_variables <- function (variables ) {
464
489
if (is.null(private $ model_methods_env_ $ model_ptr )) {
465
490
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
521
546
# ' unconstrained_draws <- fit_mcmc$unconstrain_draws(draws = fit_mcmc$draws())
522
547
# ' }
523
548
# '
549
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
550
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
551
+ # ' [hessian()]
552
+ # '
524
553
unconstrain_draws <- function (files = NULL , draws = NULL ) {
525
554
if (! is.null(files ) || ! is.null(draws )) {
526
555
if (! is.null(files ) && ! is.null(draws )) {
@@ -565,6 +594,7 @@ unconstrain_draws <- function(files = NULL, draws = NULL) {
565
594
self $ unconstrain_variables(variables = par_list )
566
595
})
567
596
})
597
+ unconstrained
568
598
}
569
599
CmdStanFit $ set(" public" , name = " unconstrain_draws" , value = unconstrain_draws )
570
600
@@ -587,6 +617,10 @@ CmdStanFit$set("public", name = "unconstrain_draws", value = unconstrain_draws)
587
617
# ' fit_mcmc$variable_skeleton()
588
618
# ' }
589
619
# '
620
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
621
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
622
+ # ' [hessian()]
623
+ # '
590
624
variable_skeleton <- function (transformed_parameters = TRUE , generated_quantities = TRUE ) {
591
625
if (is.null(private $ model_methods_env_ $ model_ptr )) {
592
626
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)
607
641
# ' @description The `$constrain_variables()` method transforms input parameters to
608
642
# ' the constrained scale
609
643
# '
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).
613
648
# ' @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).
615
650
# '
616
651
# ' @examples
617
652
# ' \dontrun{
618
653
# ' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
619
654
# ' 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))
621
656
# ' }
622
657
# '
658
+ # ' @seealso [log_prob()], [grad_log_prob()], [constrain_variables()],
659
+ # ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
660
+ # ' [hessian()]
661
+ # '
623
662
constrain_variables <- function (unconstrained_variables , transformed_parameters = TRUE ,
624
663
generated_quantities = TRUE ) {
625
664
if (is.null(private $ model_methods_env_ $ model_ptr )) {
@@ -1233,6 +1272,20 @@ CmdStanFit$set("public", name = "code", value = code)
1233
1272
# ' [`$time()`][fit-method-time] | Report total and chain-specific run times. |
1234
1273
# ' [`$return_codes()`][fit-method-return_codes] | Return the return codes from the CmdStan runs. |
1235
1274
# '
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
+ # '
1236
1289
CmdStanMCMC <- R6 :: R6Class(
1237
1290
classname = " CmdStanMCMC" ,
1238
1291
inherit = CmdStanFit ,
@@ -1718,6 +1771,20 @@ CmdStanMCMC$set("public", name = "num_chains", value = num_chains)
1718
1771
# ' [`$output()`][fit-method-output] | Pretty print the output that was printed to the console. |
1719
1772
# ' [`$return_codes()`][fit-method-return_codes] | Return the return codes from the CmdStan runs. |
1720
1773
# '
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
+ # '
1721
1788
CmdStanMLE <- R6 :: R6Class(
1722
1789
classname = " CmdStanMLE" ,
1723
1790
inherit = CmdStanFit ,
@@ -1821,6 +1888,20 @@ CmdStanMLE$set("public", name = "mle", value = mle)
1821
1888
# ' [`$output()`][fit-method-output] | Pretty print the output that was printed to the console. |
1822
1889
# ' [`$return_codes()`][fit-method-return_codes] | Return the return codes from the CmdStan runs. |
1823
1890
# '
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
+ # '
1824
1905
CmdStanVB <- R6 :: R6Class(
1825
1906
classname = " CmdStanVB" ,
1826
1907
inherit = CmdStanFit ,
0 commit comments