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

Bug fixes in IW model (brt table) #167

Merged
merged 55 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
13aed57
Increment version number to 4.4.1.9000
Neves-P May 15, 2024
95d2915
Update CI
Neves-P May 15, 2024
0c3223d
Wrong NEWS.md, d'oh!
Neves-P May 15, 2024
c901e27
Merge branch 'master' into develop
Neves-P May 15, 2024
74b038d
Add test that ML should give positive likelihood for Galapagos data w…
Jun 24, 2024
2ac5742
Remove row name of output of ML when NA
Jun 24, 2024
3f9cf6d
Undo previous commit
Jun 24, 2024
7cce2fe
don't let adding a column to a dataframe rename the row.
Jun 24, 2024
3467dad
Test for non-oceanic should yield -Inf (NA) for Galapagos for nonzero…
Jun 24, 2024
7567775
Clearer comments
Jun 24, 2024
ef739f0
Bug fix add_brt_table and proposal for new function to replace it
Jun 28, 2024
75294c9
Remove NA rows in add_brt_table
Jun 28, 2024
8403209
Merge branch 'develop' of https://github.com/rsetienne/DAISIE into de…
Jun 28, 2024
a5ac732
Add 'simplex' to test.
Jun 28, 2024
8392343
try test with subplex
Jun 28, 2024
18ddc2e
Update test DAISIE_ML1
Jul 1, 2024
46ab68d
Tolerance of DAISIE_ML1 test at 1E-6.
Jul 1, 2024
59c2705
Tolerance of DAISIE_ML1 test at 1E-6.
Jul 1, 2024
80f7bb2
Update test of DAISIE_ML1
Jul 1, 2024
f8dc008
Merge pull request #166 from rsetienne/f2018
rsetienne Jul 1, 2024
45ec8c9
Update test-DAISIE_ML2
Jul 1, 2024
0a5cf8e
Fix buggy brts_table for full_table = TRUE and change tests according…
Jul 5, 2024
5f435c7
Additional test on simulated data set
Jul 5, 2024
f74750e
[run_ci]
Jul 7, 2024
fc64b32
test [run-ci]
Jul 7, 2024
4cca850
test [run ci]
Jul 7, 2024
3255a8a
Simulated data set added and moving definition of M.
Jul 7, 2024
6366077
[run ci]
Jul 7, 2024
139a95c
Reduce data set to one item and adjust tests accordingly [run ci]
Jul 7, 2024
090b61e
Add test of IW loglik with additonal branching time
Jul 7, 2024
0e5b19a
[run ci]
Jul 7, 2024
8eeff12
Add a value for the loglik
Jul 7, 2024
6938ae8
Roxygenized
Jul 8, 2024
6269602
[run ci]
Jul 8, 2024
33b70eb
Remove data set temporarily. Next commit will add it.
Jul 8, 2024
450d8e9
Add just removed data set [run ci]
Jul 8, 2024
b65e056
merge non-oceanic_loglik [run ci]
Jul 8, 2024
1f213ad
Also cover 5 <= stac <= 9 in loglik_high_lambda
Jul 12, 2024
47c29e0
edits loglik_high_lambda
Jul 15, 2024
24920f7
early exit loglik_high_lambda
Jul 15, 2024
9077b3e
bug fix loglik_high_lambda
Jul 16, 2024
2918b5a
bug fix
Jul 16, 2024
1575f6e
changed comments in DAISIE_loglik_high_lambda.R
Jul 24, 2024
a733488
Fix the result of rexp(1,totalrates = 0) = NaN
Oct 21, 2024
e09772a
Further fix the result of rexp(1,totalrates = 0) = NaN
Oct 21, 2024
f8b3666
Give warnings if trying to optimize with 0 immigration rate or 0 exti…
Oct 21, 2024
6911ac4
Fix typo
Oct 21, 2024
02488b9
Add spaces
Oct 21, 2024
6a753cd
Fix typo
Oct 21, 2024
43e37e1
Make DAISIE_ML1 flexible to allow another loglik function to be optim…
Oct 23, 2024
b5c2d9b
Allowing for adding approximate loglikelihood function
Oct 23, 2024
f6bde00
Update the documentation about the exact and approximate likelihood f…
Oct 24, 2024
38b61f1
Updating documentation files.
Oct 24, 2024
2d93c4d
fix of bug reported by Yang Shen
Nov 4, 2024
35d2c43
Change conditions for message in IW
Nov 20, 2024
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
6 changes: 5 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:

name: R-CMD-check

permissions: read-all

jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}
Expand All @@ -22,13 +24,14 @@ jobs:
- {os: windows-latest, r: 'release'}
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -46,3 +49,4 @@ jobs:
- uses: r-lib/actions/check-r-package@v2
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
24 changes: 17 additions & 7 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,55 @@ on:

name: test-coverage

permissions: read-all

jobs:
test-coverage:
runs-on: ubuntu-latest
if: "contains(github.event.head_commit.message, '[run ci]') || (github.ref == 'refs/heads/master' || github.ref == 'refs/heads/develop') || github.event.pull_request"

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
use-public-rspm: true

- uses: r-lib/actions/setup-r-dependencies@v2
with:
extra-packages: any::covr
extra-packages: any::covr, any::xml2
needs: coverage

- name: Test coverage
run: |
covr::codecov(
cov <- covr::package_coverage(
quiet = FALSE,
clean = FALSE,
install_path = file.path(Sys.getenv("RUNNER_TEMP"), "package")
install_path = file.path(normalizePath(Sys.getenv("RUNNER_TEMP"), winslash = "/"), "package")
)
covr::to_cobertura(cov)
shell: Rscript {0}

- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: ${{ github.event_name != 'pull_request' && true || false }}
file: ./cobertura.xml
plugin: noop
disable_search: true
token: ${{ secrets.CODECOV_TOKEN }}

- name: Show testthat output
if: always()
run: |
## --------------------------------------------------------------------
find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: DAISIE
Type: Package
Title: Dynamical Assembly of Islands by Speciation, Immigration and Extinction
Version: 4.4.1
Version: 4.4.1.9000
Date: 2023-10-20
Depends: R (>= 4.2.0)
biocViews:
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# DAISIE (development version)

# DAISIE 4.4.1

* Fix bug in the calculation of shift loglikelihoods.
Expand Down
11 changes: 9 additions & 2 deletions R/DAISIE_ML1.R
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ DAISIE_ML1 <- function(
tolint = c(1E-16, 1E-10),
island_ontogeny = NA,
jitter = 0,
num_cycles = 1) {
num_cycles = 1,
function_to_optimize = 'DAISIE_exact') {
# datalist = list of all data: branching times, status of clade, and numnber of missing species
# datalist[[,]][1] = list of branching times (positive, from present to past)
# - max(brts) = age of the island
Expand Down Expand Up @@ -156,6 +157,12 @@ DAISIE_ML1 <- function(
# . eqmodel = 4 : equilibrium is assumed on immigrants using deterministic equation for endemics and immigrants
# . eqmodel = 5 : equilibrium is assumed on endemics and immigrants using deterministic equation for endemics and immigrants

if(function_to_optimize == 'DAISIE_exact') {
function_to_optimize <- DAISIE_loglik_all_choosepar
} else
{
DAISIE_loglik_all_choosepar_approx
}

out2err <- data.frame(
lambda_c = NA,
Expand Down Expand Up @@ -288,7 +295,7 @@ DAISIE_ML1 <- function(
)

optimpars <- c(tol, maxiter)
initloglik <- DAISIE_loglik_all_choosepar(
initloglik <- function_to_optimize(
trparsopt = trparsopt,
trparsfix = trparsfix,
idparsopt = idparsopt,
Expand Down
6 changes: 4 additions & 2 deletions R/DAISIE_ML_CS.R
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ DAISIE_ML_CS <- DAISIE_ML <- function(
verbose = 0,
tolint = c(1E-16, 1E-10),
jitter = 0,
num_cycles = 1) {
num_cycles = 1,
function_to_optimize = 'DAISIE_exact') {

if (datatype == "single") {
if (is.na(island_ontogeny)) {
Expand Down Expand Up @@ -229,7 +230,8 @@ DAISIE_ML_CS <- DAISIE_ML <- function(
verbose = verbose,
tolint = tolint,
jitter = jitter,
num_cycles = num_cycles)
num_cycles = num_cycles,
function_to_optimize = function_to_optimize)
}
} else {
stop(
Expand Down
14 changes: 13 additions & 1 deletion R/DAISIE_ML_IW.R
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ DAISIE_ML_IW <- function(
}
np = datalist[[1]]$not_present
if (is.null(np)) {
np = datalist[[1]]$not_present_type1 + datalist[[1]]$not_present_type2
np <- datalist[[1]]$not_present_type1 + datalist[[1]]$not_present_type2
warning('Number of species not present is misspecified.\n')
return(invisible(out2err))
}
Expand All @@ -130,6 +130,18 @@ DAISIE_ML_IW <- function(
verbose = verbose
)

pars1 <- rep(0, 5)
pars1[idparsopt] <- initparsopt
if (length(idparsfix) != 0) {
pars1[idparsfix] <- parsfix
}
if (pars1[4] == 0) {
warning('You have chosen a zero immigration rate; an empty island will never be colonized')
}
if (pars1[2] == 0) {
warning('You have chosen a zero extinction rate; this might create problems in optimization; please use a number close to 0 instead')
}

trparsopt <- initparsopt / (1 + initparsopt)
trparsopt[which(initparsopt == Inf)] <- 1
trparsfix <- parsfix / (1 + parsfix)
Expand Down
11 changes: 8 additions & 3 deletions R/DAISIE_SR_sim_core.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,10 @@ DAISIE_SR_sim_core <- function(time,mainland_n,pars)
immig_rate <- max(c(mainland_n * gam * (1 - length(island_spec[,1])/K),0),na.rm = T)

totalrate <- ext_rate + clado_rate + ana_rate + immig_rate
dt <- stats::rexp(1,totalrate)

dt <- stats::rexp(1, totalrate)
if (is.nan(dt)) {
dt <- total_time - timeval
}
if ( timeval < pars[11] & ((timeval + dt) >= pars[11]) )
{
lac <- pars[6]
Expand All @@ -98,6 +100,9 @@ DAISIE_SR_sim_core <- function(time,mainland_n,pars)
immig_rate <- max( c(mainland_n * gam * (1 - length(island_spec[,1])/K), 0), na.rm = TRUE )
totalrate <- ext_rate + clado_rate + ana_rate + immig_rate
dt <- stats::rexp(1, totalrate)
if (is.nan(dt)) {
dt <- total_time - pars[11]
}
timeval <- pars[11] + dt
} else
{
Expand All @@ -107,7 +112,7 @@ DAISIE_SR_sim_core <- function(time,mainland_n,pars)
possible_event <- sample(1:4,1,replace = FALSE,c(immig_rate,ext_rate,ana_rate,clado_rate))

##############
if(timeval <= total_time)
if(timeval < total_time)
{
new_state <- DAISIE_sim_update_state_cr(timeval = timeval,
total_time = total_time,
Expand Down
40 changes: 40 additions & 0 deletions R/DAISIE_data.R
Original file line number Diff line number Diff line change
Expand Up @@ -565,3 +565,43 @@ NULL
#' }
#' @keywords datasets
NULL

#' @name frogs_sim_datalist
#' @title Colonization and branching times of a data set simulated with the
#' MLE parameters of frogs_datalist
#' @docType data
#' @format A list with the first element containing 2 elements and the following
#'elements containing 5 components.
#' @description A list containing the colonization and branching times
#' of a simulated data set using the MLE parameters of IW model for the
#' frogs_datalist. It is an R list object with the following elements.\cr \cr
#' The first element of the list has two components: \cr \cr
#' \code{$island_age} - the island age \cr
#' \code{$not_present} - the number of mainland lineages that are not present
#' on the island \cr \cr
#' The following elements of the list each contains
#' information on a single colonist lineage on the island and has 5
#' components:\cr \cr
#' \code{$colonist_name} - the name of the species or clade
#' that colonized the island \cr
#' \code{$branching_times} - island age followed by stem age of the population/species
#' in the case of Non-endemic, Non-endemic_MaxAge species and Endemic species with no close
#' relatives on the island. For endemic clades with more than one species on the island
#' (cladogenetic clades/ radiations) these should be island age followed by the
#' branching times of the island clade including the stem age of the clade.\cr
#' \code{$stac} - the status of the colonist \cr \cr
#' * Non_endemic_MaxAge: 1 \cr
#' * Endemic: 2 \cr
#' * Endemic&Non_Endemic: 3 \cr
#' * Non_endemic: 4 \cr
#' * Endemic_MaxAge: 5 or 6 \cr \cr
#' \code{$missing_species} - number of island species
#' that were not sampled for particular clade (only applicable for endemic
#' clades) \cr
#' \code{$type1or2} - whether the colonist belongs to type 1 or
#' type 2. In this dataset all are equal to 1. \cr
#' @seealso \code{\link{DAISIE_ML}}, \code{\link{DAISIE_SR_ML}}
#' @source Etienne RS, Haegeman B, Dugo-Cota A, Vila C, Gonzalez-Voyer A &
#' Valente L. The limits to ecological limits to diversification.\cr
#' @keywords datasets
NULL
89 changes: 87 additions & 2 deletions R/DAISIE_format_IW.R
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ DAISIE_format_IW_trait <- function(island_replicates,
return(several_islands)
}

add_brt_table <- function(island, full_table = FALSE) {
add_brt_table_old <- function(island, full_table = FALSE) {
island_age <- island[[1]]$island_age
island_top <- island[[1]]
if (length(island) == 1) {
Expand Down Expand Up @@ -175,7 +175,7 @@ add_brt_table <- function(island, full_table = FALSE) {
for (k in 1:length(island[[i]]$all_colonisations)) {
the_brts <- island[[i]]$all_colonisations[[k]]$event_times[-1]
pos2 <- pos1 + length(the_brts)
ff <- matrix(ncol = 5, nrow = pos2 - pos1 + 1)
ff <- matrix(ncol = 5, nrow = pos2 - pos1)
ff[1:(pos2 - pos1), 1] <- the_brts
ff[1:(pos2 - pos1), 2] <- j
ff[1:(pos2 - pos1), 3] <- seq(1, length(the_brts))
Expand Down Expand Up @@ -203,3 +203,88 @@ add_brt_table <- function(island, full_table = FALSE) {
colnames(island[[1]]$brts_table) <- c("brt", "clade", "event", "endemic", "col")
return(island)
}

add_brt_table <- function(island, full_table = TRUE) {
island_age <- island[[1]]$island_age
island_top <- island[[1]]
if (length(island) == 1) {
brts_table <- matrix(ncol = 5, nrow = 1)
brts_table[1, ] <- c(island_age, 0, 0, NA, NA)
island[[1]]$brts_table <- brts_table
} else {
island_top <- island[[1]]
island[[1]] <- NULL
btimes <- list()
for (i in 1:length(island)) {
btimes[[i]] <- island[[i]]$branching_times[-1]
}
island <- island[rev(order(sapply(btimes, "[", 1)))]
il <- unlist(island)
stac1s <- which(il[which(names(il) == "stac")] == "1")
stac5s <- which(il[which(names(il) == "stac")] == "5")
stac1_5s <- sort(c(stac1s, stac5s))
if (length(stac1_5s) != 0) {
if (length(stac1_5s) == length(island)) {
brts_table <- matrix(ncol = 5, nrow = 1)
brts_table[1, ] <- c(island_age, 0, 0, NA, NA)
island_no_stac1or5 <- NULL
} else {
island_no_stac1or5 <- island[-stac1_5s]
}
}
if (length(stac1_5s) == 0) {
island_no_stac1or5 <- island
}
if (length(island_no_stac1or5) != 0) {
btimes <- list()
for (i in 1:length(island_no_stac1or5)) {
btimes[[i]] <- island_no_stac1or5[[i]]$branching_times[-1]
}
brts <- rev(sort(unlist(btimes)))
brts_IWK <- NULL
pos1 <- 0
for (i in 1:length(btimes)) {
the_stac <- island_no_stac1or5[[i]]$stac
if(!is.null(island[[i]]$all_colonisations) & full_table == TRUE) {
#if(length(island[[i]]$all_colonisations) > 0) print(i)
for (k in 1:length(island[[i]]$all_colonisations)) {
the_brts <- island[[i]]$all_colonisations[[k]]$event_times[-1]
pos2 <- pos1 + length(the_brts)
ff <- matrix(ncol = 5, nrow = pos2 - pos1)
ff[1:(pos2 - pos1), 1] <- the_brts
ff[1:(pos2 - pos1), 2] <- i
ff[1:(pos2 - pos1), 3] <- seq(1, length(the_brts))
ff[1:(pos2 - pos1), 4] <- (the_stac == 2) + (the_stac == 3)
ff[1:(pos2 - pos1), 5] <- k
brts_IWK <- rbind(brts_IWK,ff)
pos1 <- pos2
}
} else {
the_brts <- btimes[[i]]
pos2 <- pos1 + length(the_brts)
ff <- matrix(ncol = 5, nrow = pos2 - pos1)
ff[1:(pos2 - pos1), 1] <- the_brts
ff[1:(pos2 - pos1), 2] <- i
ff[1:(pos2 - pos1), 3] <- seq(1, length(the_brts))
ff[1:(pos2 - pos1), 4] <- (the_stac == 2) + (the_stac == 3)
ff[1:(pos2 - pos1), 5] <- 1
brts_IWK <- rbind(brts_IWK,ff)
pos1 <- pos2
}
}
brts_table <- brts_IWK[rev(order(brts_IWK[, 1])), ]
brts_table <- rbind(c(island_age, 0, 0, NA, NA), brts_table)
}
island_top$brts_table <- brts_table
if (length(stac1_5s) != 0) {
for (i in 1:length(stac1_5s)) {
island[[length(island) + 1]] <- island[[stac1_5s[i]]]
island[[stac1_5s[i]]] <- NULL
stac1_5s <- stac1_5s - 1
}
}
island <- append(list(island_top), island)
}
colnames(island[[1]]$brts_table) <- c("brt", "clade", "event", "endemic", "col")
return(island)
}
8 changes: 4 additions & 4 deletions R/DAISIE_loglik_CS.R
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,14 @@ DAISIE_loglik_CS_M1 <- DAISIE_loglik <- function(pars1,
# means that any colonization that took place before this maximum
# colonization time (including presence in the non-oceanic scenario)
# does not count and should be followed by another colonization.
# To allow this we introduce a third set of equations for the
# probability that colonization might have happened before but
# recolonization has not taken place yet (Q_M,n).
# To allow this we introduce a third and fourth set of equations for
# the probability that colonization might have happened before but
# recolonization has not taken place yet (Q_M,n and Q^M_{M,n}).
epss <- 1.01E-5 #We're taking the risk
if (abs(brts[2] - brts[1]) >= epss) {
probs[(2 * lx + 1):(4 * lx)] <- probs[1:(2 * lx)]
probs[1:(2 * lx)] <- 0
} else {
} else { #max age equals island age
probs[(2 * lx + 1):(4 * lx)] <- 0
}

Expand Down
Loading
Loading