ArtPoon / kamphir

Phylogenetic inference using a tree-shape kernel in an Approximate Bayesian Computation framework

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error in computing make.fgy() in rcolgem

ArtPoon opened this issue · comments

1   0.89698 1.00000 0.8 1.0 0.54644 0.9 50000.0 1560.0
Error in sample.int(m^2, size = 1, prob = as.vector(.lambdamat)) : 
  NA in probability vector
Calls: simulate.binary.dated.tree.fgy -> lapply -> FUN -> sample.int
Execution halted
2   0.89698 1.00000 0.77164 1.0 0.54644 0.9 50000.0 1560.0
debug: .F <- fgy$.F
debug: .G <- fgy$.G
debug: .Y <- fgy$.Y
debug: a <- A/.Y
debug: extantLines <- which(isExtant)
debug: .lambdamat <- (t(t(a)) %*% a) * .F
debug: kl <- sample.int(m^2, size = 1, prob = as.vector(.lambdamat))
Error in sample.int(m^2, size = 1, prob = as.vector(.lambdamat)) : 
  NA in probability vector
Calls: simulate.binary.dated.tree.fgy -> lapply -> FUN -> sample.int

So far I haven't been able to reproduce this problem in an interactive session (R GUI) but it does come up through Rscript.

Got it:

> source('simulate.DiffRisk.R')
Called from: value[[3L]](cond)
Browse[1]> where
where 1: value[[3L]](cond)
where 2: tryCatchOne(expr, names, parentenv, handlers[[1L]])
where 3: tryCatchList(expr, classes, parentenv, handlers)
where 4: tryCatch({
    .lambdamat <- (t(t(a)) %*% a) * .F
    kl <- sample.int(m^2, size = 1, prob = as.vector(.lambdamat))
    k <- 1 + ((kl - 1)%%m)
    l <- 1 + floor((kl - 1)/m)
    probstates <- mstates[extantLines, ]
    u_i <- sample.int(nExtant, size = 1, prob = probstates[, 
        k])
    probstates[u_i, ] <- 0
    u <- extantLines[u_i]
    v <- sample(extantLines, size = 1, prob = probstates[, l])
}, error = function(e) browser())
where 5: FUN(1:10[[4L]], ...)
where 6: lapply(1:n.reps, run1)
where 7 at simulate.DiffRisk.R#135: simulate.binary.dated.tree.fgy(tfgy[[1]], tfgy[[2]], tfgy[[3]], 
    tfgy[[4]], sampleTimes, sampleStates, integrationMethod = integrationMethod, 
    n.reps = n.reps, n.cores = n.cores)
where 8: eval(expr, envir, enclos)
where 9: eval(ei, envir)
where 10: withVisible(eval(ei, envir))
where 11: source("simulate.DiffRisk.R")

Browse[1]> a
      I1       I2 
1.210267      Inf 
Browse[1]> .F
            [,1]         [,2]
[1,] 0.007359528 0.0006387746
[2,] 0.000000000 0.0000000000
Browse[1]> .lambdamat
         [,1] [,2]
I1 0.01077985  Inf
I2        NaN  NaN

Solution of .solve.Q.A.L:

Browse[1]> out
[[1]]
           [,1]        [,2]
[1,] 0.45372289 0.008613176
[2,] 0.08774176 0.777683663

[[2]]
[1]  0.3417951 -0.2653040

[[3]]
[1] NaN

Exception is thrown at last step of loop over length(eventTimes)-1 which is 999 - this seems to be the final coalescent event to the root. nExtant is 2, which is consistent with this interpretation.

Browse[1]> h0
[1] 1425.713
Browse[1]> h1
[1] 1549.263

These are node heights.

Browse[1]> get.fgy(h1)
$.F
            [,1]         [,2]
[1,] 0.007359528 0.0006387746
[2,] 0.000000000 0.0000000000

$.G
     [,1] [,2]
[1,]    0    0
[2,]    0    0

$.Y
I1 I2 
 1  0 

These are the birth, migration, and lineage count matrices.

This seems to be a numerical approximation issue, fgyResolution is currently set to 100 which is quick to compute but apparently results in sum(.Y) of 1 with 2 lineages still extant. At this setting, time is partitioned as follows:

Browse[1]> times
  [1] 1560.00000 1544.24242 1528.48485 1512.72727 1496.96970 1481.21212
  [7] 1465.45455 1449.69697 1433.93939 1418.18182 1402.42424 1386.66667
 [13] 1370.90909 1355.15152 1339.39394 1323.63636 1307.87879 1292.12121

Note h1 is at 1549.263 (previous comment) which falls between the first and second time points.
I tried bumping fgyResolution up to 1000 and it seems to resolve the issue. Experimenting with different settings to see the smallest value I can get away with.

Fortunately there is only a modest linear time cost for increasing fgyResolution. Set to 500 and closing for now.