diff --git a/.Rbuildignore b/.Rbuildignore index 5279f53..8de32c8 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -1,11 +1,15 @@ ^.*\.Rproj$ ^\.Rproj\.user$ cran-comments.md +CONTRIBUTING.md +codemeta.json ^revdep$ ^README\.Rmd$ ^README-.*\.png$ -^NEWS\.md$ ^LICENSE\.md$ ^examples$ ^\.travis\.yml$ ^\.github$ +^doc$ +^Meta$ +^codemeta\.json$ diff --git a/.github/workflows/check-standard.yaml b/.github/workflows/R-CMD-check.yaml similarity index 67% rename from .github/workflows/check-standard.yaml rename to .github/workflows/R-CMD-check.yaml index 52c2b27..ee65ccb 100644 --- a/.github/workflows/check-standard.yaml +++ b/.github/workflows/R-CMD-check.yaml @@ -1,5 +1,9 @@ # Workflow derived from https://github.com/r-lib/actions/tree/v2/examples # Need help debugging build failures? Start at https://github.com/r-lib/actions#where-to-find-help +# +# NOTE: This workflow is overkill for most R packages and +# check-standard.yaml is likely a better choice. +# usethis::use_github_action("check-standard") will install it. on: push: branches: [main, master] @@ -18,18 +22,27 @@ jobs: fail-fast: false matrix: config: - - {os: macOS-latest, r: 'release'} + - {os: macos-latest, r: 'release'} + - {os: windows-latest, r: 'release'} + # Use 3.6 to trigger usage of RTools35 + - {os: windows-latest, r: '3.6'} + # use 4.1 to check with rtools40's older compiler + - {os: windows-latest, r: '4.1'} + - {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'} - {os: ubuntu-latest, r: 'release'} - {os: ubuntu-latest, r: 'oldrel-1'} + - {os: ubuntu-latest, r: 'oldrel-2'} + - {os: ubuntu-latest, r: 'oldrel-3'} + - {os: ubuntu-latest, r: 'oldrel-4'} env: GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} R_KEEP_PKG_SOURCE: yes steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v3 - uses: r-lib/actions/setup-pandoc@v2 diff --git a/.github/workflows/pkgcheck.yaml b/.github/workflows/pkgcheck.yaml deleted file mode 100644 index caf2b00..0000000 --- a/.github/workflows/pkgcheck.yaml +++ /dev/null @@ -1,22 +0,0 @@ -name: pkgcheck - -# This will cancel running jobs once a new run is triggered -concurrency: - group: ${{ github.workflow }}-${{ github.head_ref }} - cancel-in-progress: true - -on: - # Manually trigger the Action under Actions/pkgcheck - workflow_dispatch: - # Run on every push to master - push: - branches: - - master - -jobs: - check: - runs-on: ubuntu-latest - steps: - - uses: ropensci-review-tools/pkgcheck-action@main - with: - summary-only: false diff --git a/.gitignore b/.gitignore index 96e6265..80331cb 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,7 @@ mobsim.Rproj revdep/ .Rproj.user mobsim_package.Rproj + +.DS_Store +/doc/ +/Meta/ diff --git a/DESCRIPTION b/DESCRIPTION index 274173a..9745445 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,11 +1,16 @@ Package: mobsim Type: Package Title: Spatial Simulation and Scale-Dependent Analysis of Biodiversity Changes -Version: 0.2.0.9015 -Authors@R: c(person("Felix", "May", email = "felix.may@posteo.de", role = c("aut","cre")), - person("Alban", "Sagouis", email = "alban.sagouis@idiv.de", role = c("aut")) - ) -Date: 2022-03-04 +Version: 0.3.0 +Authors@R: c( + person("Felix", "May", email = "felix.may@fu-berlin.de", + role = c("aut", "cre"), + comment = c(ORCID = "0000-0002-1106-8188")), + person("Alban", "Sagouis", email = "alban.sagouis@idiv.de", + role = "aut", + comment = c(ORCID = "0000-0002-3827-1063")) + ) +Date: 2023-12-12 Description: Tools for the simulation, analysis and sampling of spatial biodiversity data (May et al. 2017) . In the simulation tools user define the numbers of @@ -14,7 +19,6 @@ Description: Tools for the simulation, analysis and sampling of spatial and accumulation curves, species-area relationships and the distance decay of similarity. License: GPL (>= 3) -LazyData: TRUE Imports: Rcpp, vegan, @@ -22,7 +26,8 @@ Imports: grDevices, utils, graphics, - stats + stats, + methods LinkingTo: Rcpp Suggests: @@ -31,9 +36,10 @@ Suggests: testthat (>= 3.0.0), mockery, knitr Config/testthat/edition: 3 +Config/testthat/parallel: true VignetteBuilder: knitr -RoxygenNote: 7.1.2 +RoxygenNote: 7.2.3 +Language: en-GB Encoding: UTF-8 -Source: https://prograide.com/pregunta/73922/avertissement-concernant-lutf-8-avec-roxygen2 URL: https://github.com/AlbanSagouis/mobsim BugReports: https://github.com/AlbanSagouis/mobsim diff --git a/NAMESPACE b/NAMESPACE index 72158e9..875de75 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,5 +32,7 @@ export(sim_thomas_coords) export(spec_sample) export(spec_sample_curve) importFrom(Rcpp,sourceCpp) +importFrom(methods,is) +importFrom(sads,rsad) useDynLib(mobsim) useDynLib(mobsim, .registration = TRUE) diff --git a/NEWS.md b/NEWS.md index c8cec50..dbe995e 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,4 +1,4 @@ -mobsim 0.2.1 +mobsim 0.3.0 ================================================================================ ### POTENTIAL CODE BREAK diff --git a/R/Diversity_Area_Relationships.R b/R/Diversity_Area_Relationships.R index 9e19e31..383ad79 100644 --- a/R/Diversity_Area_Relationships.R +++ b/R/Diversity_Area_Relationships.R @@ -64,12 +64,12 @@ div_rect <- function(x0, y0, xsize, ysize, comm) abund <- abund[abund > 0] relabund <- abund/sum(abund) - shannon <- - sum(relabund * log(relabund)) + shannon <- -sum(relabund * log(relabund)) n <- sum(abund) if (n > 1) #simpson <- (n/(n-1)) * (1 - sum(relabund^2)) - simpson <- 1- sum(relabund^2) + simpson <- 1 - sum(relabund^2) else simpson <- NA @@ -123,12 +123,11 @@ div_rand_rect <- function(prop_area = 0.25, comm, n_rect = 100, area <- dx_plot * dy_plot * prop_area square_size <- sqrt(area) - if (square_size <= min(c(dx_plot, dy_plot))){ + if (square_size <= min(c(dx_plot, dy_plot))) { dx_rect <- square_size dy_rect <- square_size - } else - { - if (dx_plot >= dy_plot){ + } else { + if (dx_plot >= dy_plot) { dx_rect <- dx_plot*prop_area dy_rect <- dy_plot } else { @@ -143,7 +142,7 @@ div_rand_rect <- function(prop_area = 0.25, comm, n_rect = 100, max = comm$y_min_max[2] - dy_rect) div_plots <- mapply(div_rect, xpos, ypos, - MoreArgs=list(xsize = dx_rect, ysize = dy_rect, + MoreArgs = list(xsize = dx_rect, ysize = dy_rect, comm = comm)) if (exclude_zeros == T) div_plots <- div_plots[, div_plots["n_species",] > 0] @@ -204,7 +203,7 @@ divar <- function(comm, prop_area = seq(0.1, 1, by = 0.1), n_samples = 100, warning("Subplot areas larger than the community size are ignored!") prop_area <- prop_area[prop_area <= 1] - if (class(comm) != "community") + if (!is(comm, "community")) stop("DiVAR requires a community object as input. See ?community.") n_scales <- length(prop_area) @@ -292,7 +291,7 @@ abund_rect <- function(x0, y0, xsize, ysize, comm) y <- comm$census$y # logical vector which trees are in the sampling rectangle - in_rect <- (x >= x0 & x < (x0+xsize) & y >= y0 & y < (y0+ysize)) + in_rect <- (x >= x0 & x < (x0 + xsize) & y >= y0 & y < (y0 + ysize)) abund <- table(comm$census$species[in_rect]) return(abund) @@ -318,6 +317,7 @@ abund_rect <- function(x0, y0, xsize, ysize, comm) #' dd1 <- dist_decay(sim_com1, prop_area = 0.005, n_samples = 20) #' plot(dd1) #' +#' @importFrom methods is #'@export #' dist_decay <- function(comm, prop_area = 0.005, n_samples = 20, @@ -329,14 +329,15 @@ dist_decay <- function(comm, prop_area = 0.005, n_samples = 20, if (length(prop_area) != 1L) stop("prop_area has to be of length 1") - if (class(comm) != "community") + if (!is(comm, "community")) stop("dist_decay requires a community object as input. See ?community.") dx_plot <- comm$x_min_max[2] - comm$x_min_max[1] dy_plot <- comm$y_min_max[2] - comm$y_min_max[1] area <- dx_plot * dy_plot * prop_area - samples1 <- sample_quadrats(comm, n_quadrats = n_samples, quadrat_area = area, + samples1 <- sample_quadrats(comm, n_quadrats = n_samples, + quadrat_area = area, avoid_overlap = T, plot = F) com_mat <- samples1$spec_dat[rowSums(samples1$spec_dat) > 0,] d <- stats::dist(samples1$xy_dat[rowSums(samples1$spec_dat) > 0,]) diff --git a/R/RcppExports.R b/R/RcppExports.R index 9693a9b..403c12d 100644 --- a/R/RcppExports.R +++ b/R/RcppExports.R @@ -6,13 +6,13 @@ sSAC1_C <- function(x, y, id_spec) { } #' Thomas process individual distribution simulation for one species -#' +#' #' Usually used internally inside \code{\link{sim_thomas_coords}} #' This function randomly draws points (individuals) around one or several mother points using Rcpp. #' The function is an efficient re-implementation of the rThomas function from the spatstat package. -#' +#' #' @name rThomas_rcpp -#' +#' #' @param n_points The total number of points (individuals). #' @param n_mother_points Number of mother points (= cluster centres). #' @param xmother Vector of \code{n_mother_points} x coordinates for the mother points. diff --git a/R/SAC_spatial.R b/R/SAC_spatial.R index 4e46c95..fce4507 100644 --- a/R/SAC_spatial.R +++ b/R/SAC_spatial.R @@ -108,22 +108,23 @@ rare_curve <- function(abund_vec) #' plot(sac1) #' #' @export +#' @importFrom methods is #' spec_sample_curve <- function(comm, method = c("accumulation" ,"rarefaction")) { - if (class(comm) != "community") + if (!is(comm, "community")) stop("spec_sample_curve requires a community object as input. See ?community.") out_dat <- data.frame(n = 1:nrow(comm$census)) method <- match.arg(method, several.ok = TRUE) - if ("accumulation" %in% method){ + if ("accumulation" %in% method) { out_dat$spec_accum <- sSAC1_C(comm$census$x, comm$census$y, as.integer(comm$census$species)) } - if ("rarefaction" %in% method){ + if ("rarefaction" %in% method) { abund <- community_to_sad(comm) out_dat$spec_rarefied <- rare_curve(abund) @@ -156,19 +157,19 @@ plot.spec_sample_curve <- function(x, ...) ylab = "Expected no.of species", main = "Species sampling curves", ...) - if (ncol(x) == 2){ - if (names(x)[2] == "spec_accum"){ + if (ncol(x) == 2) { + if (names(x)[2] == "spec_accum") { legend_text <- c("Accumulation") line_col <- "red" } - if (names(x)[2] == "spec_rarefied"){ + if (names(x)[2] == "spec_rarefied") { legend_text <- c("Rarefaction") line_col <- "blue" } graphics::lines(x[[1]], x[[2]], col = line_col) } - if (ncol(x) == 3){ + if (ncol(x) == 3) { legend_text <- c("Accumulation","Rarefaction") line_col <- c("red","blue") graphics::lines(x[[1]], x[[2]], col = line_col[1]) @@ -177,4 +178,3 @@ plot.spec_sample_curve <- function(x, ...) graphics::legend("bottomright", legend = legend_text, lty = 1, col = line_col) } - diff --git a/R/Sample_quadrats.R b/R/Sample_quadrats.R index 1dbe637..05420d9 100644 --- a/R/Sample_quadrats.R +++ b/R/Sample_quadrats.R @@ -33,7 +33,7 @@ #' specnumber(comm_mat1$spec_dat) #' diversity(comm_mat1$spec_dat, index = "shannon") #' -#' +#' @importFrom methods is #' @export #' sample_quadrats <- function(comm, n_quadrats = 20, quadrat_area = 0.01, @@ -42,7 +42,7 @@ sample_quadrats <- function(comm, n_quadrats = 20, quadrat_area = 0.01, x0 = 0, y0 = 0, delta_x = 0.1, delta_y = 0.1, seed = NULL) { - if (class(comm) != "community") + if (!is(comm, "community")) stop("comm has to be a community object") if (round(n_quadrats, 0) != n_quadrats) stop("n_quadrats has to be an integer") diff --git a/R/Sim_Community.R b/R/Sim_Community.R index 649d1fc..471aef5 100644 --- a/R/Sim_Community.R +++ b/R/Sim_Community.R @@ -115,7 +115,7 @@ #' # Different important SAD models #' #' # Fisher's log-series -#' sad_logseries <- sim_sad(s_pool = NULL, n_sim = 10000, sad_type = "ls", +#' sad_logseries <- sim_sad(s_pool = NULL, n_sim = NULL, sad_type = "ls", #' sad_coef = list("N" = 1e5, "alpha" = 20)) #' #' # Poisson log-normal @@ -123,7 +123,7 @@ #' sad_coef = list("mu" = 5, "sig" = 0.5)) #' #' # Mac-Arthur's broken stick -#' sad_broken_stick <- sim_sad(s_pool = NULL, n_sim = 10000, sad_type = "bs", +#' sad_broken_stick <- sim_sad(s_pool = NULL, n_sim = NULL, sad_type = "bs", #' sad_coef = list("N" = 1e5, "S" = 100)) #' #' # Plot all SADs together as rank-abundance curves @@ -133,7 +133,8 @@ #' lines(1:length(sad_broken_stick), sad_broken_stick, type = "b", col = 4) #' legend("topright", c("Log-series","Log-normal","Poisson log-normal","Broken stick"), #' col = 1:4, pch = 1) -#' +#' @importFrom methods is +#' @importFrom sads rsad #' @export sim_sad <- function(s_pool = NULL, n_sim = NULL, @@ -151,11 +152,12 @@ sim_sad <- function(s_pool = NULL, n_sim = NULL, # Handles parameters that give the community size + assertions ---- if (sad_type %in% c("bs", "ls", "mzsm")) { - S <- switch(sad_type, - bs = sad_coef$S, - ls = sad_coef$alpha * log( 1 + sad_coef$N / sad_coef$alpha ), - mzsm = sum(sad_coef$theta / (1:sad_coef$J) * - (1 - (1:sad_coef$J)/sad_coef$J)^(sad_coef$theta - 1)) + S <- switch( + sad_type, + bs = sad_coef$S, + ls = sad_coef$alpha * log( 1 + sad_coef$N / sad_coef$alpha ), + mzsm = sum(sad_coef$theta / (1:sad_coef$J) * + (1 - (1:sad_coef$J)/sad_coef$J)^(sad_coef$theta - 1)) ) S <- round(S) if (!is.null(s_pool)) warning(paste("For the selected SAD model the value of s_pool is ignored. @@ -170,7 +172,7 @@ sim_sad <- function(s_pool = NULL, n_sim = NULL, if (round(s_pool, 0L) != s_pool || s_pool <= 0) stop("s_pool has to be a positive integer number") if (is.null(n_sim) || is.na(n_sim)) stop("The argument n_sim is mandatory for the selected sad and has to be a positive integer number.") if (round(n_sim, 0L) != n_sim || n_sim <= 0) stop("n_sim has to be a positive integer number") - if (class(sad_coef) != "list" || is.null(names(sad_coef))) stop("coef must be a named list!") + if (!is(sad_coef, "list") || is.null(names(sad_coef))) stop("coef must be a named list!") } @@ -179,7 +181,7 @@ sim_sad <- function(s_pool = NULL, n_sim = NULL, if (isTRUE(s_pool == 1) && !is.null(n_sim)) { abund_local <- n_sim - class(abund_local) <- c("sad","integer") + class(abund_local) <- c("sad", "integer") return(abund_local) # return early } @@ -206,15 +208,20 @@ sim_sad <- function(s_pool = NULL, n_sim = NULL, # Generates the "community" ---- if (sad_type %in% c("gamma","geom","lnorm","nbinom","weibull")) { - sadr <- utils::getFromNamespace(paste("r", sad_type, sep = ""), ns = "stats") + sadr <- utils::getFromNamespace(paste0("r", sad_type), ns = "stats") } else { - sadr <- utils::getFromNamespace(paste("r", sad_type, sep = ""), ns = "sads") + sadr <- utils::getFromNamespace(paste0("r", sad_type), ns = "sads") } abund_pool <- do.call(sadr, c(list(n = s_pool), sad_coef)) rel_abund_pool <- abund_pool/sum(abund_pool) rel_abund_pool <- sort(rel_abund_pool, decreasing = TRUE) - names(rel_abund_pool) <- paste("species", formatC(x = seq_along(rel_abund_pool), width = nchar(length(rel_abund_pool)), format = "d", flag = "0"), sep = "_") # underslash addition for readability + names(rel_abund_pool) <- paste( + "species", + formatC(x = seq_along(rel_abund_pool), + width = nchar(length(rel_abund_pool)), + format = "d", flag = "0"), + sep = "_") sample_vec <- sample(x = names(rel_abund_pool), size = n_sim, replace = TRUE, @@ -310,35 +317,43 @@ plot.sad <- function(x, ..., method = c("octave","rank")) x <- x[x > 0] - if (method == "rank") - graphics::plot(sort(as.numeric(x), decreasing = TRUE), type = "b", log = "y", - xlab = "Species rank", ylab = "Species abundance", - main = "Rank-abundance curve", las = 1, ...) - - if (method == "octave") { - - # code adopted from untb:preston() - max_abund <- max(x) - n <- 1 + ceiling(log(max_abund)/log(2)) # number of abundance classes - - if (n < 2) breaks <- c(0, 1) - else breaks <- c(0, 2^(0:(n - 2)), max_abund) - - r <- graphics::hist(x, plot = FALSE, breaks = breaks, right = TRUE) - abund_dist <- r$counts - - if (n <= 2) names(abund_dist) <- c("1","2")[1:n] - else names(abund_dist) <- c("1", "2", - paste(breaks[-c(1:2, length(breaks))] + 1, - "-", breaks[-c(1:3)], sep = "")) - - graphics::barplot(height = as.numeric(abund_dist), - names.arg = names(abund_dist), - xlab = "Abundance class", ylab = "No. of species", - main = "Preston octave plot", las = 1, ...) - } + switch(method, + rank = graphics::plot(sort(as.numeric(x), decreasing = TRUE), + type = "b", log = "y", + xlab = "Species rank", + ylab = "Species abundance", + main = "Rank-abundance curve", las = 1, ...), + + octave = { + + # code adopted from untb:preston() + max_abund <- max(x) + n <- 1 + ceiling(log(max_abund)/log(2)) # number of abundance classes + + if (n < 2) { + breaks <- c(0, 1) + } else { breaks <- c(0, 2^(0:(n - 2)), max_abund) } + + r <- graphics::hist(x, plot = FALSE, breaks = breaks, + right = TRUE) + abund_dist <- r$counts + + if (n <= 2) { + names(abund_dist) <- c("1","2")[1:n] + } else { + names(abund_dist) <- c("1", "2", + paste(breaks[-c(1:2, length(breaks))] + 1, + "-", breaks[-c(1:3)], sep = "")) + } + + graphics::barplot(height = as.numeric(abund_dist), + names.arg = names(abund_dist), + xlab = "Abundance class", + ylab = "No. of species", + main = "Preston octave plot", las = 1, ...) + } + ) } - #' Create spatial community object #' #' Creates a spatial community object with defined extent and with coordinates @@ -371,11 +386,14 @@ plot.sad <- function(x, ..., method = c("octave","rank")) #' community <- function(x, y, spec_id, xrange = c(0,1), yrange = c(0,1)) { - if (length(xrange) < 2 | length(yrange) < 2) stop("Error: missing ranges for x or y!") - - if (is.vector(xrange) & is.vector(yrange)) { # converting xrange and yrange from vectors to data.frames - xrange <- data.frame(matrix(xrange, length(unique(spec_id)), 2, byrow = TRUE)) - yrange <- data.frame(matrix(yrange, length(unique(spec_id)), 2, byrow = TRUE)) + if (length(xrange) < 2 || length(yrange) < 2) { + stop("Error: missing ranges for x or y!") } + + if (is.vector(xrange) && is.vector(yrange)) { # converting xrange and yrange from vectors to data.frames + xrange <- data.frame(matrix(data = xrange, byrow = TRUE, + nrow = length(unique(spec_id)), ncol = 2)) + yrange <- data.frame(matrix(data = yrange, byrow = TRUE, + nrow = length(unique(spec_id)), ncol = 2)) } if (min(x) < min(xrange[,1])) stop("Error: Inappropriate ranges for x!") @@ -408,7 +426,7 @@ community <- function(x, y, spec_id, xrange = c(0,1), yrange = c(0,1)) #' #' @export #' -summary.community <- function(object, digits=2, ...) # digits should be passed through ... instead. +summary.community <- function(object, digits = 2, ...) # digits should be passed through ... instead. { cat("No. of individuals: ", nrow(object$census), "\n") cat("No. of species: ", length(unique(object$census$species)), "\n") @@ -459,11 +477,12 @@ plot.community <- function(x, ..., col = NULL, pch = NULL) #' plot(sad1, method = "rank") #' plot(sad1, method = "octave") #' +#' @importFrom methods is #' @export #' community_to_sad <- function(comm) { - if (class(comm) != "community") + if (!is(comm, "community")) stop("community_to_sad requires a community object as input. See ?community.") abund <- table(comm$census$species) diff --git a/README.Rmd b/README.Rmd index 9b46849..7fe2a0f 100644 --- a/README.Rmd +++ b/README.Rmd @@ -11,25 +11,25 @@ knitr::opts_chunk$set( fig.path = "README-" ) ``` - + [![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/mobsim)](https://cran.r-project.org/package=mobsim) [![](http://cranlogs.r-pkg.org/badges/grand-total/mobsim)](https://cran.rstudio.com/web/packages/mobsim/index.html) - [![Licence](https://img.shields.io/badge/licence-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html) [![Project Status: Active – The project has reached a stable, usable state and is being actively developed.](http://www.repostatus.org/badges/latest/active.svg)](https://www.repostatus.org/) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.1170472.svg)](https://doi.org/10.5281/zenodo.1170472) -[![R-CMD-check](https://github.com/MoBiodiv/mobsim/workflows/R-CMD-check/badge.svg)](https://github.com/MoBiodiv/mobsim/actions) +[![R-CMD-check](https://github.com/AlbanSagouis/mobsim/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/AlbanSagouis/mobsim/actions/workflows/R-CMD-check.yaml) + ## Overview The goal of the package `mobsim` is to facilitate understanding of scale-dependent biodiversity changes. -The package includes functions to simulate species distributions in space with controlled abundance distributions as well as controlled intraspecific aggregation. For analysis there are functions for species rarefaction and accumulation curves, species-area relationships, endemics-area relationships and th distance-decay of community similarity. +The package includes functions to simulate species distributions in space with controlled abundance distributions as well as controlled intraspecific aggregation. For analysis there are functions for species rarefaction and accumulation curves, species-area relationships, endemics-area relationships and the distance-decay of community similarity. A detailed introduction of the package is available at [bioRxiv](https://www.biorxiv.org/content/10.1101/209502v1). diff --git a/README.md b/README.md index 908a697..238b3e0 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -[![CRAN\_Status\_Badge](http://www.r-pkg.org/badges/version/mobsim)](https://cran.r-project.org/package=mobsim) +[![CRAN_Status_Badge](http://www.r-pkg.org/badges/version/mobsim)](https://cran.r-project.org/package=mobsim) [![](http://cranlogs.r-pkg.org/badges/grand-total/mobsim)](https://cran.rstudio.com/web/packages/mobsim/index.html) [![Licence](https://img.shields.io/badge/licence-GPL--3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0.en.html) @@ -23,7 +23,7 @@ The package includes functions to simulate species distributions in space with controlled abundance distributions as well as controlled intraspecific aggregation. For analysis there are functions for species rarefaction and accumulation curves, species-area relationships, -endemics-area relationships and th distance-decay of community +endemics-area relationships and the distance-decay of community similarity. A detailed introduction of the package is available at diff --git a/codemeta.json b/codemeta.json index 88fbb60..c46e7da 100644 --- a/codemeta.json +++ b/codemeta.json @@ -7,31 +7,27 @@ "codeRepository": "https://github.com/AlbanSagouis/mobsim", "issueTracker": "https://github.com/AlbanSagouis/mobsim", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "0.2.0.9011", + "version": "0.2.1", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", "url": "https://r-project.org" }, - "runtimePlatform": "R version 4.1.2 (2021-11-01)", - "provider": { - "@id": "https://cran.r-project.org", - "@type": "Organization", - "name": "Comprehensive R Archive Network (CRAN)", - "url": "https://cran.r-project.org" - }, + "runtimePlatform": "R version 4.3.1 (2023-06-16)", "author": [ { "@type": "Person", "givenName": "Felix", "familyName": "May", - "email": "felix.may@posteo.de" + "email": "felix.may@fu-berlin.de", + "@id": "https://orcid.org/0000-0002-1106-8188" }, { "@type": "Person", "givenName": "Alban", "familyName": "Sagouis", - "email": "alban.sagouis@idiv.de" + "email": "alban.sagouis@idiv.de", + "@id": "https://orcid.org/0000-0002-3827-1063" } ], "maintainer": [ @@ -39,7 +35,8 @@ "@type": "Person", "givenName": "Felix", "familyName": "May", - "email": "felix.may@posteo.de" + "email": "felix.may@fu-berlin.de", + "@id": "https://orcid.org/0000-0002-1106-8188" } ], "softwareSuggestions": [ @@ -83,6 +80,7 @@ "@type": "SoftwareApplication", "identifier": "testthat", "name": "testthat", + "version": ">= 3.0.0", "provider": { "@id": "https://cran.r-project.org", "@type": "Organization", @@ -174,9 +172,14 @@ "identifier": "stats", "name": "stats" }, + "8": { + "@type": "SoftwareApplication", + "identifier": "methods", + "name": "methods" + }, "SystemRequirements": null }, - "fileSize": "3709.306KB", + "fileSize": "1719.797KB", "citation": [ { "@type": "ScholarlyArticle", @@ -193,9 +196,8 @@ "familyName": "Gerstner" }, { - "@type": "Person", - "givenName": "Dan", - "familyName": "McGlinn" + "@type": "Organization", + "name": "DanMcGlinn" }, { "@type": "Person", @@ -209,8 +211,10 @@ } ], "name": "mobsim: An R package for the simulation and measurement of biodiversity across spatial scales", - "url": "https://doi.org/10.1111/2041-210X.12986", - "pagination": "1401-1408", + "identifier": "https://doi.org/10.1111/2041-210X.12986", + "pagination": "-7", + "@id": "https://doi.org/10.1111/2041-210X.12986", + "sameAs": "https://doi.org/10.1111/2041-210X.12986", "isPartOf": { "@type": "PublicationIssue", "issueNumber": "6", @@ -222,5 +226,9 @@ } } } - ] + ], + "releaseNotes": "https://github.com/AlbanSagouis/mobsim/blob/master/NEWS.md", + "readme": "https://github.com/AlbanSagouis/mobsim/blob/master/README.md", + "contIntegration": "https://github.com/MoBiodiv/mobsim/actions", + "developmentStatus": "https://www.repostatus.org/" } diff --git a/cran-comments.md b/cran-comments.md index 7f691d6..f35f7b4 100644 --- a/cran-comments.md +++ b/cran-comments.md @@ -1,35 +1,27 @@ ## Resubmission -This is a resubmission. In this version I have: +This is a resubmission. In this version we have: -* Formatted the reference according to the suggestions of Uwe Ligges and changed from -a reference using the bioRXiv labe to the DOI - -* Included a reference to the paper describing the package in the DESCRIPTION file, see -[](https://www.biorxiv.org/content/early/2017/10/26/209502), as suggested by the CRAN team -member Swetlana Herbrandt - -* Fixed small typos in the vignettes and documentation +* Fixed the problems that caused mobsim to be archived on CRAN: essentially by +removing the dependency on spatstat.core and using spatstat.random and +spatstat.geom instead. +* Fixed inst/CITATION and DESCRIPTION +* Added features to major functions ## Test environments -* local ubuntu 16.04 LTS, R 3.4.2 -* win-builder (devel and release) +* local - Darwin, R 4.3.1 +* win-builder (release and devel) +* r hub - Ubuntu Linux 20.04.1 LTS, R-release, GCC +* r hub - Fedora Linux, R-devel, clang, gfortran ## R CMD check results - -There were no ERRORs or WARNINGs. - -There was 1 NOTE: - -* checking CRAN incoming feasibility ... NOTE -Maintainer: ‘Felix May ’ - -It seems this is normal for the first submission of a package +0 errors | 1 warnings | 0 notes ## Downstream dependencies -I ran devtools::revdep_check() locally +We ran revdepcheck::revdep_check() locally and no packages depend on mobsim +any more. -0 packages with problems +Best wishes, -No ERRORs or WARNINGs found :) +Felix may and Alban Sagouis diff --git a/inst/CITATION b/inst/CITATION index e4c9c35..6888caa 100644 --- a/inst/CITATION +++ b/inst/CITATION @@ -1,23 +1,11 @@ -citHeader("To cite mobsim in publications use:") - -citEntry(entry = "Article", - title = "mobsim: An R package for the simulation and measurement of biodiversity across spatial scales", - author = personList(as.person("Felix May"), - as.person("Katharina Gerstner"), - as.person("Dan McGlinn"), - as.person("Xiao Xiao"), - as.person("Jonathan M. Chase")), - journal = "Methods in Ecology and Evolution", - year = "2018", - volume = "9", - number = "6", - pages = "1401-1408", - url = "https://doi.org/10.1111/2041-210X.12986", - - textVersion = - paste("May, F, Gerstner, K, McGlinn, DJ, Xiao, X,", - "Chase, JM. mobsim: An r package for the simulation", - "and measurement of biodiversity across spatial scales.", - "Methods in Ecology and Evolution. 2018; 9:1401–1408.", - "https://doi.org/10.1111/2041-210X.12986") +bibentry( + "Article", + title = "mobsim: An R package for the simulation and measurement of biodiversity across spatial scales", + author = "Felix May, Katharina Gerstner, DanMcGlinn, Xiao Xiao, Jonathan M. Chase", + year = 2018, + journal = "Methods in Ecology and Evolution", + volume = 9, + number = 6, + pages = 1401-1408, + doi = "https://doi.org/10.1111/2041-210X.12986", ) diff --git a/man/mobsim.Rd b/man/mobsim.Rd index 4942d66..6838984 100644 --- a/man/mobsim.Rd +++ b/man/mobsim.Rd @@ -70,7 +70,5 @@ is described by its xy-coordinates and a species identity label. } \author{ -Felix May - Felix May } diff --git a/man/sample_quadrats.Rd b/man/sample_quadrats.Rd index a4645cc..76b9a5b 100644 --- a/man/sample_quadrats.Rd +++ b/man/sample_quadrats.Rd @@ -63,5 +63,4 @@ quadrat_area = 0.002, method = "grid") specnumber(comm_mat1$spec_dat) diversity(comm_mat1$spec_dat, index = "shannon") - } diff --git a/man/sampling_grids.Rd b/man/sampling_grids.Rd index 69ca8a4..f01df2d 100644 --- a/man/sampling_grids.Rd +++ b/man/sampling_grids.Rd @@ -28,9 +28,7 @@ sampling_grids( \item{ymax}{(numeric) maximum possible value on the y axis a quadrat can cover.} -\item{x0}{(numeric value) Lower left corner of the first quadrat in transect and grid sampling} - -\item{y0}{(numeric value) Lower left corner of the first quadrat in transect and grid sampling} +\item{x0, y0}{(numeric value) Lower left corner of the first quadrat in transect and grid sampling} \item{delta_x}{(numeric value) Distance between consecutive quadrats in transect and grid sampling in x-direction (the distance between the left sides is measured)} diff --git a/man/sampling_transects.Rd b/man/sampling_transects.Rd index 4c687ab..8f05d35 100644 --- a/man/sampling_transects.Rd +++ b/man/sampling_transects.Rd @@ -28,9 +28,7 @@ sampling_transects( \item{ymax}{(numeric) maximum possible value on the y axis a quadrat can cover.} -\item{x0}{(numeric value) Lower left corner of the first quadrat in transect and grid sampling} - -\item{y0}{(numeric value) Lower left corner of the first quadrat in transect and grid sampling} +\item{x0, y0}{(numeric value) Lower left corner of the first quadrat in transect and grid sampling} \item{delta_x}{(numeric value) Distance between consecutive quadrats in transect and grid sampling in x-direction (the distance between the left sides is measured)} diff --git a/man/sim_sad.Rd b/man/sim_sad.Rd index 82b2581..a91fe71 100644 --- a/man/sim_sad.Rd +++ b/man/sim_sad.Rd @@ -131,7 +131,7 @@ legend("topright", c("fix_s_sim = TRUE","fix_s_sim = FALSE"), # Different important SAD models # Fisher's log-series -sad_logseries <- sim_sad(s_pool = NULL, n_sim = 10000, sad_type = "ls", +sad_logseries <- sim_sad(s_pool = NULL, n_sim = NULL, sad_type = "ls", sad_coef = list("N" = 1e5, "alpha" = 20)) # Poisson log-normal @@ -139,7 +139,7 @@ sad_poilog <- sim_sad(s_pool = 100, n_sim = 10000, sad_type = "poilog", sad_coef = list("mu" = 5, "sig" = 0.5)) # Mac-Arthur's broken stick -sad_broken_stick <- sim_sad(s_pool = NULL, n_sim = 10000, sad_type = "bs", +sad_broken_stick <- sim_sad(s_pool = NULL, n_sim = NULL, sad_type = "bs", sad_coef = list("N" = 1e5, "S" = 100)) # Plot all SADs together as rank-abundance curves @@ -149,7 +149,6 @@ lines(1:length(sad_poilog), sad_poilog, type = "b", col = 3) lines(1:length(sad_broken_stick), sad_broken_stick, type = "b", col = 4) legend("topright", c("Log-series","Log-normal","Poisson log-normal","Broken stick"), col = 1:4, pch = 1) - } \author{ Felix May diff --git a/src/rThomas_rcpp.cpp b/src/rThomas_rcpp.cpp index c3004aa..a4b70b1 100644 --- a/src/rThomas_rcpp.cpp +++ b/src/rThomas_rcpp.cpp @@ -2,13 +2,13 @@ using namespace Rcpp; //' Thomas process individual distribution simulation for one species -//' +//' //' Usually used internally inside \code{\link{sim_thomas_coords}} //' This function randomly draws points (individuals) around one or several mother points using Rcpp. //' The function is an efficient re-implementation of the rThomas function from the spatstat package. -//' +//' //' @name rThomas_rcpp -//' +//' //' @param n_points The total number of points (individuals). //' @param n_mother_points Number of mother points (= cluster centres). //' @param xmother Vector of \code{n_mother_points} x coordinates for the mother points. @@ -38,52 +38,53 @@ DataFrame rThomas_rcpp(int n_points, double ymax = 1 ) { - + NumericVector xpoints(n_points); NumericVector ypoints(n_points); - - + + RNGScope scope; - + bool mother_points_specified = Rcpp::na_omit(xmother).size() > 0; - + if (n_mother_points > 0 || mother_points_specified) { // if clustering - if(!LogicalVector::is_na(n_mother_points) & all(is_na(xmother)) & all(is_na(ymother))) { + if (!LogicalVector::is_na(n_mother_points) && + all(is_na(xmother)) & all(is_na(ymother))) { xmother = runif(n_mother_points, xmin, xmax); ymother = runif(n_mother_points, ymin, ymax); } else { n_mother_points = xmother.size(); } - + double xnew, ynew; int imother; - + NumericVector dxy(2); - + for (int ipoint = 0; ipoint < n_points; ++ipoint){ - + do { - + imother = as(runif(1, 0, n_mother_points)); - + dxy = rnorm(2, 0.0, sigma); xnew = xmother[imother] + dxy[0]; ynew = ymother[imother] + dxy[1]; - + } while (!(xnew > xmin && xnew < xmax && ynew > ymin && ynew < ymax)); - + xpoints[ipoint] = xnew; ypoints[ipoint] = ynew; } } // end if n_mother_points > 0 || xmother.size() > 0 - + else { xpoints = runif(n_points, xmin, xmax); ypoints = runif(n_points, ymin, ymax); } - + DataFrame xydat = DataFrame::create(_["x"] = xpoints, _["y"] = ypoints); - + return(xydat); } diff --git a/tests/testthat/Rplots.pdf b/tests/testthat/Rplots.pdf index 7f9fa4a..9737fd8 100644 Binary files a/tests/testthat/Rplots.pdf and b/tests/testthat/Rplots.pdf differ diff --git a/tests/testthat/test_sampling-sample_quadrats().R b/tests/testthat/test_sampling-sample_quadrats().R index ea8c8fc..752abc8 100644 --- a/tests/testthat/test_sampling-sample_quadrats().R +++ b/tests/testthat/test_sampling-sample_quadrats().R @@ -8,7 +8,7 @@ test_that("fails correctly", { test_that("classes are as expected", { comm_mat1 <- sample_quadrats( - sim_poisson_community(s_pool = 5L, n_sim = 50L), + comm = sim_poisson_community(s_pool = 5L, n_sim = 50L), plot = FALSE, n_quadrats = 2L, method = "random", avoid_overlap = FALSE ) @@ -20,7 +20,8 @@ test_that("classes are as expected", { test_that("dimensions are as expected", { S = 5L - sim_com1 <- sim_poisson_community(s_pool = S, n_sim = 50L, fix_s_sim = TRUE) + sim_com1 <- sim_poisson_community( + s_pool = S, n_sim = 50L, fix_s_sim = TRUE) n_quadrats <- 10L comm_mat <- sample_quadrats( sim_com1, n_quadrats = n_quadrats, @@ -34,10 +35,11 @@ test_that("dimensions are as expected", { }) test_that("default argument values are as expected", { - comm <- sim_thomas_community(s_pool = 10L, n_sim = 100L, - sad_type = "lnorm", sad_coef = list(cv_abund = 1), fix_s_sim = TRUE, - sigma = 1, mother_points = 0L, xrange = c(0, 1), yrange = c(0, 1), - seed = 42L) + comm <- sim_thomas_community( + s_pool = 10L, n_sim = 100L, + sad_type = "lnorm", sad_coef = list(cv_abund = 1), fix_s_sim = TRUE, + sigma = 1, mother_points = 0L, xrange = c(0, 1), yrange = c(0, 1), + seed = 42L) expect_snapshot_output(sample_quadrats(comm = comm, seed = 42L)) }) @@ -46,13 +48,24 @@ test_that("calls the correct subfunction if method is random, with overlap", { quadrat_area <- 0.01 expect_equal( as.matrix( - sample_quadrats(sim_com1, seed = 42L, plot = FALSE, n_quadrats = 10L, quadrat_area = quadrat_area, - method = "random", avoid_overlap = FALSE)$xy_dat, - rownames.force = FALSE), + suppressWarnings( + sample_quadrats( + comm = sim_com1, seed = 42L, plot = FALSE, n_quadrats = 10L, + quadrat_area = quadrat_area, + method = "random", avoid_overlap = FALSE)$xy_dat + ), + rownames.force = FALSE + ), as.matrix( - sampling_random_overlap(xmin = sim_com1$x_min_max[1L], xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), - ymin = sim_com1$y_min_max[1L], ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), - min_dist = sqrt(2*quadrat_area), n_quadrats = 10L, seed = 42L) + suppressWarnings( + sampling_random_overlap( + xmin = sim_com1$x_min_max[1L], + xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), + ymin = sim_com1$y_min_max[1L], + ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), + min_dist = sqrt(2*quadrat_area), + n_quadrats = 10L, seed = 42L) + ) ) ) }) @@ -63,30 +76,41 @@ test_that("calls the correct subfunction if method is random, without overlap, w quadrat_area <- 0.01 expect_equal( as.matrix( - sample_quadrats(sim_com1, seed = 42L, plot = FALSE, n_quadrats = 10L, quadrat_area = quadrat_area, - method = "random", avoid_overlap = TRUE)$xy_dat, + sample_quadrats( + comm = sim_com1, seed = 42L, plot = FALSE, n_quadrats = 10L, + quadrat_area = quadrat_area, + method = "random", avoid_overlap = TRUE)$xy_dat, rownames.force = FALSE), as.matrix( - sampling_random_spatstat(xmin = sim_com1$x_min_max[1L], xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), - ymin = sim_com1$y_min_max[1L], ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), - min_dist = sqrt(2*quadrat_area), n_quadrats = 10L, seed = 42L) + sampling_random_spatstat( + xmin = sim_com1$x_min_max[1L], + xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), + ymin = sim_com1$y_min_max[1L], + ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), + min_dist = sqrt(2*quadrat_area), n_quadrats = 10L, seed = 42L) ) ) }) test_that("calls the correct subfunction if method is random, without overlap, without spatstat", { - mockery::stub(where = sample_quadrats, what = "requireNamespace", how = FALSE, depth = 1L) + mockery::stub(where = sample_quadrats, + what = "requireNamespace", how = FALSE, depth = 1L) + sim_com1 <- sim_poisson_community(s_pool = 5L, n_sim = 50L) quadrat_area <- 0.01 expect_equal( as.matrix( - sample_quadrats(sim_com1, seed = 42L, plot = FALSE, n_quadrats = 10L, quadrat_area = quadrat_area, - method = "random", avoid_overlap = TRUE)$xy_dat, + sample_quadrats( + comm = sim_com1, seed = 42L, plot = FALSE, n_quadrats = 10L, quadrat_area = quadrat_area, + method = "random", avoid_overlap = TRUE)$xy_dat, rownames.force = FALSE), as.matrix( - sampling_random_bruteforce(xmin = sim_com1$x_min_max[1L], xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), - ymin = sim_com1$y_min_max[1L], ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), - min_dist = sqrt(2*quadrat_area), n_quadrats = 10L, seed = 42L) + sampling_random_bruteforce( + xmin = sim_com1$x_min_max[1L], + xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), + ymin = sim_com1$y_min_max[1L], + ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), + min_dist = sqrt(2*quadrat_area), n_quadrats = 10L, seed = 42L) ) ) }) @@ -96,8 +120,10 @@ test_that("calls the correct subfunction if method is transects", { quadrat_area <- 0.01 expect_equal( as.matrix( - sample_quadrats(sim_com1, plot = FALSE, n_quadrats = 5L, quadrat_area = quadrat_area, - method = "transect")$xy_dat, + sample_quadrats( + comm = sim_com1, plot = FALSE, n_quadrats = 5L, + quadrat_area = quadrat_area, + method = "transect")$xy_dat, rownames.force = FALSE), as.matrix( sampling_transects(xmin = sim_com1$x_min_max[1L], xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), @@ -116,10 +142,13 @@ test_that("calls the correct subfunction if method is grid", { method = "grid")$xy_dat, rownames.force = FALSE), as.matrix( - sampling_grids(xmin = sim_com1$x_min_max[1L], xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), - ymin = sim_com1$y_min_max[1L], ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), - x0 = 0, y0 = 0, delta_x = 0.1, delta_y = 0.1, - quadrat_size = sqrt(quadrat_area), n_quadrats = 5L), + sampling_grids( + xmin = sim_com1$x_min_max[1L], + xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), + ymin = sim_com1$y_min_max[1L], + ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), + x0 = 0, y0 = 0, delta_x = 0.1, delta_y = 0.1, + quadrat_size = sqrt(quadrat_area), n_quadrats = 5L), rownames.force = FALSE) ) }) @@ -129,12 +158,18 @@ test_that("calls the correct subfunction if edge case where n_quadrats == 1", { quadrat_area <- 0.01 expect_equal( as.numeric( - sample_quadrats(sim_com1, seed = 42L, plot = FALSE, n_quadrats = 1L, quadrat_area = quadrat_area, - method = "random", avoid_overlap = TRUE)$xy_dat + sample_quadrats( + comm = sim_com1, seed = 42L, plot = FALSE, n_quadrats = 1L, + quadrat_area = quadrat_area, + method = "random", avoid_overlap = TRUE)$xy_dat ), as.numeric( - sampling_one_quadrat(xmin = sim_com1$x_min_max[1L], xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), - ymin = sim_com1$y_min_max[1L], ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), seed = 42L) + sampling_one_quadrat( + xmin = sim_com1$x_min_max[1L], + xmax = sim_com1$x_min_max[2L] - sqrt(quadrat_area), + ymin = sim_com1$y_min_max[1L], + ymax = sim_com1$y_min_max[2L] - sqrt(quadrat_area), + seed = 42L) ) ) }) diff --git a/tests/testthat/test_sampling-sampling_random_with_overlap.R b/tests/testthat/test_sampling-sampling_random_with_overlap.R index ef41a2b..5958f0d 100644 --- a/tests/testthat/test_sampling-sampling_random_with_overlap.R +++ b/tests/testthat/test_sampling-sampling_random_with_overlap.R @@ -59,15 +59,17 @@ test_that("seed parameter is respected", { sim_com1 <- sim_poisson_community(s_pool = 5L, n_sim = 50L) expect_equal( - sampling_random_overlap(n_quadrats = 5L, + suppressWarnings( + sampling_random_overlap(n_quadrats = 5L, xmin = 0, xmax = 1, ymin = 0, ymax = 1, min_dist = sqrt(2 * 0.01), - seed = 42L), - sampling_random_overlap(n_quadrats = 5L, + seed = 42L)), + suppressWarnings( + sampling_random_overlap(n_quadrats = 5L, xmin = 0, xmax = 1, ymin = 0, ymax = 1, min_dist = sqrt(2 * 0.01), - seed = 42L) + seed = 42L)) ) }) diff --git a/tests/testthat/test_simulation_functions-sim_sad().R b/tests/testthat/test_simulation_functions-sim_sad().R index d9357d7..dd038cd 100644 --- a/tests/testthat/test_simulation_functions-sim_sad().R +++ b/tests/testthat/test_simulation_functions-sim_sad().R @@ -1,21 +1,30 @@ test_that("sim_sad() - correct assertions", { expect_error(sim_sad(n_sim = 100L, sad_type = "lnorm")) - expect_error(sim_sad(s_pool = NA, n_sim = 100L, sad_type = "lnorm"), "The argument s_pool is mandatory for the selected sad and has to be a positive integer number.") - expect_error(sim_sad(s_pool = 3.5, n_sim = 100L, sad_type = "lnorm"), "s_pool has to be a positive integer number") - expect_error(sim_sad(s_pool = 0L, n_sim = 100L, sad_type = "lnorm"), "s_pool has to be a positive integer number") + expect_error(sim_sad(s_pool = NA, n_sim = 100L, sad_type = "lnorm"), + "The argument s_pool is mandatory for the selected sad and has to be a positive integer number.") + expect_error(sim_sad(s_pool = 3.5, n_sim = 100L, sad_type = "lnorm"), + "s_pool has to be a positive integer number") + expect_error(sim_sad(s_pool = 0L, n_sim = 100L, sad_type = "lnorm"), + "s_pool has to be a positive integer number") expect_error(sim_sad(s_pool = 3L, sad_type = "lnorm")) expect_error(sim_sad(s_pool = 3L, n_sim = NA, sad_type = "lnorm")) - expect_error(sim_sad(s_pool = 3L, n_sim = 100.5, sad_type = "lnorm"), "n_sim has to be a positive integer number") - expect_error(sim_sad(s_pool = 3L, n_sim = 0L, sad_type = "lnorm"), "n_sim has to be a positive integer number") - - expect_error(sim_sad(s_pool = 3L, n_sim = 100L, sad_type = "lnorm", sad_coef = 0.1), "coef must be a named list!") + expect_error(sim_sad(s_pool = 3L, n_sim = 100.5, sad_type = "lnorm"), + "n_sim has to be a positive integer number") + expect_error(sim_sad(s_pool = 3L, n_sim = 0L, sad_type = "lnorm"), + "n_sim has to be a positive integer number") + + expect_error(sim_sad(s_pool = 3L, n_sim = 100L, sad_type = "lnorm", + sad_coef = 0.1), + "coef must be a named list!") expect_error(sim_sad(s_pool = 3L, n_sim = 100L, sad_type = "wrong_sad_type")) expect_error(sim_sad(sad_type = "bs", sad_coef = list(S = 4L))) - expect_warning(sim_sad(s_pool = 3L, sad_type = "bs", sad_coef = list(S = 4L, N = 10L))) - expect_warning(sim_sad(n_sim = 10L, sad_type = "bs", sad_coef = list(S = 4L, N = 10L))) + expect_warning(sim_sad(s_pool = 3L, sad_type = "bs", + sad_coef = list(S = 4L, N = 10L))) + expect_warning(sim_sad(n_sim = 10L, sad_type = "bs", + sad_coef = list(S = 4L, N = 10L))) }) test_that("default argument values are as expected", { @@ -27,46 +36,57 @@ test_that("sim_sad() - results are as expected", { n_sim <- 10L s_pool <- 3L - sadNorm <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "lnorm", fix_s_sim = TRUE) + sadNorm <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "lnorm", + fix_s_sim = TRUE) expect_equal(sum(sadNorm), n_sim) expect_equal(length(sadNorm), s_pool) - sadBs <- sim_sad(sad_type = "bs", sad_coef = list(S = s_pool, N = n_sim), fix_s_sim = TRUE) + sadBs <- sim_sad(sad_type = "bs", sad_coef = list(S = s_pool, N = n_sim), + fix_s_sim = TRUE) expect_equal(sum(sadBs), n_sim) expect_equal(length(sadBs), s_pool) - sadGamma <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "gamma", sad_coef = list(shape = 1, scale = 1), fix_s_sim = TRUE) + sadGamma <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "gamma", + sad_coef = list(shape = 1, scale = 1), fix_s_sim = TRUE) expect_equal(sum(sadGamma), n_sim) expect_equal(length(sadGamma), s_pool) - sadGeom <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "geom", sad_coef = list(prob = 0.5), fix_s_sim = TRUE) + sadGeom <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "geom", + sad_coef = list(prob = 0.5), fix_s_sim = TRUE) expect_equal(sum(sadGeom), n_sim) expect_equal(length(sadGeom), s_pool) - sadLs <- sim_sad(sad_type = "ls", sad_coef = list(N = n_sim, alpha = 3), fix_s_sim = TRUE) + sadLs <- sim_sad(sad_type = "ls", sad_coef = list(N = n_sim, alpha = 3), + fix_s_sim = TRUE) expect_equal(sum(sadLs), n_sim) # expect_equal(sum(sim_sad(sad_type = "mzsm", sad_coef = list(n = 3L, J = n_sim, theta = 4))), n_sim) - sadNbinom <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "nbinom", sad_coef = list(size = 100L, prob = 0.5), fix_s_sim = TRUE) + sadNbinom <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "nbinom", + sad_coef = list(size = 100L, prob = 0.5), + fix_s_sim = TRUE) expect_equal(sum(sadNbinom), n_sim) expect_equal(length(sadNbinom), s_pool) - sadPareto <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "pareto", sad_coef = list(shape = 2), fix_s_sim = TRUE) + sadPareto <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "pareto", + sad_coef = list(shape = 2), fix_s_sim = TRUE) expect_equal(sum(sadPareto), n_sim) expect_equal(length(sadPareto), s_pool) - sadPoilog <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "poilog", fix_s_sim = TRUE) + sadPoilog <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "poilog", + fix_s_sim = TRUE) expect_equal(sum(sadPoilog), n_sim) expect_equal(length(sadPoilog), s_pool) - sadPower <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "power", sad_coef = list(s = 2), fix_s_sim = TRUE) + sadPower <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "power", + sad_coef = list(s = 2), fix_s_sim = TRUE) expect_equal(sum(sadPower), n_sim) expect_equal(length(sadPower), s_pool) # expect_equal(sum(sim_sad(s_pool = 3L, n_sim = n_sim, sad_type = "powbend", sad_coef = list(s = 2, omega = 3))), n_sim) - sadWeibull <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "weibull", sad_coef = list(shape = 2), fix_s_sim = TRUE) + sadWeibull <- sim_sad(s_pool = s_pool, n_sim = n_sim, sad_type = "weibull", + sad_coef = list(shape = 2), fix_s_sim = TRUE) expect_equal(sum(sadWeibull), n_sim) expect_equal(length(sadWeibull), s_pool) })