[R-sig-eco] SIMPER problem: invalid 'nrow' value (too large or NA)

mastratton mastratton at vims.edu
Tue Oct 14 20:41:19 CEST 2014


markusvlindh wrote
> Dear all,
> 
> I'm having difficulty applying a SIMPER analysis found in vegan, following
> the example provided i the help function of simper. I keep receiving the
> following error message: 
> 
> Error in matrix(ncol = P, nrow = n.a * n.b) : 
>   invalid 'nrow' value (too large or NA)
> 
> My data consist of a community matrix with 200 species and 43 dates (class
> = "data.frame") and my groups consists of factors with in total 12 levels.
> 
> A mock example could be the following that is working! :
> library(vegan)
> community<-data.frame(replicate(43,sample(0:1000,200,rep=TRUE)))
> groups<-as.factor(replicate(1,sample(c("Alpha","Beta","Gamma","Epsilon","Bact","Actino","Verr","Unclass","Cyano","Plancto","Eury","Chloro"),200,rep=T))
> simper_test<-simper(community,groups)
> summary(simper_test)
> 
> But please see the attached files for true data that is not working.
> 
> Could someone please please assist in what is the problem with my data.
> 
> Kind regards!

Markus,

I was getting the same error message and discovered that simper() is not
written to handle an input 'group' factor that has one or more unique values
with only one occurrence. Your data (reattached) have two of these
instances:

unq.grps=as.vector(unique(simper_group$x))
occurrences=integer(length = length(unq.grps))
for(i in 1:length(occurrences)) {
  occurrences[i] = length(simper_group[simper_group == unq.grps[i], ])
}
(ptable=data.frame(taxa = unq.grps, occurrences = occurrences))

The source code for simper() can be modified to allow these instances:

getAnywhere(simper)

Copy the source code, rename the function to your liking, and insert the
following code between the indicated lines of source code ('bookends'):

    n.b <- nrow(group.b) # source code upper bookend
    if(is.null(n.a)) { # new code begin
      group.a <- as.matrix(t(comm[group == comp[i, 1], ]))
      rownames(group.a) = 1  
      n.a <- nrow(group.a)
    }
    if(is.null(n.b)) {
      group.b <- as.matrix(t(comm[group == comp[i, 2], ]))
      rownames(group.b) = 1  
      n.b <- nrow(group.b)
    } # new code end
    contr <- matrix(ncol = P, nrow = n.a * n.b) # source code lower bookend

As originally written, the source code converts the data for a group with
only one occurrence to a named numeric vector as opposed to a matrix if
there is more than one occurrence. Calling nrow() for a single object group
results in a value of NULL (see upper source code bookend), which yields the
"invalid 'nrow' value (too large or NA)" error when attempting to generate
an empty matrix of the appropriate size (see lower source code bookend).

An alternative is to use the original simper() function and specify a
grouping structure that has at least two occurrences for each group (based
on a different trimmed cluster analysis output, for instance). This may not
be an option for your purposes.

Best, Mark

simper_comm.txt
<http://r-sig-ecology.471788.n2.nabble.com/file/n7579122/simper_comm.txt>  
simper_group.txt
<http://r-sig-ecology.471788.n2.nabble.com/file/n7579122/simper_group.txt>  



--
View this message in context: http://r-sig-ecology.471788.n2.nabble.com/SIMPER-problem-invalid-nrow-value-too-large-or-NA-tp7579026p7579122.html
Sent from the r-sig-ecology mailing list archive at Nabble.com.



More information about the R-sig-ecology mailing list