MoBiodiv / mobr

Tools for analyzing changes in diversity across scales
Other
23 stars 18 forks source link

Error in `get_delta_stats` for calculating density effects when dens_ratio < 1 #264

Closed jimjunker1 closed 3 months ago

jimjunker1 commented 2 years ago

I have run into an issue when calculating the density effects from get_delta_stats() in mobr_2.0.2. From what I can tell, the rarefaction function shortens the effort vector for all effort/dens_ratio > n when calculating S_N rarefaction. It is my understanding from rarefaction description that it should pass the observed number of species when effort (in this case, effort/dens_ratio) > n and extrapolate == FALSE. From rarefaction() documentation:

"If effort is greater than sample size and extrapolate = FALSE then the observed number of species is returned."

However, it drops these effort values (from rarefaction() effort = effort[effort/dens_ratio <= n) and I can't find when/if the observed S is replaced. This leads to an altered vector length and error when attempting to create a data frame:

""Error in data.frame(test = "N", sample = "indiv", effort = inds, S = S_N, : arguments imply differing number of rows:"

Very possible I am misunderstanding the issue or missed where it is discussed and am misapplying the method. Any help or clarification would be appreciated.

Cheers.

Code to reproduce the issue:

First the community and plot attribute data:

commDF = structure(list(a = c(804.8, 1641.6, 667.2, 0, 750.4, 1049.6, 
2604.8, 963.2, 990.4, 2145.6, 3174.4, 1691.2, 0, 0, 576, 0, 0, 
0, 0, 1344, 0, 211.2, 643.2, 432, 340.8, 160, 576), b = c(3.2, 
0, 8, 3.2, 29.68, 0, 0, 0, 0, 0, 0, 0, 0, 27.2, 0, 769.6, 0, 
188.8, 364.8, 0, 1014.4, 116.8, 1888, 73.6, 411.2, 1286.4, 268.8
), c = c(0, 0, 764.8, 228.8, 132.8, 0, 0, 156.8, 0, 0, 0, 0, 
0, 600, 0, 235.2, 1881.6, 16, 206.4, 68.8, 596.8, 449.6, 716.8, 
483.2, 244.8, 0, 569.6), d = c(0, 0, 30.4, 1665.6, 0, 0, 0, 0, 
0, 0, 0, 0, 1203.2, 537.6, 0, 0, 2390.4, 27.2, 1161.6, 0, 0, 
0, 0, 0, 744, 548.8, 1.6), e = c(0, 0, 0, 35.2, 48.208, 0, 0, 
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0), 
    f = c(0, 36.8, 0, 6.4, 0, 0, 0, 0, 0, 0, 0, 0, 11.84, 0, 
    0, 0, 5.12, 0, 0, 0, 0, 3.2, 8, 0, 0, 0, 0), g = c(4.8, 0, 
    0, 48, 3.808, 6.4, 41.6, 33.6, 35.2, 0, 0, 0, 739.2, 62.4, 
    0, 67.2, 24, 0, 0, 379.2, 64, 40, 0, 166.4, 0, 0, 0), h = c(0, 
    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 463.2, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 0), i = c(1.6, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 0, 0, 0, 0, 0, 3.2, 14.4, 14.4, 0, 0, 0, 0, 0, 19.2, 
    0, 0, 0), j = c(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 
    0, 0, 25.6, 0, 16, 0, 0, 19.2, 0, 0, 0, 0, 1.216)), row.names = c(NA, 
-27L), class = c("tbl_df", "tbl", "data.frame"))

plotDF = structure(list(siteID = c("B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "C", "C", "C", "C", "C", 
"C", "C", "C", "C", "C", "C", "C"), long_dd = c(-89.8582133333333, 
-89.8583283333333, -89.8584166666667, -89.8585, -89.85865, -89.8586633333333, 
-89.858695, -89.8588383333333, -89.8590266666667, -89.3593716666667, 
-89.8595111166667, -89.8596516666667, -89.85978, -89.8600066666667, 
-89.8604266666667, -89.8601166666667, -89.8600277777778, -89.8598888888889, 
-89.8596611111111, -89.8584916666667, -89.8584916666667, -89.8582722222222, 
-89.8580166666667, -89.8584361111111, -89.8583666666667, -89.8582333333333, 
-89.8579805555556), lat_dd = c(29.5529766666667, 29.5528633333333, 
29.5527166666667, 29.5525166666667, 29.5520833333333, 29.55303, 
29.5530033333333, 29.5529233333333, 29.552765, 29.5524316666667, 
29.5539305, 29.5538483333333, 29.5537616666667, 29.5537183333333, 
29.5535316666667, 29.5583816666667, 29.5583472222222, 29.5582833333333, 
29.5581722222222, 29.5565, 29.5565194444444, 29.5565805555556, 
29.5566444444444, 29.5562055555556, 29.5562555555556, 29.5563166666667, 
29.5563583333333)), row.names = c(NA, -27L), class = c("tbl_df", 
"tbl", "data.frame"))

Converting with make_mob_in and attempting to get delta statistics:

mob_in <- mobr::make_mob_in(commDF, plotDF,
                      coord_names = c("long_dd", "lat_dd"), latlong = TRUE
)

delta_stats <- mobr::get_delta_stats(mob_in, 
                               env_var = "siteID",
                               ref_level = NULL,
                               log_scale = TRUE, 
                               n_perm = 1000,
                               overall_p = TRUE, 
                               type = "discrete"
)

This throws the error: "Error in data.frame(test = "N", sample = "indiv", effort = inds, S = S_N, : arguments imply differing number of rows: 1, 16, 15"

Thanks again.

sessionInfo() R version 4.0.3 (2020-10-10) Platform: x86_64-w64-mingw32/x64 (64-bit) Running under: Windows 10 x64 (build 19042)

attached base packages: [1] parallel grid stats graphics grDevices utils datasets methods base

other attached packages: [1] gplots_3.1.1 usedist_0.4.0 pairwiseAdonis_0.4 cluster_2.1.0 vegan_2.5-7
[6] lattice_0.20-41 permute_0.9-5 mobr_2.0.2 taxize_0.9.99 rfishbase_3.1.8
[11] gtable_0.3.0 forcats_0.5.1 stringr_1.4.0 dplyr_1.0.7 purrr_0.3.4
[16] readr_2.0.2 tidyr_1.1.3 tibble_3.1.3 ggplot2_3.3.5 tidyverse_1.3.1
[21] readxl_1.3.1 rmarkdown_2.9 styler_1.5.1 here_1.0.1 pacman_0.5.1
[26] devtools_2.4.2 usethis_2.0.1