to_posterior <- function(model, data) { sampling(model, data = data, chains = 1, iter = 1, refresh = 0, algorithm = "Fixed_param") } unconstrain_sam <- function(model, data, pos_draws){ ### #' recover the log density based on the posterior draws #' #' output: n by n_sam matrix with n the number of parameters ### posterior <- to_posterior(model, data) par_template <- get_inits(posterior)[[1]] # get a template of parameters npar <- length(par_template) # number of pars n_inits <- sapply(par_template, length) fn <- function(theta){ j = 1 for(i in 1:npar){ par_template[[i]] <- theta[j:(j + n_inits[i] - 1)] j = j + n_inits[i] } unconstrain_pars(posterior, par_template) } apply(sapply(pos_draws[[1]], unlist), 1, fn) }