AnneChao / SpadeR

SpadeR provides simple R functions to compute various biodiversity indices and related (dis)similarity measures based on individual-based (abundance) data or sampling-unit-based (incidence) data taken from one or multiple communities/assemblages.
27 stars 10 forks source link

ChaoSpecies error (Error in if (var_iChao2 > 0) { : missing value where TRUE/FALSE needed #3

Open PeterSoroye opened 8 years ago

PeterSoroye commented 8 years ago

Hello Anne,

I'm attempting to run the ChaoSpecies function (method=incidence, k=10, conf=0.95) for about 360 quadrats (each in a different column) for which I have a variable number of sampling units and a possible 271 species (two example quadrats are included in the attached text file). I have a script which goes through each quadrat (column), determines whether the proportion of singleton species is greater than 0.5 (in which case it would be inappropriate to resample), and then performs the ChaoSpecies function on the quadrat and extracts certain species richness estimates to a final table.

quads <- nrow(ebquads) #determine number of quadrats
ebuttresamp <- data.frame(1:quads, NA, NA, NA, NA) #create final table where estimates will go
colnames(ebuttresamp) <- c("obssprich", "ichao", "ichaoSE", "jk2", "jk2SE")

for (i in 2:quads) #loop through all quadrats 
{
  singletons <- sum(ebuttsamp3[,i] == 1) ##count number singletons
  totspobs <- sum(ebuttsamp3[,i] > 0) ##count totnumber sp (obs richness)
  singratio <- singletons/totspobs
  ebuttresamp[i,1] <- totspobs #add observed species richness to all quadrats
  if (singratio >= 0.5) {
    ebuttresamp[i,2:5] <- "NA"   #if ratio singletons to total is greater than 0.5, do not resample
  } else {
    resampstats <- ChaoSpecies(ebuttsamp3[,i], datatype= "incidence", k=10, conf=0.95)  #otherwise resample 
    ebuttresamp[i,2] <- resampstats$Species.Table$Estimate[4]  #extracting iChao2 and add to final table
    ebuttresamp[i,3] <- resampstats$Species.Table$s.e.[4]      #extracting iChao2 SE and add to final table
    ebuttresamp[i,4] <- resampstats$Species.Table$Estimate[8]  #extracting jacknife2 and add to final table
    ebuttresamp[i,5] <- resampstats$Species.Table$s.e.[8]      #extracting jacknife2 SE and add to final table
    } #end if/else
}  #end for

The script works fine for most quadrats (ex. it works fine for the first example quadrat, Q22), however when it gets to certain quadrats (ex. the second example quadrat, Q23), it stops the loop and returns the following error:

Error in if (var_iChao2 > 0) { : missing value where TRUE/FALSE needed

And the traceback function traces the error to

3: SpecInciiChao2(data, k, conf) 2: SpecInciOut(data, method, k, conf) 1: ChaoSpecies(ebuttsamp3[, i], datatype = "incidence", k = 10, conf = 0.95) at #12

Like I mentioned, it's only an issue for certain, specific quadrats, and the problematic quadrats don't seem to be obviously different from others in terms of observed species richness, sampling units or proportion of singletons. That said my knowledge on exactly how the ChaoSpecies function works, and therefore it's limitations/requirements is not comprehensive, so I though I'd ask whether you know any reason that the ChaoSpecies function might be returning this error for some quadrats?

Thanks in advance, I'd really appreciate any help or insight into this problem that you could offer.

Sincerely,

-Peter quad22and23.txt

DanielaVargasR commented 7 years ago

I have the same error. Is there any solution?

Thank you very much!

Daniela

dsiddons commented 7 years ago

Hello Daniela,

Not sure if this will help, but I found that there was a quirk with the SortDataFun and proposed a solution in another feed:

"Further problems with ChaoShared"

The problem seems to stem from "SortDataFun"

specifically this line of code

sit1 <- sample(sit1[1:(D1-D12)], replace=F)

When the result of D1-D12 is 1, it gives three numbers (a known problem/result of doing this with a single value) rather than the result of "3".

try it:

x1 = c(1,0,1,0), x2 = c(1,1,0,1)
sit1 <- order(x1 > 0 & x2 == 0, decreasing=T)
set.seed(123)
sit1 <- sample(sit1[1:(D1-D12)], replace=F)  # random sample
special1 <- cbind(x1[sit1[1:(D1-D12)]], x2[sit1[1:(D1-D12)]])

the result for special one is "1,1" and not "1,0" as it should be.

a quick fix would be an if/else on the value of D1-D12 (and I suppose for D2-D12 for sit2). e.g.

if (D1-D12 == 1){
    special1 <- cbind(x1[sit1[1]], x2[sit1[1]])  
    } else {
    set.seed(123)
    sit1 <- sample(sit1[1:(D1-D12)], replace=F)  # random sample
    special1 <- cbind(x1[sit1[1:(D1-D12)]], x2[sit1[1:(D1-D12)]])
    }
ambreen578 commented 6 years ago

i have this error can anybody help me. if(LCL[i] == 0){Pin1 <- sum(dbinom(LCL[i]:UCL, n, P1, FALSE))} Error in if (LCL[i] == 0) { : missing value where TRUE/FALSE needed

pendragon1998 commented 6 months ago
> ParkDatasetPivot2
     SL001 SL002 SL003 SL005 SL006 SL007 SL008 SL009 SL010 SL012 SL013
AMCR     1     0     0     0     0     0     0     0     0     0     0
AMRE     0     1     0     0     0     0     0     0     0     0     0
AMRO     0     1     0     0     0     0     0     0     0     0     0
BAWW     0     0     0     0     1     0     0     1     0     1     0
BGGN     1     1     1     1     1     1     1     1     1     1     1
BHCO     0     0     1     1     1     1     1     1     1     1     0
BHVI     0     0     0     0     0     0     1     0     0     0     0
BLGR     0     1     0     0     0     0     0     0     0     0     0
BLJA     1     1     1     1     1     1     1     1     1     1     1
BRTH     0     0     0     0     1     0     0     1     0     1     0
BTGR     0     0     0     0     0     0     0     0     0     0     1
CACH     1     1     1     1     1     1     1     1     1     1     1
CANG     0     1     0     0     0     0     0     0     0     0     0
CARW     1     1     1     1     1     1     1     1     1     1     1
CEDW     0     1     0     0     0     0     0     0     0     0     0
CHSP     0     0     0     0     0     0     0     0     0     1     0
CHSW     1     0     0     0     0     0     0     0     0     0     0
CLRA     0     1     0     0     0     0     0     0     0     0     0
COYE     0     0     0     0     0     1     0     0     0     0     0
DOWO     1     1     1     0     0     1     1     1     0     1     1
EABL     1     0     0     0     0     0     0     0     0     1     1
EATO     0     0     0     0     0     0     0     0     0     0     1
FICR     1     1     0     0     0     1     1     0     1     1     1
GBHE     0     1     0     0     0     0     0     0     1     0     0
GCFL     1     1     1     1     1     1     1     1     1     1     1
GRCA     0     0     0     0     0     1     0     0     0     1     0
GRHE     1     0     0     1     0     0     0     0     0     0     0
HETH     0     0     0     0     0     0     0     0     0     0     1
HOWA     0     0     0     1     0     0     0     0     0     0     0
LAGU     1     0     0     1     1     1     1     0     1     1     0
MODO     1     0     1     0     1     1     0     0     1     1     1
NOCA     1     1     1     1     1     1     1     1     1     1     1
NOMO     0     0     0     0     1     0     1     0     0     0     0
NOPA     1     1     1     1     1     1     1     1     1     0     1
OCWA     0     0     0     0     0     0     1     1     0     0     0
OSPR     0     0     0     0     0     0     0     1     0     0     0
PABU     0     0     0     0     0     0     0     0     0     1     1
PIWA     1     0     0     0     0     1     1     1     1     1     1
PIWO     1     1     1     1     1     1     1     1     1     0     1
RBWO     1     1     1     1     1     1     1     1     1     1     1
RCKI     0     0     0     1     1     1     1     0     0     1     1
REVI     1     0     0     0     0     0     1     0     1     0     0
RHWO     0     0     1     0     0     0     0     0     0     1     0
ROYT     0     0     0     0     0     0     0     0     0     0     1
RWBL     0     0     0     0     0     0     0     0     0     0     1
SUTA     0     0     0     0     1     1     0     1     0     0     0
TUTI     1     0     1     1     1     1     1     1     1     1     1
WEVI     1     0     1     1     1     1     1     1     1     1     1
YRWA     0     0     0     0     1     0     0     0     0     0     0
YTVI     1     0     0     1     0     0     0     1     0     0     0
YTWA     1     1     1     1     1     1     1     1     1     1     1
>   #Calculate iChao2 Species Richness Estimators:
>     ChaoSpecies(ParkDatasetPivot2, datatype=c("incidence_raw"), k=100, conf=0.95)
Error in if (var_iChao2 > 0) { : missing value where TRUE/FALSE needed

Has there ever been a fix made for this error? I can't figure out what is causing it. My datasets for other sampling areas work fine with the same code, but this particular dataset throws this error code.