Skip to content

Commit

Permalink
Fix no site names (fixes #76)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias Grenié authored Dec 2, 2023
1 parent b98a6fe commit d11d749
Show file tree
Hide file tree
Showing 19 changed files with 172 additions and 0 deletions.
9 changes: 9 additions & 0 deletions R/fd_fdis.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
#' * `site` the names of the sites as the row names of the input `sp_com`,
#' * `FDis` the values of functional dispersion at each site.
#'
#' ```{r child = "man/rmdchunks/no_row_names.Rmd"}
#' ```
#'
#' NB: when a site contains no species FDis is equal to 0.
#'
#' @references
Expand Down Expand Up @@ -62,6 +65,12 @@ fd_fdis <- function(traits, sp_com) {

}

if (is.null(rownames(sp_com))) {

rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com)))

}

# Standardize abundance per site
site_abundances <- rowSums(sp_com, na.rm = TRUE)
site_abundances[site_abundances == 0] <- 1 # Account for site with no species
Expand Down
9 changes: 9 additions & 0 deletions R/fd_fdiv.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#' * `site` the names of the sites as the row names of the input `sp_com`,
#' * `FDiv` the values of functional divergence at each site.
#'
#' ```{r child = "man/rmdchunks/no_row_names.Rmd"}
#' ```
#'
#' NB: when a site contains no species FDiv is equal to 0. If for a site
#' there are less traits than species, then FDiv is equal to `NaN`.
#'
Expand Down Expand Up @@ -61,6 +64,12 @@ fd_fdiv <- function(traits, sp_com) {

}

if (is.null(rownames(sp_com))) {

rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com)))

}

# Standardize abundance per site
site_abundances <- rowSums(sp_com, na.rm = TRUE)
site_abundances[site_abundances == 0] <- 1 # Account for site with no species
Expand Down
9 changes: 9 additions & 0 deletions R/fd_feve.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#' row names,
#' * `FEve` numeric column that contains FEve values corresponding to each site.
#'
#' ```{r child = "man/rmdchunks/no_row_names.Rmd"}
#' ```
#'
#' NB: By definition FEve is equal to `NA` when the number of species per site
#' is strictly lower than 3.
#'
Expand Down Expand Up @@ -68,6 +71,12 @@ fd_feve <- function(traits = NULL, sp_com, dist_matrix = NULL) {
)
}

if (is.null(rownames(sp_com))) {

rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com)))

}

# Standardize abundance per site
site_abundances <- rowSums(sp_com, na.rm = TRUE)
site_abundances[site_abundances == 0] <- 1 # Account for site with no species
Expand Down
9 changes: 9 additions & 0 deletions R/fd_fric.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
#' * `site` the names of the sites as the row names of the input `sp_com`,
#' * `FRic` the values of functional richness at each site.
#'
#' ```{r child = "man/rmdchunks/no_row_names.Rmd"}
#' ```
#'
#' NB: FRic is equal to `NA` when there are strictly less species in a site
#' than the number of provided traits. Note that only species with strictly
#' different trait combinations are considered unique, species that share the
Expand Down Expand Up @@ -94,6 +97,12 @@ fd_fric <- function(traits, sp_com, stand = FALSE) {

}

if (is.null(rownames(sp_com))) {

rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com)))

}

max_range <- 1

if (stand) {
Expand Down
9 changes: 9 additions & 0 deletions R/fd_fric_intersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#' * `FRic_intersect` the volume of the convex hulls intersection of each
#' pair of site.
#'
#' ```{r child = "man/rmdchunks/no_row_names.Rmd"}
#' ```
#'
#' NB: FRic_intersect is equal to `NA` when there are strictly less species in
#' one of the sites than the number of provided traits. Note that only species
#' with strictly different trait combinations are considered unique, species
Expand Down Expand Up @@ -73,6 +76,12 @@ fd_fric_intersect <- function(traits, sp_com, stand = FALSE) {

}

if (is.null(rownames(sp_com))) {

rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com)))

}

max_range <- 1

if (stand) {
Expand Down
9 changes: 9 additions & 0 deletions R/fd_raoq.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
#' * `site` the names of the sites as the row names of the input `sp_com`,
#' * `Q` the values of Rao's quadratic entropy at each site.
#'
#' ```{r child = "man/rmdchunks/no_row_names.Rmd"}
#' ```
#'
#' NB: Rao's quadratic entropy is 0 when there are no species in the site.
#'
#' @references
Expand Down Expand Up @@ -76,6 +79,12 @@ fd_raoq <- function(traits = NULL, sp_com, dist_matrix = NULL) {

}

if (is.null(rownames(sp_com))) {

rownames(sp_com) <- paste0("s", seq_len(nrow(sp_com)))

}

# Standardize abundance per site
site_abundances <- rowSums(sp_com, na.rm = TRUE)
site_abundances[site_abundances == 0] <- 1 # Account for site with no species
Expand Down
3 changes: 3 additions & 0 deletions man/fd_fdis.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fd_fdiv.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fd_feve.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fd_fric.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fd_fric_intersect.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/fd_raoq.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions man/rmdchunks/no_row_names.Rmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
If the `sp_com` argument is not provided or if `sp_com` doesn't have rownames,
arbitrary rownames `s1`, `s2`, `s3`, etc. will be used.
16 changes: 16 additions & 0 deletions tests/testthat/test-fdis.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ test_that("Functional Dispersion works on data.frame as well as matrix", {
)
})

