Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose skeleton #706

Merged
merged 5 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions R/fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -464,18 +464,30 @@ CmdStanFit$set("public", name = "unconstrain_pars", value = unconstrain_pars)
#' implied by newly-constrained parameters (defaults to TRUE)
#' @param generated_quantities (boolean) Whether to return generated quantities
#' implied by newly-constrained parameters (defaults to TRUE)
#' @param skeleton_only (boolean) Whether to return only the "skeleton" needed by the
#' utils::relist function (defaults to FALSE)
#'
#' @examples
#' \dontrun{
#' fit_mcmc <- cmdstanr_example("logistic", method = "sample")
#' fit_mcmc$constrain_pars(upars = c(0.5, 1.2, 1.1, 2.2, 1.1))
#' }
#'
constrain_pars <- function(upars, transformed_parameters = TRUE, generated_quantities = TRUE) {
constrain_pars <- function(upars, transformed_parameters = TRUE, generated_quantities = TRUE,
skeleton_only = FALSE) {
if (is.null(private$model_methods_env_$model_ptr)) {
stop("The method has not been compiled, please call `init_model_methods()` first",
call. = FALSE)
}

skeleton <- create_skeleton(private$model_methods_env_$param_metadata_,
self$runset$args$model_variables,
transformed_parameters,
generated_quantities)
if (skeleton_only) {
return(skeleton)
}

if (length(upars) != private$model_methods_env_$num_upars_) {
stop("Model has ", private$model_methods_env_$num_upars_, " unconstrained parameter(s), but ",
length(upars), " were provided!", call. = FALSE)
Expand All @@ -484,11 +496,6 @@ constrain_pars <- function(upars, transformed_parameters = TRUE, generated_quant
private$model_methods_env_$model_ptr_,
private$model_methods_env_$model_rng_,
upars, transformed_parameters, generated_quantities)

skeleton <- create_skeleton(private$model_methods_env_$param_metadata_,
self$runset$args$model_variables,
transformed_parameters,
generated_quantities)
utils::relist(cpars, skeleton)
}
CmdStanFit$set("public", name = "constrain_pars", value = constrain_pars)
Expand Down
6 changes: 5 additions & 1 deletion man/fit-method-constrain_pars.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions tests/testthat/test-model-methods.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,14 @@ test_that("Methods return correct values", {
expect_equal(fit$constrain_pars(c(0.1), generated_quantities = FALSE),
list(theta = 0.52497918747894001257))

skeleton <- list(
theta = array(0, dim = 1),
log_lik = array(0, dim = data_list$N)
)

expect_equal(fit$constrain_pars(skeleton_only = TRUE),
skeleton)

upars <- fit$unconstrain_pars(cpars)
expect_equal(upars, c(0.1))
})
Expand Down