@@ -542,6 +542,8 @@ CmdStanFit$set("public", name = "unconstrain_variables", value = unconstrain_var
542
542
# ' @param files (character vector) The paths to the CmdStan CSV files. These can
543
543
# ' be files generated by running CmdStanR or running CmdStan directly.
544
544
# ' @param draws A `posterior::draws_*` object.
545
+ # ' @param format (string) The format of the returned draws. Must be a valid
546
+ # ' format from the \pkg{posterior} package.
545
547
# '
546
548
# ' @examples
547
549
# ' \dontrun{
@@ -562,7 +564,8 @@ CmdStanFit$set("public", name = "unconstrain_variables", value = unconstrain_var
562
564
# ' [unconstrain_variables()], [unconstrain_draws()], [variable_skeleton()],
563
565
# ' [hessian()]
564
566
# '
565
- unconstrain_draws <- function (files = NULL , draws = NULL ) {
567
+ unconstrain_draws <- function (files = NULL , draws = NULL ,
568
+ format = getOption(" cmdstanr_draws_format" , " draws_array" )) {
566
569
if (! is.null(files ) || ! is.null(draws )) {
567
570
if (! is.null(files ) && ! is.null(draws )) {
568
571
stop(" Either a list of CSV files or a draws object can be passed, not both" ,
@@ -600,13 +603,16 @@ unconstrain_draws <- function(files = NULL, draws = NULL) {
600
603
skeleton <- self $ variable_skeleton(transformed_parameters = FALSE ,
601
604
generated_quantities = FALSE )
602
605
par_columns <- ! (names(draws ) %in% c(" .chain" , " .iteration" , " .draw" ))
603
- unconstrained <- lapply(split(draws , f = draws $ .chain ), function (chain ) {
604
- lapply(asplit(chain , 1 ), function (draw ) {
605
- par_list <- utils :: relist(as.numeric(draw [par_columns ]), skeleton )
606
- self $ unconstrain_variables(variables = par_list )
607
- })
606
+ meta_columns <- ! par_columns
607
+ unconstrained <- lapply(asplit(draws , 1 ), function (draw ) {
608
+ par_list <- utils :: relist(as.numeric(draw [par_columns ]), skeleton )
609
+ self $ unconstrain_variables(variables = par_list )
608
610
})
609
- unconstrained
611
+
612
+ unconstrained <- do.call(rbind.data.frame , unconstrained )
613
+ uncon_names <- private $ model_methods_env_ $ unconstrained_param_names(private $ model_methods_env_ $ model_ptr_ , FALSE , FALSE )
614
+ names(unconstrained ) <- repair_variable_names(uncon_names )
615
+ maybe_convert_draws_format(cbind.data.frame(unconstrained , draws [,meta_columns ]), format )
610
616
}
611
617
CmdStanFit $ set(" public" , name = " unconstrain_draws" , value = unconstrain_draws )
612
618
@@ -1546,7 +1552,7 @@ loo <- function(variables = "log_lik", r_eff = TRUE, moment_match = FALSE, ...)
1546
1552
loo = loo_result ,
1547
1553
post_draws = function (x , ... ) { x $ draws(format = " draws_matrix" ) },
1548
1554
log_lik_i = log_lik_i ,
1549
- unconstrain_pars = function (x , pars , ... ) { do.call( rbind , lapply( x $ unconstrain_draws(), function ( chain ) { do.call( rbind , chain ) }) ) },
1555
+ unconstrain_pars = function (x , pars , ... ) { x $ unconstrain_draws(format = " draws_matrix " ) },
1550
1556
log_prob_upars = function (x , upars , ... ) { apply(upars , 1 , x $ log_prob ) },
1551
1557
log_lik_i_upars = log_lik_i_upars ,
1552
1558
...
0 commit comments