NCEAS / codyn

Community dynamics metrics
33 stars 8 forks source link

synchrony.R line 166 ['Gross'] creating error #127

Open gsrednick opened 2 years ago

gsrednick commented 2 years ago

I am working with a large community time series (n = 91 species, 50 locations, 12 years). I have subsetted species into separate groups varying in the number of species for performing synchrony calculations. When running synchrony function with 'Loreau' metric there are no issues other than 'NA's produced for locations with species that do not vary over time. However, when I run the same function with the 'Gross' metric the function produces an error:

Error in names(subout) <- c(species.var, "sppcor") : 'names' attribute [2] must be the same length as the vector [1]

It appears that this error is produced at the inclusion of 15 species but not 16 species. I have isolated the error to line 166 in synchrony.R. My guess is that there are no correlations between some species due to high prevalence of zeros or unchanging values, but I am not sure.

Any thoughts on why this error is being produced?

A note: the dataset is fully balanced with no missing years and no NAs.

mbjones commented 2 years ago

@laurenmh any thoughts on this one?

mbjones commented 2 years ago

Also, @gsrednick its really hard for us volunteers to track down these issues without a reproducible example of the problem. Please help us help you with a reprex: https://reprex.tidyverse.org/articles/reprex-dos-and-donts.html Thanks!

gsrednick commented 2 years ago

Absolutely. Apologies for not including a reproducible example. Here is some code that shows proper function as well as the error. Additional apologies for the volume of data included.

I find that for site "A", where some species show no temporal variation and are removed, the synchrony function produces a value. However for site "C" where all species lack temporal variation (i.e., they are all zero) the synchrony function produces the error noted above.

My thought is that instead of not producing any "value" when all species abundance does not vary it might be more useful if the function produces an "NA" or "NaN" (as done for the Loreau metric) for a given site.

Thanks again!!!

require(codyn)
require(tidyverse)

test_set <- structure(list(species = c("CPUN", "CPUN", "CPUN", "CPUN", "CPUN", 
                                       "CPUN", "HROS", "HROS", "HROS", "HROS", "HROS", "HROS", "OYT", 
                                       "OYT", "OYT", "OYT", "OYT", "OYT", "PCLA", "PCLA", "PCLA", "PCLA", 
                                       "PCLA", "PCLA", "SMYS", "SMYS", "SMYS", "SMYS", "SMYS", "SMYS"), 
                           year = c(2007L, 2008L, 2009L, 2008L, 2007L, 2009L, 2007L, 
                                    2008L, 2009L, 2007L, 2009L, 2008L, 2007L, 2008L, 2009L, 2009L, 
                                    2008L, 2007L, 2007L, 2008L, 2009L, 2009L, 2008L, 2007L, 2007L, 
                                    2008L, 2009L, 2007L, 2008L, 2009L), 
                          cover = c(0, 0, 0, 6.375, 7.111111111, 11.2, 0, 0, 0, 1, 1, 1.083333333, 0, 0, 0, 1, 
                                    1.277777778, 8.481481481, 0, 0, 0, 1, 1.083333333, 1.095238095, 0, 0, 0, 0, 0, 0), 
                          site = c("C", "C", "C", "A", "A", "A", "C", "C", "C",
                                   "A", "A", "A", "C", "C", "C", "A", "A", "A", "C", "C", "C", "A", 
                                   "A", "A", "A", "A", "A", "C", "C", "C")), 
                      class = "data.frame", row.names = c(NA, -30L))

# all species abundance is zero; this produces an error
test_set_error<-test_set %>% filter(site == "C")

synchrony(test_set_error,
          time.var = "year",species.var = "species",
          abundance.var = "cover",
          replicate.var = "site",
          metric = "Gross")

# function removes species with zero abundace; warning but no error 
test_set_no_error<-test_set %>% filter(site == "A")

synchrony(test_set_no_error,
          time.var = "year",
          species.var = "species",
          abundance.var = "cover",
          replicate.var = "site",
          metric = "Gross")
mbjones commented 2 years ago

Seems related to #126. @laurenmh do you want to chime in on #127 or #126 to determine how to deal with the issue when abundance does not vary for a species in the synchrony calculations? We have another documentation bugfix to go out for #128, so I am trying to determine if these should be included in that release.