Skip to content

Commit

Permalink
#52 Require multiPhylo objects to have same number of branches
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Claessens authored and Scott Claessens committed Oct 1, 2024
1 parent ba6adf4 commit 362183f
Show file tree
Hide file tree
Showing 10 changed files with 79 additions and 6 deletions.
3 changes: 2 additions & 1 deletion R/coev_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
#' the data. The id column must exactly match the tip labels in the phylogeny.
#' @param tree A phylogenetic tree object of class \code{phylo} or
#' \code{multiPhylo}. The tree(s) must be rooted and must include positive
#' non-zero branch lengths.
#' non-zero branch lengths. All trees in \code{multiPhylo} objects must have
#' the same number of internal nodes and branches.
#' @param effects_mat (optional) A boolean matrix with row and column names
#' exactly matching the variables declared for the model. If not specified,
#' all cross-lagged effects will be estimated in the model. If specified, the
Expand Down
10 changes: 10 additions & 0 deletions R/coev_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ run_checks <- function(data, variables, id, tree, effects_mat,
)
}
}
# stop if trees have different numbers of internal nodes or branches
if (length(unique(lapply(tree, function(x) x$Nnode))) != 1 |
length(unique(lapply(tree, function(x) length(x$edge.length)))) != 1) {
stop2(
paste0(
"All trees in 'tree' argument must have the same number of ",
"internal nodes and branches."
)
)
}
# stop if id in data contains missing values
if (any(is.na(data[,id]))) {
stop2("The id variable in the data must not contain NAs.")
Expand Down
3 changes: 2 additions & 1 deletion R/coev_make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
#' the data. The id column must exactly match the tip labels in the phylogeny.
#' @param tree A phylogenetic tree object of class \code{phylo} or
#' \code{multiPhylo}. The tree(s) must be rooted and must include positive
#' non-zero branch lengths.
#' non-zero branch lengths. All trees in \code{multiPhylo} objects must have
#' the same number of internal nodes and branches.
#' @param effects_mat (optional) A boolean matrix with row and column names
#' exactly matching the variables declared for the model. If not specified,
#' all cross-lagged effects will be estimated in the model. If specified, the
Expand Down
3 changes: 2 additions & 1 deletion R/coev_make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
#' the data. The id column must exactly match the tip labels in the phylogeny.
#' @param tree A phylogenetic tree object of class \code{phylo} or
#' \code{multiPhylo}. The tree(s) must be rooted and must include positive
#' non-zero branch lengths.
#' non-zero branch lengths. All trees in \code{multiPhylo} objects must have
#' the same number of internal nodes and branches.
#' @param effects_mat (optional) A boolean matrix with row and column names
#' exactly matching the variables declared for the model. If not specified,
#' all cross-lagged effects will be estimated in the model. If specified, the
Expand Down
3 changes: 2 additions & 1 deletion man/coev_fit.Rd

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

3 changes: 2 additions & 1 deletion man/coev_make_stancode.Rd

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

3 changes: 2 additions & 1 deletion man/coev_make_standata.Rd

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

19 changes: 19 additions & 0 deletions tests/testthat/test-coev_fit.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ test_that("coev_fit() produces expected errors", {
"The id variable in the data does not match tree tip labels exactly.",
fixed = TRUE
)
expect_error(
{
tree2 <- c(tree, ape::di2multi(tree, tol = 0.01)) # collapse internal node
coev_fit(
data = d,
variables = list(
x = "bernoulli_logit",
y = "ordered_logistic"
),
id = "id",
tree = tree2
)
},
paste0(
"All trees in 'tree' argument must have the same number of ",
"internal nodes and branches."
),
fixed = TRUE
)
expect_error(
{
d2 <- d; d2$id[1] <- NA
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-coev_make_stancode.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ test_that("coev_make_stancode() produces expected errors", {
"The id variable in the data does not match tree tip labels exactly.",
fixed = TRUE
)
expect_error(
{
tree2 <- c(tree, ape::di2multi(tree, tol = 0.01)) # collapse internal node
coev_make_stancode(
data = d,
variables = list(
x = "bernoulli_logit",
y = "ordered_logistic"
),
id = "id",
tree = tree2
)
},
paste0(
"All trees in 'tree' argument must have the same number of ",
"internal nodes and branches."
),
fixed = TRUE
)
expect_error(
{
d2 <- d; d2$id[1] <- NA
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-coev_make_standata.R
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,25 @@ test_that("coev_make_standata() produces expected errors", {
"The id variable in the data does not match tree tip labels exactly.",
fixed = TRUE
)
expect_error(
{
tree2 <- c(tree, ape::di2multi(tree, tol = 0.01)) # collapse internal node
coev_make_standata(
data = d,
variables = list(
x = "bernoulli_logit",
y = "ordered_logistic"
),
id = "id",
tree = tree2
)
},
paste0(
"All trees in 'tree' argument must have the same number of ",
"internal nodes and branches."
),
fixed = TRUE
)
expect_error(
{
d2 <- d; d2$id[1] <- NA
Expand Down

0 comments on commit 362183f

Please sign in to comment.