Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pbastide committed Nov 12, 2024
1 parent 0a9f313 commit c31b166
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ Suggests:
lme4,
lmerTest
Remotes: lamho86/phylolm
RoxygenNote: 7.3.1
RoxygenNote: 7.3.2
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ importFrom(methods,new)
importFrom(stats,approxfun)
importFrom(stats,lowess)
importFrom(stats,model.matrix)
importFrom(stats,uniroot)
11 changes: 7 additions & 4 deletions R/phylogeneticCorrelations.R
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ NULL
#' @export
#'
#' @importFrom graphics lines title
#' @importFrom stats approxfun lowess model.matrix
#' @importFrom stats approxfun lowess model.matrix uniroot
#'
phylogeneticCorrelations <- function(object, design = NULL, phy, col_species = NULL,
model = c("BM", "lambda", "OUfixedRoot", "OUrandomRoot", "delta"),
Expand Down Expand Up @@ -165,6 +165,7 @@ get_consensus_tree <- function(y_data, design, phy, model, measurement_error, we
}
reqpckg <- c("phylolm")

i <- NULL
all_fits <- foreach::foreach(i = 1:nrow(y_data), .packages = reqpckg) %myinfix% {

data_phylolm <- as.data.frame(cbind(y_data[i, ], design))
Expand Down Expand Up @@ -251,6 +252,7 @@ get_consensus_tree_lambda <- function(phy, all_phyfit, measurement_error, trim)
params = list(model = "lambda",
measurement_error = measurement_error,
lambda = lambda_mean,
lambda_error = lambda_mean,
atanh_lambda = all_lambdas_transform)))
}

Expand Down Expand Up @@ -328,7 +330,7 @@ get_consensus_tree_OUfixedRoot <- function(phy, all_phyfit, measurement_error, t
atanh(pmax(-1, rho_prime(alp, t_original_tree)))
}
trans_inv_alpha <- function(tt) {
rho_prime_inv(tanh(tt), t_original_tree)
rho_prime_inv(tanh(tt), t_original_tree, alpha_bounds)
}

all_alphas <- sapply(all_phyfit, function(x) x$optpar)
Expand Down Expand Up @@ -462,13 +464,14 @@ rho_prime <- function(alpha, t_tree, tol = .Machine$double.eps) {
#'
#' @param y the rhoprime value
#' @param t_tree the total height of the tree
#' @param alpha_bounds lower and upper bounds on alpha values
#'
#' @return Value of alpha
#'
#' @keywords internal
#'
rho_prime_inv <- function(y, t_tree) {
uniroot((function(x) rho_prime(x, t_tree) - y), interval = c(.Machine$double.eps, 1), tol = .Machine$double.eps^0.5)$root
rho_prime_inv <- function(y, t_tree, alpha_bounds) {
uniroot((function(x) rho_prime(x, t_tree) - y), interval = alpha_bounds, tol = .Machine$double.eps^0.5)$root
}

#' @title Get OU transformed tree
Expand Down
26 changes: 26 additions & 0 deletions man/rho_prime.Rd

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

22 changes: 22 additions & 0 deletions man/rho_prime_inv.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/test-ddf.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ test_that("Satterthwaite - chen tree", {
measurement_error = TRUE,
lower.bound = list(sigma2_error = getMinError(tree_rep)),
REML = TRUE)
expect_equal(ddf_satterthwaite_BM_error(fit_phylolm, tree_rep)$ddf[1], nsamples - 2, tolerance = 1e-2)
expect_equal(ddf_satterthwaite_BM_error(fit_phylolm, tree_rep)$ddf[1], 2.38, tolerance = 1e-2)

})

Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-errors.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ test_that("Errors with phylogenetic correlations", {

## phylo cor
pc <- phylogeneticCorrelations(y_data, design, phy = tree, model = "OUfixedRoot", measurement_error = FALSE, REML = FALSE)
expect_equal(pc$params$alpha, 4.56, tol = 1e-2)
expect_equal(pc$params$alpha, 2.06, tol = 1e-2)

expect_error(
phylolmFit(y_data, design = design, phy = tree, use_consensus = TRUE, consensus_tree = pc),
Expand Down
13 changes: 9 additions & 4 deletions tests/testthat/test-phylogeneticCorrelations.R
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,14 @@ test_that("phylogeneticCorrelations - BM", {

expect_equal(names(resPhyloLmFitConsLambda), names(resPhyloLmFitCons))
for (nn in names(resPhyloLmFitConsLambda)) {
if (!nn %in% c("modelphy", "measurement_error", "consensus_tree", "phy_trans")) {
if (!nn %in% c("modelphy", "measurement_error", "consensus_tree", "phy_trans", "qr", "C_tree")) {
expect_equivalent(resPhyloLmFitConsLambda[[nn]],
resPhyloLmFitCons[[nn]],
tol = 1e-4)
}
if (nn%in% c("qr", "C_tree")) {
expect_equal(resPhyloLmFitConsLambda[[nn]],
resPhyloLmFitCons[[nn]],
resPhyloLmFitCons[[nn]][[1]],
tol = 1e-4)
}
}
Expand All @@ -88,7 +93,7 @@ test_that("phylogeneticCorrelations - BM", {
tol = 1e-4)

expect_equal(resPhyloLmFitConsLambda$phy_trans,
resPhyloLmFitCons$phy_trans,
resPhyloLmFitCons$phy_trans$treecons,
tol = 1e-4)

#################################################################################################
Expand All @@ -106,7 +111,7 @@ test_that("phylogeneticCorrelations - BM", {
use_consensus = TRUE,
medianOU = TRUE)

expect_true(resPhyloLmFitConsOUmed$consensus_tree$params$alpha <= resPhyloLmFitConsOU$consensus_tree$params$alpha)
expect_true(resPhyloLmFitConsOUmed$consensus_tree$params$alpha >= resPhyloLmFitConsOU$consensus_tree$params$alpha)
expect_true(resPhyloLmFitConsOUmed$consensus_tree$params$lambda_error <= resPhyloLmFitConsOU$consensus_tree$params$lambda_error)

})
Expand Down
2 changes: 1 addition & 1 deletion tests/testthat/test-starTree.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,6 @@ test_that("phylogeneticCorrelations - Convergence issues", {
block = sub("_[1-9]", "", tree_rep$tip.label))

expect_equal(phycor$params$lambda, ducor$cor, tolerance = 1e-2)
expect_equal(phycor2$params$lambda, phycor$params$lambda, tolerance = 1e-2)
expect_equal(phycor2$params$lambda_error, phycor$params$lambda, tolerance = 1e-2)

})

0 comments on commit c31b166

Please sign in to comment.