Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when the network has negative or missing branches for phylolm #180

Merged
merged 4 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/traits.jl
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,9 @@ Returns an object of type [`MatrixTopologicalOrder`](@ref).
"""
function sharedPathMatrix(net::HybridNetwork;
checkPreorder=true::Bool)
check_nonmissing_nonnegative_edgelengths(net,
"""The variance-covariance matrix of the network is not defined.
A phylogenetic regression cannot be done.""")
recursionPreOrder(net,
checkPreorder,
initsharedPathMatrix,
Expand Down Expand Up @@ -382,6 +385,24 @@ function initsharedPathMatrix(nodes::Vector{Node}, params)
return(zeros(Float64,n,n))
end

"""
check_nonmissing_nonnegative_edgelengths(net, str="")

Throw an Exception if `net` has undefined edge lengths (coded as -1.0) or
negative edge lengths. The error message indicates the number of the offending
edge(s), followed by `str`.
"""
function check_nonmissing_nonnegative_edgelengths(net::HybridNetwork, str="")
if any(e.length == -1.0 for e in net.edge)
undefined = [e.number for e in net.edge if e.length == -1.0]
error(string("Branch(es) number ", join(undefined,","), " have no length.\n", str))
end
if any(e.length < 0 for e in net.edge)
negatives = [e.number for e in net.edge if e.length < 0.0]
error(string("Branch(es) number ", join(negatives,","), " have negative length.\n", str))
end
end

###############################################################################
"""
descendenceMatrix(net::HybridNetwork; checkPreorder=true::Bool)
Expand Down
2 changes: 1 addition & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ using CSV # for reading files
using DataFrames
using Distributed # parallel in test_correctLik.jl and test_bootstrap.jl
using GLM # for coef, nobs, residuals etc.
using LinearAlgebra: norm, diag, logdet # LinearAlgebra.rotate! not brought into scope
using LinearAlgebra: norm, diag, logdet, PosDefException # LinearAlgebra.rotate! not brought into scope
using Random
using StaticArrays # for rate substitution matrices
using Statistics
Expand Down
25 changes: 25 additions & 0 deletions test/test_lm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,31 @@ lmSH = phylolm(@formula(trait ~ pred), dfr, net, model="scalingHybrid")
# λ so large?? largest γ = 0.056, so λγ = 1.34 is > 1...
end

###############################################################################
### Undefined branch lengths
###############################################################################
@testset "Undefined branch length" begin
## No branch length
net = readTopology("(A:2.5,((B,#H1:1::0.1):1,(C:1,(D:1)#H1:1::0.9):1):0.5);");
dfr = DataFrame(trait = [11.6,8.1,10.3,9.1], tipNames = ["A","B","C","D"]);
@test_throws ErrorException("""Branch(es) number 2 have no length.
The variance-covariance matrix of the network is not defined.
A phylogenetic regression cannot be done.""") phylolm(@formula(trait ~ 1), dfr, net);
## Negative branch length
net.edge[2].length = -0.5;
@test_throws ErrorException("""Branch(es) number 2 have negative length.
The variance-covariance matrix of the network is not defined.
A phylogenetic regression cannot be done.""") phylolm(@formula(trait ~ 1), dfr, net);
## Zero branch length: allowed
net.edge[2].length = 0.0;
fit = phylolm(@formula(trait ~ 1), dfr, net);
@test loglikelihood(fit) ≈ -6.245746681512051
net.edge[2].length = 0.1; # back to non-zero
## Illicit zero length for 1st edge: from root to single "outgroup" taxon
net.edge[1].length = 0.0;
@test_throws PosDefException phylolm(@formula(trait ~ 1), dfr, net);
end

############################
## Against no regressor
###########################
Expand Down