Skip to content

Commit

Permalink
Merge branch '29-brms-style-stancode-method-for-readability' into main
Browse files Browse the repository at this point in the history
Merge branch '29-brms-style-stancode-method-for-readability'

# Conflicts:
#	NAMESPACE
#	tests/testthat/test-coev_fit.R
  • Loading branch information
Scott Claessens authored and Scott Claessens committed Aug 8, 2024
2 parents 8cc0b6a + a951a71 commit 7effc8c
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: coevolve
Title: Fit Bayesian Dynamic Coevolutionary Models using 'Stan'
Version: 0.0.0.9010
Version: 0.0.0.9011
Authors@R:
c(person("Scott", "Claessens", , "scott.claessens@gmail.com",
role = c("aut", "cre"), comment = c(ORCID = "0000-0002-3562-6981")),
Expand Down
4 changes: 4 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
S3method(extract_samples,coevfit)
S3method(print,coevfit)
S3method(print,coevsummary)
S3method(stancode,coevfit)
S3method(standata,coevfit)
S3method(summary,coevfit)
export(coev_calculate_delta_theta)
export(coev_calculate_theta)
Expand All @@ -15,4 +17,6 @@ export(coev_plot_predictive_check)
export(coev_plot_selection_gradient)
export(coev_simulate_coevolution)
export(extract_samples)
export(stancode)
export(standata)
importFrom(rlang,.data)
14 changes: 14 additions & 0 deletions R/stancode.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#' Expose the Stan code of a fitted model represented by a \code{coevfit} object
#'
#' @param object An object of class \code{coevfit}.
#'
#' @return Printed Stan code
#' @export
stancode <- function(object){
UseMethod("stancode")
}

#' @export
stancode.coevfit <- function(object){
cat(object$stan_code)
}
15 changes: 15 additions & 0 deletions R/standata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' Expose the Stan data list for a fitted model represented by a \code{coevfit}
#' object
#'
#' @param object An object of class \code{coevfit}.
#'
#' @return Named list of data for Stan
#' @export
standata <- function(object){
UseMethod("standata")
}

#' @export
standata.coevfit <- function(object){
object$stan_data
}
17 changes: 17 additions & 0 deletions man/stancode.Rd

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

19 changes: 19 additions & 0 deletions man/standata.Rd

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

32 changes: 32 additions & 0 deletions tests/testthat/test-coev_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,23 @@ test_that("coev_fit() fits the model without error", {
expect_true(SW(methods::is(extract_samples(m2), "list")))
expect_true(SW(methods::is(extract_samples(m3), "list")))
expect_true(SW(methods::is(extract_samples(m4), "list")))
# expect no error for stancode and standata methods
expect_no_error(SW(stancode(m1)))
expect_no_error(SW(stancode(m2)))
expect_no_error(SW(stancode(m3)))
expect_no_error(SW(stancode(m4)))
expect_no_error(SW(standata(m1)))
expect_no_error(SW(standata(m2)))
expect_no_error(SW(standata(m3)))
expect_no_error(SW(standata(m4)))
expect_output(SW(stancode(m1)))
expect_output(SW(stancode(m2)))
expect_output(SW(stancode(m3)))
expect_output(SW(stancode(m4)))
expect_true(SW(methods::is(standata(m1), "list")))
expect_true(SW(methods::is(standata(m2), "list")))
expect_true(SW(methods::is(standata(m3), "list")))
expect_true(SW(methods::is(standata(m4), "list")))
})

test_that("effects_mat argument to coev_fit() works as expected", {
Expand All @@ -552,6 +569,11 @@ test_that("effects_mat argument to coev_fit() works as expected", {
# expect no errors for extract_samples method
expect_no_error(SW(extract_samples(m)))
expect_true(SW(methods::is(extract_samples(m), "list")))
# expect no errors for stancode or standata methods
expect_no_error(SW(stancode(m)))
expect_no_error(SW(standata(m)))
expect_output(SW(stancode(m)))
expect_true(SW(methods::is(standata(m), "list")))
# expect effects_mat correct in model output
effects_mat <- matrix(
c(TRUE, TRUE,
Expand Down Expand Up @@ -586,6 +608,11 @@ test_that("coev_fit() works with missing data", {
# expect no errors for extract_samples method
expect_no_error(SW(extract_samples(m)))
expect_true(SW(methods::is(extract_samples(m), "list")))
# expect no errors for stancode or standata methods
expect_no_error(SW(stancode(m)))
expect_no_error(SW(standata(m)))
expect_output(SW(stancode(m)))
expect_true(SW(methods::is(standata(m), "list")))
# expect warning in summary output
capture.output(
SW(
Expand All @@ -612,4 +639,9 @@ test_that("coev_fit() works with repeated observations", {
expect_no_error(SW(summary(m)))
expect_output(SW(print(m)))
expect_output(SW(print(summary(m))))
# expect no errors for stancode or standata methods
expect_no_error(SW(stancode(m)))
expect_no_error(SW(standata(m)))
expect_output(SW(stancode(m)))
expect_true(SW(methods::is(standata(m), "list")))
})

0 comments on commit 7effc8c

Please sign in to comment.