test_that("Functional Dispersion works when sites have no names", {

site_sp_no_names <- site_sp_birds[1,, drop = FALSE]
rownames(site_sp_no_names) <- NULL

fdis <- expect_silent(fd_fdis(traits_birds, site_sp_no_names))

expect_s3_class(fdis, "data.frame")
expect_identical(dim(fdis), c(1L, 2L))
expect_named(fdis, c("site", "FDis"))

expect_equal(fdis$FDis, 151.3885, tolerance = 1e-7)
expect_equal(fdis[1, "site"], "s1")

})

# Tests for invalid inputs -----------------------------------------------------

test_that("Functional Dispersion fails gracefully", {
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-fdiv.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ test_that("Functional Divergence works on data.frame as well as matrix", {
)
})

test_that("Functional Divergence works when sites have no names", {

site_sp_no_names <- site_sp_birds[1,, drop = FALSE]
rownames(site_sp_no_names) <- NULL

fdiv <- expect_silent(fd_fdiv(traits_birds, site_sp_no_names))

expect_s3_class(fdiv, "data.frame")
expect_identical(dim(fdiv), c(1L, 2L))
expect_named(fdiv, c("site", "FDiv"))

expect_equal(fdiv$FDiv, 0.6847251, tolerance = 1e-7)
expect_equal(fdiv[1, "site"], "s1")

})

# Tests for invalid inputs -----------------------------------------------------

test_that("Functional Divergence fails gracefully", {
Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-feve.R
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ test_that("Functional Evenness works on data.frame as well as matrix", {
)
})

test_that("Functional Evenness works when sites have no names", {

site_sp_no_names <- site_sp_birds[1,, drop = FALSE]
rownames(site_sp_no_names) <- NULL

feve <- expect_silent(fd_feve(traits_birds, site_sp_no_names))

expect_s3_class(feve, "data.frame")
expect_identical(dim(feve), c(1L, 2L))
expect_named(feve, c("site", "FEve"))

expect_equal(feve$FEve, 0.3841202, tolerance = 1e-7)
expect_equal(feve[1, "site"], "s1")

})


# Tests for invalid inputs -----------------------------------------------------

test_that("Functional Evenness fails gracefully", {
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-fric.R
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,22 @@ test_that("Functional Richness works on data.frame as well as matrix", {
)
})

test_that("Functional Richness works when sites have no names", {

site_sp_no_names <- site_sp_birds[1,, drop = FALSE]
rownames(site_sp_no_names) <- NULL

fric <- expect_silent(fd_fric(traits_birds, site_sp_no_names))

expect_s3_class(fric, "data.frame")
expect_identical(dim(fric), c(1L, 2L))
expect_named(fric, c("site", "FRic"))

expect_equal(fric$FRic, 171543.730, tolerance = 1e-7)
expect_equal(fric[1, "site"], "s1")

})


# Tests for invalid inputs -----------------------------------------------------

Expand Down
17 changes: 17 additions & 0 deletions tests/testthat/test-fric_intersect.R
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@ test_that("Functional Richness Inters. works on data.frame as well as matrix", {
)
})

test_that("Functional Richness Intersection works when sites have no names", {

site_sp_no_names <- site_sp_birds[1,, drop = FALSE]
rownames(site_sp_no_names) <- NULL

fric_int <- expect_silent(fd_fric_intersect(traits_birds, site_sp_no_names))

expect_s3_class(fric_int, "data.frame")
expect_identical(dim(fric_int), c(1L, 3L))
expect_named(fric_int, c("first_site", "second_site", "FRic_intersect"))

expect_equal(fric_int$FRic_intersect, 171543.73, tolerance = 1e-7)
expect_equal(fric_int[1, "first_site"], "s1")
expect_equal(fric_int[1, "second_site"], "s1")

})

# Tests for invalid inputs -----------------------------------------------------

test_that("Functional Richness Intersection fails gracefully", {
Expand Down
16 changes: 16 additions & 0 deletions tests/testthat/test-raoq.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ test_that("Rao's Quadratic Entropy works on data.frame as well as matrix", {
)
})

test_that("Rao's Quadratric Entropy works when sites have no names", {

site_sp_no_names <- site_sp_birds[1,, drop = FALSE]
rownames(site_sp_no_names) <- NULL

raoq <- expect_silent(fd_raoq(traits_birds, site_sp_no_names))

expect_s3_class(raoq, "data.frame")
expect_identical(dim(raoq), c(1L, 2L))
expect_named(raoq, c("site", "Q"))

expect_equal(raoq$Q, 194.78095, tolerance = 1e-7)
expect_equal(raoq[1, "site"], "s1")

})


# Tests for invalid inputs -----------------------------------------------------

Expand Down

0 comments on commit d11d749

Please sign in to comment.