From fd0e063fdfaadeb11ccc61a2540ef63a9b29eb4a Mon Sep 17 00:00:00 2001 From: erik-ringen Date: Wed, 7 Aug 2024 15:33:21 -0400 Subject: [PATCH] Make extract_samples a method of coevfit --- DESCRIPTION | 2 +- NAMESPACE | 3 +- R/coev_extract_samples.R | 32 ------------------- R/extract_samples.R | 19 +++++++++++ ..._extract_samples.Rd => extract_samples.Rd} | 8 ++--- 5 files changed, 26 insertions(+), 38 deletions(-) delete mode 100644 R/coev_extract_samples.R create mode 100644 R/extract_samples.R rename man/{coev_extract_samples.Rd => extract_samples.Rd} (68%) diff --git a/DESCRIPTION b/DESCRIPTION index 2826b22..4671869 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -16,7 +16,7 @@ URL: https://github.com/ScottClaessens/coevolve BugReports: https://github.com/ScottClaessens/coevolve/issues Encoding: UTF-8 Roxygen: list(markdown = TRUE) -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.1 Suggests: knitr, rmarkdown, diff --git a/NAMESPACE b/NAMESPACE index 94c0eb3..06f3597 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,11 +1,11 @@ # Generated by roxygen2: do not edit by hand +S3method(extract_samples,coevfit) S3method(print,coevfit) 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) @@ -14,4 +14,5 @@ export(coev_plot_flowfield) export(coev_plot_predictive_check) export(coev_plot_selection_gradient) export(coev_simulate_coevolution) +export(extract_samples) importFrom(rlang,.data) diff --git a/R/coev_extract_samples.R b/R/coev_extract_samples.R deleted file mode 100644 index 281e6df..0000000 --- a/R/coev_extract_samples.R +++ /dev/null @@ -1,32 +0,0 @@ -#' 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)) -} diff --git a/R/extract_samples.R b/R/extract_samples.R new file mode 100644 index 0000000..570a92e --- /dev/null +++ b/R/extract_samples.R @@ -0,0 +1,19 @@ +#' 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 +extract_samples <- function(object) { + UseMethod("extract_samples") +} + +#' @export +extract_samples.coevfit <- function(object) { + 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)) +} \ No newline at end of file diff --git a/man/coev_extract_samples.Rd b/man/extract_samples.Rd similarity index 68% rename from man/coev_extract_samples.Rd rename to man/extract_samples.Rd index 3ff2d31..166905f 100644 --- a/man/coev_extract_samples.Rd +++ b/man/extract_samples.Rd @@ -1,10 +1,10 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/coev_extract_samples.R -\name{coev_extract_samples} -\alias{coev_extract_samples} +% Please edit documentation in R/extract_samples.R +\name{extract_samples} +\alias{extract_samples} \title{Extract samples (draws) from a fitted \code{coevfit} object} \usage{ -coev_extract_samples(object) +extract_samples(object) } \arguments{ \item{object}{An object of class \code{coevfit}}