Skip to content

Commit cbb570a

Browse files
authored
Merge pull request #794 from stan-dev/next-release
cmdstanr v0.6.0
2 parents abe87ba + 7d15ab4 commit cbb570a

File tree

149 files changed

+13456
-14988
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+13456
-14988
lines changed

.Rbuildignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
^docs$
88
^pkgdown$
99
^man-roxygen$
10-
^dev-helpers\.R$
1110
^LICENSE\.md$
1211
^\.appveyor\.yml$
1312
^\.github$
1413
^vignettes/articles-online-only$
14+
^release-prep\.R$

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
.Ruserdata
55
.vscode/*
66

7-
design-questions/*
87
inst/doc
98
dev-helpers.R
9+
release-prep.R

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: cmdstanr
22
Title: R Interface to 'CmdStan'
3-
Version: 0.5.3
4-
Date: 2022-04-24
3+
Version: 0.6.0
4+
Date: 2023-07-25
55
Authors@R:
66
c(person(given = "Jonah", family = "Gabry", role = c("aut", "cre"),
77
email = "[email protected]"),

NEWS.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,37 @@
1+
# cmdstanr 0.6.0
2+
3+
### Major new features
4+
5+
* New `expose_functions()` method to expose Stan functions to R by @andrjohns in #702. See `?expose_functions`.
6+
* New methods for accessing log_prob, grad_log_prob, hessian, un/constrain variables by @andrjohns in #701. See `?init_model_methods`.
7+
8+
### Other changes
9+
10+
* mod$variables works w includes in precompile state (fix #680) by @MKyhos in #682
11+
* Update broken link for Stan OpenCL support page by @erictleung in #686
12+
* Add newline to check syntax output by @rok-cesnovar in #689
13+
* Allow exposing functions without sampling by @andrjohns in #705
14+
* Expose skeleton by @andrjohns in #706
15+
* WSL - Run cmdstan and models under WSL filesystem by @andrjohns in #696
16+
* Bugfix - Deep copy method/function environments by @andrjohns in #709
17+
* Add option for including jacobian adjustments in hessian method by @andrjohns in #710
18+
* WSL Optimisations and Bugfixes for CI by @andrjohns in #711
19+
* add stancflags from make/local by @rok-cesnovar in #690
20+
* Update co-authors by @andrjohns in #715
21+
* Update model methods parameter naming and extract skeleton function by @andrjohns in #724
22+
* Add method for unconstraining all parameter draws by @andrjohns in #729
23+
* Improve efficiency of variable matching by @sbfnk in #736
24+
* Add verbosity to download output and errors by @andrjohns in #745
25+
* Update handling of show_messages, add show_exceptions by @andrjohns in #746
26+
* Rtools43 support by @andrjohns in #755
27+
* Add stanc M1 make patch, suppress boost warnings by @andrjohns in #756
28+
* more examples of summary method by @gravesti in #751
29+
* Fix model$format and model$check_syntax for compiled models with include-paths by @adrian-lison in #775
30+
* Generalise RTools config/support by @andrjohns in #777
31+
* New posterior vignette by @gravesti in #719
32+
* Add moment-matching support to $loo() method by @andrjohns in #778
33+
* replace \ with function by @jsocolar in #789
34+
135
# cmdstanr 0.5.3
236

337
### New features

R/csv.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ read_cmdstan_csv <- function(files,
422422
#' Read CmdStan CSV files from sampling into \R
423423
#'
424424
#' Deprecated. Use [read_cmdstan_csv()] instead.
425-
#'
425+
#' @keywords internal
426426
#' @export
427427
#' @param files,variables,sampler_diagnostics Deprecated. Use
428428
#' [read_cmdstan_csv()] instead.

R/example.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ write_stan_file <- function(code,
188188
#' Write Stan code to a temporary file
189189
#'
190190
#' This function is deprecated. Please use [write_stan_file()] instead.
191-
#'
191+
#' @keywords internal
192192
#' @export
193193
#' @inheritParams write_stan_file
194194
write_stan_tempfile <- function(code, dir = tempdir()) {

R/fit.R

Lines changed: 105 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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() {
301301
CmdStanFit$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
#'
321330
init_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+
#'
364377
log_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+
#'
398415
grad_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+
#'
432453
hessian <- 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+
#'
463488
unconstrain_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+
#'
524553
unconstrain_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
}
569599
CmdStanFit$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+
#'
590624
variable_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+
#'
623662
constrain_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+
#'
12361289
CmdStanMCMC <- 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+
#'
17211788
CmdStanMLE <- 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+
#'
18241905
CmdStanVB <- R6::R6Class(
18251906
classname = "CmdStanVB",
18261907
inherit = CmdStanFit,

0 commit comments

Comments
 (0)