diff --git a/.github/workflows/R-CMD-check.yaml b/.github/workflows/R-CMD-check.yaml index 17d90dbe..80097dc5 100644 --- a/.github/workflows/R-CMD-check.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -6,6 +6,8 @@ on: name: R-CMD-check +permissions: read-all + jobs: R-CMD-check: runs-on: ${{ matrix.config.os }} @@ -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 @@ -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")' diff --git a/.github/workflows/test-coverage.yaml b/.github/workflows/test-coverage.yaml index ddaeb214..6eea989b 100644 --- a/.github/workflows/test-coverage.yaml +++ b/.github/workflows/test-coverage.yaml @@ -6,16 +6,17 @@ 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: @@ -23,28 +24,37 @@ jobs: - 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 diff --git a/DESCRIPTION b/DESCRIPTION index de450254..91cd085e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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: diff --git a/NEWS.md b/NEWS.md index 0a5047c9..5490f5bb 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,5 @@ +# DAISIE (development version) + # DAISIE 4.4.1 * Fix bug in the calculation of shift loglikelihoods. diff --git a/R/DAISIE_ML1.R b/R/DAISIE_ML1.R index 2fb1edb9..4f602942 100644 --- a/R/DAISIE_ML1.R +++ b/R/DAISIE_ML1.R @@ -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 @@ -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, @@ -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, diff --git a/R/DAISIE_ML_CS.R b/R/DAISIE_ML_CS.R index 18b40c7c..58dfa9a8 100644 --- a/R/DAISIE_ML_CS.R +++ b/R/DAISIE_ML_CS.R @@ -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)) { @@ -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( diff --git a/R/DAISIE_ML_IW.R b/R/DAISIE_ML_IW.R index b2c19933..e7b1d38e 100644 --- a/R/DAISIE_ML_IW.R +++ b/R/DAISIE_ML_IW.R @@ -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)) } @@ -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) diff --git a/R/DAISIE_SR_sim_core.R b/R/DAISIE_SR_sim_core.R index 599498bd..7b91a04a 100644 --- a/R/DAISIE_SR_sim_core.R +++ b/R/DAISIE_SR_sim_core.R @@ -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] @@ -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 { @@ -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, diff --git a/R/DAISIE_data.R b/R/DAISIE_data.R index 4e4991a0..1daeb6ea 100644 --- a/R/DAISIE_data.R +++ b/R/DAISIE_data.R @@ -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 diff --git a/R/DAISIE_format_IW.R b/R/DAISIE_format_IW.R index 9584c632..c26d1867 100644 --- a/R/DAISIE_format_IW.R +++ b/R/DAISIE_format_IW.R @@ -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) { @@ -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)) @@ -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) +} diff --git a/R/DAISIE_loglik_CS.R b/R/DAISIE_loglik_CS.R index 1bbb2993..c2a9afa6 100644 --- a/R/DAISIE_loglik_CS.R +++ b/R/DAISIE_loglik_CS.R @@ -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 } diff --git a/R/DAISIE_loglik_IW.R b/R/DAISIE_loglik_IW.R index 6a059c48..bc8c539f 100644 --- a/R/DAISIE_loglik_IW.R +++ b/R/DAISIE_loglik_IW.R @@ -368,7 +368,9 @@ DAISIE_loglik_IW <- function( lxm <- min(lx,M + 1) lxe <- lx - if(M * (1 - exp((min(brts) * gam))) > 0.2 * lxm) { + est_num_potential_colonizers <- min(1 + ceiling(Kprime), M * (1 - exp((min(brts) * gam)))) + + if(est_num_potential_colonizers > lxm) { message('With this colonization rate and system size setting, results may not be accurate.') } @@ -427,7 +429,12 @@ DAISIE_loglik_IW <- function( test_for_colonization <- TRUE } else { - test_for_colonization <- (max(event[which(clade == col[k + 2])]) > 1) + #test_for_colonization <- (max(event[which(clade == col[k + 2])]) > 1) + # this tests whether the original clade has diversified, but this does not need to be the case + # it could also be a case of anagenesis followed by colonization, so this test seems inappropriate + # also, one should expect that colonizations in the data are all real colonizations; a colonization + # which is not followed by speciation but by recolonization should only occur once (the last colonization) + test_for_colonization <- TRUE } if(test_for_colonization) # new colonization or recolonization after speciation { diff --git a/R/DAISIE_loglik_high_lambda.R b/R/DAISIE_loglik_high_lambda.R index ccbb2d8b..a8164503 100644 --- a/R/DAISIE_loglik_high_lambda.R +++ b/R/DAISIE_loglik_high_lambda.R @@ -1,27 +1,30 @@ DAISIE_loglik_high_lambda <- function(pars1, brts, stac) { lbrts <- length(brts) if (brts[lbrts] == 0) { - brts <- brts[-lbrts] - lbrts <- length(brts) + brts <- brts[-lbrts] + lbrts <- length(brts) } - N <- lbrts - 1 - mu <- pars1[2] gam <- pars1[4] - brtsdiff <- brts - c(brts[2:(N + 1)], 0) if (stac == 0) { - out <- -gam * brts[1] - } - if (stac == 2) { - out <- -gam * brtsdiff[1] + - log(gam) + - log(N) + - (N - 1) * log(mu) + - lgamma(N) + - - (N - 1) * log(N - 1) + - - mu / (N - 1) * sum((1:N) * (0:(N - 1)) * brtsdiff[2:(N + 1)]) - } - if (stac == 1 | stac == 3 | stac == 4) { - out <- -Inf + out <- -gam * brts[1] + return(out) } + if (stac == 2 | stac == 6) { + N <- lbrts - 1 - (stac == 6) + mu <- pars1[2] + brtsdiff <- brts - c(brts[2:lbrts], 0) + out <- -gam * brtsdiff[1] + + log(gam) * (stac == 2) + + log(1 - exp(-gam * brtsdiff[2])) * (stac == 6) + + #log(-expm1(-gam * brtsdiff[2])) * (stac == 6) + + #log1p(-exp(-gam * brtsdiff[2])) * (stac == 6) + + log(N) + + (N - 1) * log(mu) + + lgamma(N) + + - (N - 1) * log(N - 1) + + - mu / (N - 1) * sum((1:N) * (0:(N - 1)) * brtsdiff[(2 + (stac == 6)):(N + 1 + (stac == 6))]) + return(out) + } else return(-Inf) # stac == 1, 3, 4, 5, 7, 8, 9 + #Stac 6 to be checked still return(out) } diff --git a/R/DAISIE_rates.R b/R/DAISIE_rates.R index ee8a574c..f4030306 100644 --- a/R/DAISIE_rates.R +++ b/R/DAISIE_rates.R @@ -607,7 +607,7 @@ get_trans_rate <- function(trait_pars, #' @keywords internal #' #' @author Joshua Lambert, Pedro Neves, Shu Xie -calc_next_timeval <- function(max_rates, timeval) { +calc_next_timeval <- function(max_rates, timeval, total_time) { # testit::assert(timeval >= 0) if (length(max_rates) == 4) { ## no considering about two trait states @@ -619,8 +619,12 @@ calc_next_timeval <- function(max_rates, timeval) { max_rates[[5]] + max_rates[[6]] + max_rates[[7]] + max_rates[[8]] + max_rates[[9]] + max_rates[[10]] } - dt <- stats::rexp(1, totalrate) - timeval <- timeval + dt + if (totalrate != 0) { + dt <- stats::rexp(1, totalrate) + timeval <- timeval + dt + } else { + timeval <- total_time + } return(list(timeval = timeval, dt = dt)) } @@ -639,11 +643,16 @@ calc_next_timeval <- function(max_rates, timeval) { #' @author Joshua Lambert, Pedro Neves, Shu Xie calc_next_timeval_shift <- function(max_rates, timeval, - dynamic_shift_times) { + dynamic_shift_times, + total_time) { # testit::assert(timeval >= 0) totalrate <- max_rates[[1]] + max_rates[[2]] + max_rates[[3]] + max_rates[[4]] - dt <- stats::rexp(1, totalrate) - timeval <- timeval + dt + if (totalrate != 0) { + dt <- stats::rexp(1, totalrate) + timeval <- timeval + dt + } else { + timeval <- total_time + } rate_shift <- FALSE if (timeval >= dynamic_shift_times[1]) { diff --git a/R/DAISIE_sim_core_cr.R b/R/DAISIE_sim_core_cr.R index 5da5b211..41f1c514 100644 --- a/R/DAISIE_sim_core_cr.R +++ b/R/DAISIE_sim_core_cr.R @@ -74,12 +74,13 @@ DAISIE_sim_core_cr <- function( timeval_and_dt <- calc_next_timeval( max_rates = rates, - timeval = timeval + timeval = timeval, + total_time = total_time ) timeval <- timeval_and_dt$timeval - if (timeval <= total_time) { + if (timeval < total_time) { rates <- update_rates( timeval = timeval, total_time = total_time, diff --git a/R/DAISIE_sim_core_cr_shift.R b/R/DAISIE_sim_core_cr_shift.R index 7379db30..02510dca 100644 --- a/R/DAISIE_sim_core_cr_shift.R +++ b/R/DAISIE_sim_core_cr_shift.R @@ -90,7 +90,8 @@ DAISIE_sim_core_cr_shift <- function( timeval_shift <- calc_next_timeval_shift( max_rates = rates, timeval = timeval, - dynamic_shift_times = dynamic_shift_times + dynamic_shift_times = dynamic_shift_times, + total_time = total_time ) timeval <- timeval_shift$timeval diff --git a/R/DAISIE_sim_core_time_dep.R b/R/DAISIE_sim_core_time_dep.R index f1388641..ad65de6f 100644 --- a/R/DAISIE_sim_core_time_dep.R +++ b/R/DAISIE_sim_core_time_dep.R @@ -86,7 +86,8 @@ DAISIE_sim_core_time_dep <- function( timeval_and_dt <- calc_next_timeval( max_rates = max_rates, - timeval = timeval + timeval = timeval, + total_time = total_time ) timeval <- timeval_and_dt$timeval diff --git a/R/DAISIE_sim_core_trait_dep.R b/R/DAISIE_sim_core_trait_dep.R index d35e5c87..65d68b3b 100644 --- a/R/DAISIE_sim_core_trait_dep.R +++ b/R/DAISIE_sim_core_trait_dep.R @@ -77,7 +77,8 @@ DAISIE_sim_core_trait_dep <- function( ) timeval_and_dt <- calc_next_timeval( max_rates = rates, - timeval = timeval + timeval = timeval, + total_time = total_time ) timeval <- timeval_and_dt$timeval diff --git a/R/DAISIE_sim_trait_dep.R b/R/DAISIE_sim_trait_dep.R index 4f575444..03481696 100644 --- a/R/DAISIE_sim_trait_dep.R +++ b/R/DAISIE_sim_trait_dep.R @@ -1,6 +1,6 @@ #' @title Simulate islands with given trait-dependent parameters. #' @description This function simulates islands with given cladogenesis, -#' extinction, K, immigration and anagenesis parameters for binary states. +#' extinction, K, immigration and anagenesis parameters for binary trait states. #' #' Returns R list object that contains the simulated islands #' @@ -8,7 +8,7 @@ #' #' @return #' A list. The highest level of the least corresponds to each individual -#' replciate. The first element of each replicate is composed of island +#' replicate. The first element of each replicate is composed of island #' information containing: #' \itemize{ #' \item{\code{$island_age}: A numeric with the island age.} @@ -115,7 +115,7 @@ DAISIE_sim_trait_dep <- function( } while (number_present < cond) { if(M == 0 || is.null(trait_pars)){ - stop("One state exist on mainland, should use constant rate DAISIE.") + stop("One state exists on mainland, should use constant rate DAISIE.") }else{ for (m_spec in 1:M) { ### M1 = 1, M2 = 0 diff --git a/R/DAISIE_utils.R b/R/DAISIE_utils.R index da83b360..f02a1aa6 100644 --- a/R/DAISIE_utils.R +++ b/R/DAISIE_utils.R @@ -439,10 +439,12 @@ add_column_to_dataframe <- function(df, position, column_to_insert) { if(is.character(position)) { position <- which(names(df) == position) } + rn <- row.names(df) df <- data.frame(df[1:position], nc = column_to_insert, df[(position + 1):ncol(df)]) names(df)[names(df) == 'nc'] <- names(column_to_insert) + row.names(df) <- rn return(df) } diff --git a/R/default_params_doc.R b/R/default_params_doc.R index 2ebaafe5..666d9502 100644 --- a/R/default_params_doc.R +++ b/R/default_params_doc.R @@ -420,6 +420,10 @@ #' incorrect output due to parameter transformation. #' @param num_cycles The number of cycles the optimizer will go through. #' Default is 1. +#' @param function_to_optimize Here one can indicate to use a function +#' ('DAISIE_approx') that approximates the DAISIE likelihood or the default +#' value 'DAISIE_exact'). The approximate likelihood can for example be used +#' in case the exact one fails. #' @param trait_pars A named list containing diversification rates considering #' two trait states created by \code{\link{create_trait_pars}}: #' \itemize{ diff --git a/README.md b/README.md index 67b5f693..963c7765 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ The model can be fitted to both empirical dated phylogenies and simulated data. * For an overview of the simulation functionality see [here](https://cran.r-project.org/web/packages/DAISIE/vignettes/demo_sim.html). * Details and an overview of the maximum likelihood inference capabilities to estimate parameters see [here](https://cran.r-project.org/web/packages/DAISIE/vignettes/demo_optimize.html). -* For details on comparing between two diversity depedence models see [here](https://cran.r-project.org/web/packages/DAISIE/vignettes/demo_CSvsIW.html). +* For details on comparing between two diversity dependence models see [here](https://cran.r-project.org/web/packages/DAISIE/vignettes/demo_CSvsIW.html). ## Installing DAISIE diff --git a/data/frogs_sim_datalist.RData b/data/frogs_sim_datalist.RData new file mode 100644 index 00000000..fa197a15 Binary files /dev/null and b/data/frogs_sim_datalist.RData differ diff --git a/man/DAISIE_ML.Rd b/man/DAISIE_ML.Rd index 3cf746dd..8004a6cf 100644 --- a/man/DAISIE_ML.Rd +++ b/man/DAISIE_ML.Rd @@ -30,7 +30,8 @@ DAISIE_ML_CS( verbose = 0, tolint = c(1e-16, 1e-10), jitter = 0, - num_cycles = 1 + num_cycles = 1, + function_to_optimize = "DAISIE_exact" ) } \arguments{ @@ -201,6 +202,11 @@ incorrect output due to parameter transformation.} \item{num_cycles}{The number of cycles the optimizer will go through. Default is 1.} + +\item{function_to_optimize}{Here one can indicate to use a function +('DAISIE_approx') that approximates the DAISIE likelihood or the default +value 'DAISIE_exact'). The approximate likelihood can for example be used +in case the exact one fails.} } \value{ The output is a dataframe containing estimated parameters and diff --git a/man/DAISIE_ML1.Rd b/man/DAISIE_ML1.Rd index 158de7c1..2cb61361 100644 --- a/man/DAISIE_ML1.Rd +++ b/man/DAISIE_ML1.Rd @@ -26,7 +26,8 @@ DAISIE_ML1( tolint = c(1e-16, 1e-10), island_ontogeny = NA, jitter = 0, - num_cycles = 1 + num_cycles = 1, + function_to_optimize = "DAISIE_exact" ) } \arguments{ @@ -184,6 +185,11 @@ incorrect output due to parameter transformation.} \item{num_cycles}{The number of cycles the optimizer will go through. Default is 1.} + +\item{function_to_optimize}{Here one can indicate to use a function +('DAISIE_approx') that approximates the DAISIE likelihood or the default +value 'DAISIE_exact'). The approximate likelihood can for example be used +in case the exact one fails.} } \value{ The output is a dataframe containing estimated parameters and diff --git a/man/DAISIE_sim_trait_dep.Rd b/man/DAISIE_sim_trait_dep.Rd index 1a3cb8e7..94cb76c8 100644 --- a/man/DAISIE_sim_trait_dep.Rd +++ b/man/DAISIE_sim_trait_dep.Rd @@ -153,7 +153,7 @@ same as `2` and prints intermediate progress during likelihood computation.} } \value{ A list. The highest level of the least corresponds to each individual -replciate. The first element of each replicate is composed of island +replicate. The first element of each replicate is composed of island information containing: \itemize{ \item{\code{$island_age}: A numeric with the island age.} @@ -184,7 +184,7 @@ not sampled for particular clade (only applicable for endemic clades)} } \description{ This function simulates islands with given cladogenesis, -extinction, K, immigration and anagenesis parameters for binary states. +extinction, K, immigration and anagenesis parameters for binary trait states. Returns R list object that contains the simulated islands } diff --git a/man/calc_next_timeval.Rd b/man/calc_next_timeval.Rd index 993ad3c1..def18a12 100644 --- a/man/calc_next_timeval.Rd +++ b/man/calc_next_timeval.Rd @@ -4,7 +4,7 @@ \alias{calc_next_timeval} \title{Calculates when the next timestep will be.} \usage{ -calc_next_timeval(max_rates, timeval) +calc_next_timeval(max_rates, timeval, total_time) } \arguments{ \item{max_rates}{named list of max rates as returned by diff --git a/man/calc_next_timeval_shift.Rd b/man/calc_next_timeval_shift.Rd index 3e27b462..6860ca35 100644 --- a/man/calc_next_timeval_shift.Rd +++ b/man/calc_next_timeval_shift.Rd @@ -4,7 +4,7 @@ \alias{calc_next_timeval_shift} \title{Calculates when the next timestep will be, and if a shift has occured.} \usage{ -calc_next_timeval_shift(max_rates, timeval, dynamic_shift_times) +calc_next_timeval_shift(max_rates, timeval, dynamic_shift_times, total_time) } \arguments{ \item{max_rates}{named list of max rates as returned by diff --git a/man/default_params_doc.Rd b/man/default_params_doc.Rd index 5665810c..9ce49557 100644 --- a/man/default_params_doc.Rd +++ b/man/default_params_doc.Rd @@ -737,6 +737,11 @@ DAISIEprep. Can also be simulated data generated with DAISIE_sim function.} \item{sort_clade_sizes}{Default sort_clade_sizes = T outputs clade sizes sorted in ascending order of number of species. sort_clade_sizes=F outputs clade sizes in the same order as they appear in the input datalist.} + +\item{function_to_optimize}{Here one can indicate to use a function +('DAISIE_approx') that approximates the DAISIE likelihood or the default +value 'DAISIE_exact'). The approximate likelihood can for example be used +in case the exact one fails.} } \value{ Nothing diff --git a/man/frogs_sim_datalist.Rd b/man/frogs_sim_datalist.Rd new file mode 100644 index 00000000..929c8365 --- /dev/null +++ b/man/frogs_sim_datalist.Rd @@ -0,0 +1,49 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/DAISIE_data.R +\docType{data} +\name{frogs_sim_datalist} +\alias{frogs_sim_datalist} +\title{Colonization and branching times of a data set simulated with the +MLE parameters of frogs_datalist} +\format{ +A list with the first element containing 2 elements and the following +elements containing 5 components. +} +\source{ +Etienne RS, Haegeman B, Dugo-Cota A, Vila C, Gonzalez-Voyer A & +Valente L. The limits to ecological limits to diversification.\cr +} +\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}} +} +\keyword{datasets} diff --git a/tests/testthat/test-DAISIE_ML1.R b/tests/testthat/test-DAISIE_ML1.R index 291e783f..7b32743d 100644 --- a/tests/testthat/test-DAISIE_ML1.R +++ b/tests/testthat/test-DAISIE_ML1.R @@ -1,8 +1,9 @@ context("DAISIE_ML1") -test_that("use", { +test_that("DAISIE_ML1 works and simplex and subplex give the same answer", { skip_if(Sys.getenv("CI") == "", message = "Run only on CI") skip_on_cran() + set.seed(42) data(Galapagos_datalist) datalist <- Galapagos_datalist initparsopt <- c(2.5, 2.7, 20, 0.009, 1.01) @@ -10,29 +11,60 @@ test_that("use", { idparsopt <- c(1,2,3,4,5) parsfix <- c() idparsfix <- c() - tested_MLE <- DAISIE_ML1( - datalist = datalist, - initparsopt = initparsopt, - idparsopt = idparsopt, - parsfix = parsfix, - ddmodel = ddmodel, - idparsfix = idparsfix, - verbose = 0, - tol = c(0.01, 0.1, 0.001), - res = 15, - tolint = c(0.1, 0.01) - ) + tested_MLE1 <- DAISIE_ML1( + datalist = datalist, + initparsopt = initparsopt, + idparsopt = idparsopt, + parsfix = parsfix, + ddmodel = ddmodel, + idparsfix = idparsfix, + verbose = 0, + methode = 'odeint::runge_kutta_cash_karp54', + tol = c(1e-04, 1e-05, 1e-07), + res = 15, + tolint = c(1E-16, 1E-10), + optimmethod = 'subplex', + num_cycles = 4) + tested_MLE2 <- DAISIE_ML1( + datalist = datalist, + initparsopt = as.numeric(tested_MLE1[1:5]), + idparsopt = idparsopt, + parsfix = parsfix, + ddmodel = ddmodel, + idparsfix = idparsfix, + verbose = 0, + methode = 'odeint::runge_kutta_cash_karp54', + tol = c(1e-04, 1e-05, 1e-07), + res = 15, + tolint = c(1E-16, 1E-10), + optimmethod = 'simplex', + num_cycles = 1) + tested_MLE3 <- DAISIE_ML1( + datalist = datalist, + initparsopt = as.numeric(tested_MLE2[1:5]), + idparsopt = idparsopt, + parsfix = parsfix, + ddmodel = ddmodel, + idparsfix = idparsfix, + verbose = 0, + methode = 'odeint::runge_kutta_cash_karp54', + tol = c(1e-04, 1e-05, 1e-07), + res = 15, + tolint = c(1E-16, 1E-10), + optimmethod = 'subplex', + num_cycles = 1) + testthat::expect_equal(tested_MLE2, tested_MLE3, tolerance = 1E-6) expected_MLE <- data.frame( - lambda_c = 3.689104200780465, - mu = 4.31030299415995, - K = 906.6501180193454, - gamma = 0.0173458887696076, - lambda_a = 3.677789527566334, - loglik = -64.2199684450019, + lambda_c = 1.8704396748021859, + mu = 1.9365019587293075, + K = Inf, + gamma = 0.0073959695777554, + lambda_a = 1.1636058561427665, + loglik = -76.7923936480216014, df = 5L, conv = 0L ) - testthat::expect_equal(tested_MLE, expected_MLE) + testthat::expect_equal(tested_MLE3, expected_MLE, tolerance = 1E-6) }) test_that("abuse", { diff --git a/tests/testthat/test-DAISIE_ML2.R b/tests/testthat/test-DAISIE_ML2.R index 38635aa0..c310a0cc 100644 --- a/tests/testthat/test-DAISIE_ML2.R +++ b/tests/testthat/test-DAISIE_ML2.R @@ -19,38 +19,27 @@ test_that("use", { idparsopt = c(2, 4, 5, 6, 7), parsfix = c(0, Inf), idparsfix = c(1, 3), + methode = 'odeint::runge_kutta_cash_karp54', tol = c(0.01, 0.1, 0.001), res = 15, - tolint = c(0.1, 0.01), + tolint = c(1E-16, 1E-10), verbose = 0 ) expected_MLE <- data.frame( lambda_c = c(0.0, - 0.315015693879739, + 0.1198600880941345, 0.0, 0.0), - mu = c(2.67928476648249, - 2.67928476648249, - 2.67928476648249, - 2.67928476648249), - K = c(Inf, - Inf, - Inf, - Inf), - gamma = c(0.141748213992604, - 0.338044376412124, - 0.141748213992604, - 0.141748213992604), - lambda_a = c(1.10809210249609, - 1.10809210249609, - 1.10809210249609 , - 1.10809210249609), - loglik = c(-409.60117813893, - -409.60117813893, - -409.60117813893, - -409.60117813893), - df = c(5L, 5L, 5L, 5L), - conv = c(0L, 0L, 0L, 0L) + mu = rep(1.0604828698048443,4), + K = rep(Inf,4), + gamma = c(0.0544106971300572, + 0.1706160417441286, + 0.0544106971300572, + 0.0544106971300572), + lambda_a = rep(0.4749396123469573,4), + loglik = rep(-449.6525311168169310,4), + df = rep(5L,4), + conv = rep(0L,4) ) testthat::expect_equal(tested_MLE, expected_MLE) }) diff --git a/tests/testthat/test-DAISIE_format_IW.R b/tests/testthat/test-DAISIE_format_IW.R index d257aa3a..fcdfab0e 100644 --- a/tests/testthat/test-DAISIE_format_IW.R +++ b/tests/testthat/test-DAISIE_format_IW.R @@ -94,11 +94,11 @@ test_that("silent with non-empty island with correct output", { brts_table <- matrix(ncol = 5, nrow = 6) colnames(brts_table) <- c("brt", "clade", "event", "endemic", "col") brts_table[1, ] <- c(1, 0, 0, NA, NA) - brts_table[2, ] <- c(0.9244818166871660, 1, 1, 1, NA) - brts_table[3, ] <- c(0.9105856673960619, 1, 2, 1, NA) - brts_table[4, ] <- c(0.5557734125062590, 2, 1, 0, NA) - brts_table[5, ] <- c(0.5288428248966160, 3, 1, 0, NA) - brts_table[6, ] <- c(0.3146835586399670, 1, 3, 1, NA) + brts_table[2, ] <- c(0.9244818166871660, 1, 1, 1, 1) + brts_table[3, ] <- c(0.9105856673960619, 1, 2, 1, 1) + brts_table[4, ] <- c(0.5557734125062590, 2, 1, 0, 1) + brts_table[5, ] <- c(0.5288428248966160, 3, 1, 0, 1) + brts_table[6, ] <- c(0.3146835586399670, 1, 3, 1, 1) expected_IW_format[[1]][[1]] <- list(island_age = 1, not_present = 7, stt_all = stt_all, @@ -255,8 +255,8 @@ test_that("silent with non-empty nonoceanic island with brts_table <- matrix(ncol = 5, nrow = 3) colnames(brts_table) <- c("brt", "clade", "event", "endemic", "col") brts_table[1, ] <- c(1, 0, 0, NA, NA) - brts_table[2, ] <- c(1, 2, 1, 1, NA) - brts_table[3, ] <- c(1, 1, 1, 1, NA) + brts_table[2, ] <- c(1, 2, 1, 1, 1) + brts_table[3, ] <- c(1, 1, 1, 1, 1) expected_IW_format[[1]][[1]] <- list(island_age = 1, not_present = 8, stt_all = stt_all, @@ -316,8 +316,8 @@ test_that("silent with non-empty nonoceanic island with brts_table <- matrix(ncol = 5, nrow = 3) colnames(brts_table) <- c("brt", "clade", "event", "endemic", "col") brts_table[1, ] <- c(1, 0, 0, NA, NA) - brts_table[2, ] <- c(1, 2, 1, 1, NA) - brts_table[3, ] <- c(1, 1, 1, 1, NA) + brts_table[2, ] <- c(1, 2, 1, 1, 1) + brts_table[3, ] <- c(1, 1, 1, 1, 1) expected_IW_format[[1]][[1]] <- list(island_age = 1, not_present = 8, stt_all = stt_all, @@ -385,10 +385,10 @@ test_that("add_brt_table output is correct when length(island) != 1", { brt_table <- matrix(ncol = 5, nrow = 5) colnames(brt_table) <- c("brt", "clade", "event", "endemic", "col") brt_table[1, ] <- c(1, 0, 0, NA, NA) - brt_table[2, ] <- c(0.9244818, 1, 1, 1, NA) - brt_table[3, ] <- c(0.9105857, 1, 2, 1, NA) - brt_table[4, ] <- c(0.5557734, 2, 1, 0, NA) - brt_table[5, ] <- c(0.3146836, 1, 3, 1, NA) + brt_table[2, ] <- c(0.9244818, 1, 1, 1, 1) + brt_table[3, ] <- c(0.9105857, 1, 2, 1, 1) + brt_table[4, ] <- c(0.5557734, 2, 1, 0, 1) + brt_table[5, ] <- c(0.3146836, 1, 3, 1, 1) expected_brt <- list() expected_brt[[1]] <- list(island_age = 1, not_present = 3, @@ -540,9 +540,9 @@ test_that("silent when species with two trait states with brts_table <- matrix(ncol = 5, nrow = 4) colnames(brts_table) <- c("brt", "clade", "event", "endemic", "col") brts_table[1, ] <- c(5.00000000000000, 0, 0, NA, NA) - brts_table[2, ] <- c(3.10261367452990, 1, 1, 1, NA) - brts_table[3, ] <- c(1.50562999775257, 2, 1, 1, NA) - brts_table[4, ] <- c(1.26245655913561, 2, 2, 1, NA) + brts_table[2, ] <- c(3.10261367452990, 1, 1, 1, 1) + brts_table[3, ] <- c(1.50562999775257, 2, 1, 1, 1) + brts_table[4, ] <- c(1.26245655913561, 2, 2, 1, 1) expected_IW_format[[1]][[1]] <- list(island_age = 5, not_present = 13, stt_all = stt_all, diff --git a/tests/testthat/test-DAISIE_format_IW_full_stt.R b/tests/testthat/test-DAISIE_format_IW_full_stt.R index 6cd03312..6b5f6a31 100644 --- a/tests/testthat/test-DAISIE_format_IW_full_stt.R +++ b/tests/testthat/test-DAISIE_format_IW_full_stt.R @@ -67,7 +67,7 @@ test_that("complete stt, 1 type, no geodynamics, oceanic island, one trait state c(Time = 0.0, nI = 0.0, nA = 1.0, nC = 0.0) ) expected_brts_table <- matrix( - c(1.0, 0.24481816687165, 0, 1, 0, 1, NA, 1, NA, NA), + c(1.0, 0.24481816687165, 0, 1, 0, 1, NA, 1, NA, 1), nrow = 2 ) colnames(expected_brts_table) <- c("brt", "clade", "event", "endemic", "col") @@ -109,7 +109,7 @@ test_that("complete stt, 1 type, no geodynamics, oceanic island, one trait state ) expected_brts_table <- matrix( - c(1.0, 0.741771912202239, 0, 1, 0, 1, NA, 0, NA, NA), + c(1.0, 0.741771912202239, 0, 1, 0, 1, NA, 0, NA, 1), nrow = 2 ) colnames(expected_brts_table) <- c("brt", "clade", "event", "endemic", "col") @@ -245,7 +245,7 @@ test_that("complete stt, 1 type, geodynamics, oceanic island, one trait state ) testthat::expect_equal( formatted_IW_sim[[1]][[1]]$brts_table[5, ], - c(brt = 0.83094531417507, clade = 4, event = 1, endemic = 1, col = NA) + c(brt = 0.83094531417507, clade = 4, event = 1, endemic = 1, col = 1) ) }) diff --git a/tests/testthat/test-DAISIE_format_IW_sampled_stt.R b/tests/testthat/test-DAISIE_format_IW_sampled_stt.R index 34ff22bf..8424f9a4 100644 --- a/tests/testthat/test-DAISIE_format_IW_sampled_stt.R +++ b/tests/testthat/test-DAISIE_format_IW_sampled_stt.R @@ -59,9 +59,9 @@ test_that("sampled stt, 1 type, no geodynamics, oceanic island (same arguments a expected_brts_table <- matrix( data = c( 1.0, 0.0, 0.0, NA, NA, - 0.244818166871655, 1, 1, 1, NA, - 0.173128288990374, 1, 2, 1, NA, - 0.029668240213840, 1, 3, 1, NA + 0.244818166871655, 1, 1, 1, 1, + 0.173128288990374, 1, 2, 1, 1, + 0.029668240213840, 1, 3, 1, 1 ), ncol = 5, byrow = TRUE diff --git a/tests/testthat/test-integration_DAISIE.R b/tests/testthat/test-integration_DAISIE.R index 857af43a..fd29255f 100644 --- a/tests/testthat/test-integration_DAISIE.R +++ b/tests/testthat/test-integration_DAISIE.R @@ -100,6 +100,99 @@ test_that("IW and CS loglik is same when K = Inf", { testthat::expect_equal(loglik_IW, loglik_CS, tol = 5E-6) }) +test_that("IW loglik is correct", { + skip_if(Sys.getenv("CI") == "" && !(Sys.getenv("USERNAME") == "rampa"), + message = "Run only on CI") + skip_on_cran() + frogs_datalist <- NULL + rm(frogs_datalist) + data(frogs_datalist, package = "DAISIE") + M <- 1000 + frogs_datalist[[1]]$not_present <- frogs_datalist[[1]]$not_present + (M - 300) + ddmodel <- 11 + initparsopt <- c(4.012298e-01,1.699521e-01,1.319595e+02,3.487955e-04) + idparsopt <- c(1,2,3,4) + parsfix <- 0 + idparsfix <- 5 + verbose <- 1 + cond <- 1 + res <- 200 + methode <- 'odeint::runge_kutta_fehlberg78' + tolint <- c(1E-16, 1E-14) + + trparsopt <- initparsopt / (1 + initparsopt) + trparsopt[which(initparsopt == Inf)] <- 1 + trparsfix <- parsfix / (1 + parsfix) + trparsfix[which(parsfix == Inf)] <- 1 + pars2 <- c(res, ddmodel, cond, verbose) + initloglik_IW <- DAISIE_loglik_IW_choosepar(trparsopt = trparsopt, + trparsfix = trparsfix, + idparsopt = idparsopt, + idparsfix = idparsfix, + M = M, + pars2 = pars2, + datalist = frogs_datalist, + methode = methode, + abstolint = tolint[1], + reltolint = tolint[2]) + + testthat::expect_equal(initloglik_IW, -215.097677998973, tol = 1E-6) +}) + +test_that("IW loglik does not error when there is recolonization", { + skip_if(Sys.getenv("CI") == "" && !(Sys.getenv("USERNAME") == "rampa"), + message = "Run only on CI") + skip_on_cran() + frogs_sim_datalist <- NULL + rm(frogs_sim_datalist) + data(frogs_sim_datalist, package = "DAISIE") + #This is data set 897 from the CS simulated data sets + M <- 1000 + ddmodel <- 11 + initparsopt <- c(4.012298e-01,1.699521e-01,1.319595e+02,3.487955e-04) + idparsopt <- c(1,2,3,4) + parsfix <- 0 + idparsfix <- 5 + verbose <- 1 + cond <- 1 + res <- 200 + methode <- 'odeint::runge_kutta_fehlberg78' + tolint <- c(1E-16, 1E-14) + + trparsopt <- initparsopt / (1 + initparsopt) + trparsopt[which(initparsopt == Inf)] <- 1 + trparsfix <- parsfix / (1 + parsfix) + trparsfix[which(parsfix == Inf)] <- 1 + pars2 <- c(res, ddmodel, cond, verbose) + initloglik_IW <- DAISIE_loglik_IW_choosepar(trparsopt = trparsopt, + trparsfix = trparsfix, + idparsopt = idparsopt, + idparsfix = idparsfix, + M = M, + pars2 = pars2, + datalist = frogs_sim_datalist, + methode = methode, + abstolint = tolint[1], + reltolint = tolint[2]) + + testthat::expect_equal(initloglik_IW, -244.7340301475871911, tol = 1E-6) + + frogs_sim_datalist2 <- frogs_sim_datalist + frogs_sim_datalist2[[2]]$all_colonisations[[2]]$event_times <- c(frogs_sim_datalist2[[2]]$all_colonisations[[2]]$event_times, 5) + initloglik_IW <- DAISIE_loglik_IW_choosepar(trparsopt = trparsopt, + trparsfix = trparsfix, + idparsopt = idparsopt, + idparsfix = idparsfix, + M = M, + pars2 = pars2, + datalist = frogs_sim_datalist2, + methode = methode, + abstolint = tolint[1], + reltolint = tolint[2]) + + testthat::expect_equal(initloglik_IW, -246.8143677718335880, tol = 1E-6) +}) + test_that("DAISIE_ML simple case works", { skip_if(Sys.getenv("CI") == "" && !(Sys.getenv("USERNAME") == "rampa"), message = "Run only on CI") @@ -231,6 +324,25 @@ test_that("DAISIE_ML with nonzero probability of initial presence gives testthat::expect_false(isTRUE(all.equal(tested_mle_zero, tested_mle_nonzero))) }) +test_that("DAISIE_ML gives a -Inf loglikelhood when probability of initial + presence is nonzero and colonization rate equals 0 for Galapagos", { + skip_if(Sys.getenv("CI") == "" && !(Sys.getenv("USERNAME") == "rampa"), + message = "Run only on CI") + skip_on_cran() + + utils::data(Galapagos_datalist) + tested_mle <- DAISIE_ML( + datalist = Galapagos_datalist, + initparsopt = c(2.5, 2.7, 20, 0, 1.01, 0.001), + ddmodel = 11, + idparsopt = 1:6, + parsfix = NULL, + idparsfix = NULL + ) + testthat::expect_true(is.na(tested_mle$loglik)) + }) + + test_that("DAISIE_ML simple case works with estimating probability of initial presence", { skip_if(Sys.getenv("CI") == "" && !(Sys.getenv("USERNAME") == "rampa"), diff --git a/vignettes/demo_CSvsIW.R b/vignettes/demo_CSvsIW.R index c09231b0..25992bf1 100644 --- a/vignettes/demo_CSvsIW.R +++ b/vignettes/demo_CSvsIW.R @@ -87,21 +87,21 @@ readRDS(file = system.file("extdata", "frog_M1_ML.rds", package = "DAISIE", must ## ----DAISIE_sim_CS, results='hide'-------------------------------------------- set.seed(1) frog_sims_CS <- DAISIE_sim_cr( - time=30, - M=300, - pars=c(0.44,0.11,36.44,0.0007,0), + time = 30, + M = 300, + pars = c(0.44, 0.11, 36.44, 0.0007, 0), divdepmodel = "CS", - replicates= 100, + replicates = 100, plot_sims = FALSE) ## ----DAISIE_sim_IW, results='hide'-------------------------------------------- set.seed(1) frog_sims_CS <- DAISIE_sim_cr( - time=30, - M=300, - pars=c(0.40,0.17,131.83,0.0012,0), + time = 30, + M = 300, + pars = c(0.40, 0.17, 131.83, 0.0012, 0), divdepmodel = "IW", - replicates= 100, + replicates = 100, plot_sims = FALSE) ## ---- echo=TRUE---------------------------------------------------------------