Skip to content

Commit

Permalink
Coerce to data frame class
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Claessens authored and Scott Claessens committed Sep 8, 2024
1 parent 6421ba9 commit f631520
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Suggests:
knitr,
rmarkdown,
testthat (>= 3.0.0),
tibble,
withr
Config/testthat/edition: 3
Imports:
Expand Down
2 changes: 2 additions & 0 deletions R/coev_make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ coev_make_stancode <- function(data, variables, id, tree,
# check arguments
run_checks(data, variables, id, tree, effects_mat,
dist_mat, prior, scale, prior_only)
# coerce data argument to data frame
data <- as.data.frame(data)
# extract distributions and variable names from named list
distributions <- as.character(variables)
variables <- names(variables)
Expand Down
2 changes: 2 additions & 0 deletions R/coev_make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ coev_make_standata <- function(data, variables, id, tree,
# check arguments
run_checks(data, variables, id, tree, effects_mat,
dist_mat, prior, scale, prior_only)
# coerce data argument to data frame
data <- as.data.frame(data)
# warning if scale = FALSE
if (!scale) {
warning2(
Expand Down
28 changes: 28 additions & 0 deletions tests/testthat/test-coev_make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -757,3 +757,31 @@ test_that("coev_make_stancode() works with repeated observations", {
)$check_syntax(quiet = TRUE)
)
})

test_that("coev_make_stancode() works with tibbles", {
# simulate data
withr::with_seed(1, {
n <- 20
tree <- ape::rcoal(n)
d <- tibble::tibble(
id = tree$tip.label,
x = rbinom(n, size = 1, prob = 0.5),
y = ordered(sample(1:4, size = n, replace = TRUE))
)
})
# make stan code
sc <-
coev_make_stancode(
data = d,
variables = list(
x = "bernoulli_logit",
y = "ordered_logistic"
),
id = "id",
tree = tree
)
# expect string of length one
expect_no_error(sc)
expect_type(sc, "character")
expect_length(sc, 1)
})
33 changes: 33 additions & 0 deletions tests/testthat/test-coev_make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -741,3 +741,36 @@ test_that("coev_make_stancode() scales data correctly", {
expect_identical(sd2$y[,"y"], as.numeric(scale(d$y)))
expect_identical(sd2$y[,"z"], as.numeric(exp(scale(log(d$z)))))
})

test_that("coev_make_standata() works with tibbles", {
# simulate data
withr::with_seed(1, {
n <- 20
tree <- ape::rcoal(n)
d <- tibble::tibble(
id = tree$tip.label,
x = rbinom(n, size = 1, prob = 0.5),
y = ordered(sample(1:4, size = n, replace = TRUE))
)
})
# make stan data
sd <-
coev_make_standata(
data = d,
variables = list(
x = "bernoulli_logit",
y = "ordered_logistic"
),
id = "id",
tree = tree
)
# expect list with correct names and prior_only = 0
expect_no_error(sd)
expect_type(sd, "list")
expect_equal(
names(sd),
c("N_tips", "N_obs", "J", "N_seg", "node_seq", "parent", "ts", "tip",
"effects_mat", "num_effects", "y", "miss", "tip_id", "prior_only")
)
expect_equal(sd$prior_only, 0)
})

0 comments on commit f631520

Please sign in to comment.