Skip to content

Commit

Permalink
function for rethinking style posterior list
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikRingen committed Aug 7, 2024
1 parent 8df4553 commit 106cc38
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ S3method(print,coevsummary)
S3method(summary,coevfit)
export(coev_calculate_delta_theta)
export(coev_calculate_theta)
export(coev_extract_samples)
export(coev_fit)
export(coev_make_stancode)
export(coev_make_standata)
Expand Down
32 changes: 32 additions & 0 deletions R/coev_extract_samples.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#' Extract samples (draws) from a fitted \code{coevfit} object
#'
#' @param object An object of class \code{coevfit}
#'
#' @return Samples in 'rethinking' style list format
#' @export
#'
coev_extract_samples <- function(object) {
# stop if object is not of class coevfit
if (!methods::is(object, "coevfit")) {
stop2(
paste0(
"Argument 'object' must be a fitted coevolutionary model ",
"of class coevfit."
)
)
}
# stop if rethinking package not installed
if (!requireNamespace("rethinking", quietly = TRUE)) {
stop2(
paste0(
"'rethinking' package must be installed to use this function. See https://github.com/rmcelreath/rethinking"
)
)
}
vars <- object$fit$metadata()$stan_variables
draws <- posterior::as_draws_rvars(object$fit$draws())

return(lapply(vars, \(var_name){
posterior::draws_of(draws[[var_name]], with_chains = FALSE)
}) |> setNames(vars))
}
17 changes: 17 additions & 0 deletions man/coev_extract_samples.Rd

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

0 comments on commit 106cc38

Please sign in to comment.