[Vegan-commits] r1328 - in pkg/vegan: R inst man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Oct 5 17:58:03 CEST 2010
Author: jarioksa
Date: 2010-10-05 17:58:02 +0200 (Tue, 05 Oct 2010)
New Revision: 1328
Modified:
pkg/vegan/R/nestednodf.R
pkg/vegan/inst/ChangeLog
pkg/vegan/man/nestedtemp.Rd
Log:
Gustavo Carvalho's new code with quantitative nestednodf (in press)
Modified: pkg/vegan/R/nestednodf.R
===================================================================
--- pkg/vegan/R/nestednodf.R 2010-10-04 05:40:19 UTC (rev 1327)
+++ pkg/vegan/R/nestednodf.R 2010-10-05 15:58:02 UTC (rev 1328)
@@ -1,44 +1,75 @@
`nestednodf` <-
- function(comm, order = TRUE)
+ function(comm, order = TRUE, weighted = FALSE)
{
- comm <- ifelse(comm > 0, 1, 0)
- ## Order rows and columns
- if (order)
+ ## Keep it consistent with similar functions
+ if (!weighted) {
+ comm <- ifelse(comm > 0, 1, 0)
+ }
+ if (order) {
comm <- comm[order(rowSums(comm), decreasing=TRUE),
order(colSums(comm), decreasing=TRUE)]
- dimensions <- dim(comm)
- fill <- sum(comm)/length(comm)
- N.paired <- 0
- ## Function to be applied to each combination of rows and columns
- comb <- function(x, rows) {
- if (identical(rows,TRUE)) {
- comb.first <- comm[x[1],]
- comb.second <- comm[x[2],]
+ }
+ fill <- comm > 0
+ rfill <- rowSums(fill)
+ cfill <- colSums(fill)
+ nr <- NROW(comm)
+ nc <- NCOL(comm)
+ fill <- sum(rfill)/length(comm)
+ ## Initializing the vectors that will hold nodf values
+ ## for each combination of rows and columns
+ N.paired.rows <- numeric(nr * (nr - 1) / 2)
+ N.paired.cols <- numeric(nc * (nc - 1) / 2)
+ counter <- 0
+ ## Nested loops to get every combination of rows/columns
+ ## Row i
+ for (i in 1:(nr-1)) {
+ first <- comm[i, ]
+ ## Row j
+ for(j in (i + 1):nr) {
+ ## Counting the number of nodfs calculated so far
+ counter <- counter + 1
+ if (rfill[i] <= rfill[j] || any(rfill[c(i, j)] == 0))
+ next
+ if (weighted) {
+ second <- comm[j, ]
+ N.paired.rows[counter] <- sum(first - second > 0 & second > 0) /
+ sum(second > 0)
+ }
+ else {
+ N.paired.rows[counter] <- sum(first + comm[j, ] == 2) /
+ rfill[j]
+ }
}
- else {
- comb.first <- comm[,x[1]]
- comb.second <- comm[,x[2]]
+ }
+ ## Reseting the counter
+ counter <- 0
+ ## Column i
+ for (i in 1:(nc-1)) {
+ first <- comm[, i]
+ ## Column j
+ for(j in (i + 1):nc) {
+ counter <- counter + 1
+ if (cfill[i] <= cfill[j] || any(cfill[c(i, j)] == 0))
+ next
+ if (weighted) {
+ second <- comm[, j]
+ N.paired.cols[counter] <- sum(first - second > 0 & second > 0) /
+ sum(second > 0)
+ }
+ else {
+ N.paired.cols[counter] <- sum(first + comm[, j] == 2) /
+ cfill[j]
+ }
}
- ## if MTi > MTj
- if (sum(comb.first) > sum(comb.second) && sum(comb.second) > 0) {
- paired.overlap <- sum((comb.first + comb.second) == 2) /
- sum(comb.second)
- N.paired <- paired.overlap
- }
- return(N.paired)
}
- ## N.paired for all combinations of columns and rows
- N.paired.rows <- combn(1:dimensions[1],2, comb, rows=TRUE)
- N.paired.columns <- combn(1:dimensions[2],2, comb, rows=FALSE)
- ## Index calculations
- N.columns <- mean(N.paired.columns) * 100
- N.rows <- mean(N.paired.rows) * 100
- NODF <- (sum(c(N.paired.rows, N.paired.columns)) * 100) /
- ((dimensions[2] * (dimensions[2] - 1) / 2) +
- (dimensions[1] * (dimensions[1] - 1) / 2))
- ## Returned list
+ ## Calculating the means and the NODF for the whole matrix
+ N.columns <- mean(N.paired.cols) * 100
+ N.rows <- mean(N.paired.rows) * 100
+ NODF <- (sum(c(N.paired.rows, N.paired.cols)) * 100)/
+ ((nc * (nc - 1)/2) + (nr * (nr - 1)/2))
out <- list(comm = comm, fill = fill,
- statistic=c("N.columns" = N.columns, "N.rows" = N.rows, "NODF" = NODF))
+ statistic=c("N.columns" = N.columns, "N.rows" = N.rows,
+ "NODF" = NODF))
class(out) <- "nestednodf"
return(out)
}
Modified: pkg/vegan/inst/ChangeLog
===================================================================
--- pkg/vegan/inst/ChangeLog 2010-10-04 05:40:19 UTC (rev 1327)
+++ pkg/vegan/inst/ChangeLog 2010-10-05 15:58:02 UTC (rev 1328)
@@ -4,6 +4,11 @@
Version 1.18-13 (opened September 26, 2010)
+ * nestednodf: Gustavo Carvalho sent a new upgraded version which
+ also implements a new quantitative method of Almeida-Neto & Ulrich
+ (Env Mod Software, in press; 2010). Based on feature request #1097
+ in R-Forge.
+
* ordistep: name clash if the fitted model contained item or term
called 'mod'. Reported by Richard Telford (Bergen, Norway).
Modified: pkg/vegan/man/nestedtemp.Rd
===================================================================
--- pkg/vegan/man/nestedtemp.Rd 2010-10-04 05:40:19 UTC (rev 1327)
+++ pkg/vegan/man/nestedtemp.Rd 2010-10-05 15:58:02 UTC (rev 1328)
@@ -26,7 +26,7 @@
nestedn0(comm)
nesteddisc(comm)
nestedtemp(comm, ...)
-nestednodf(comm, order = TRUE)
+nestednodf(comm, order = TRUE, weighted = FALSE)
\method{plot}{nestedtemp}(x, kind = c("temperature", "incidence"),
col=rev(heat.colors(100)), names = FALSE, ...)
}
@@ -40,6 +40,7 @@
If it is a logical vector of length 2, row and column labels are
returned accordingly.}
\item{order}{Order rows and columns by frequencies.}
+ \item{weighted}{Use species abundances as weights of interactions.}
\item{\dots}{Other arguments to functions.}
}
@@ -97,16 +98,19 @@
Function \code{nestednodf} implements a nestedness metric based on
overlap and decreasing fill (Almeida-Neto et al., 2008). Two basic
properties are required for a matrix to have the maximum degree of
- nestedness according to this metric: (1) complete overlap of 1's from
- right to left columns and from down to up rows, and (2) decreasing
- marginal totals between all pairs of columns and all pairs of
- rows. The nestedness statistic is evaluated separately for columns
- (\code{N columns}) for rows (\code{N rows}) and combined for the whole
- matrix (\code{NODF}). If you set \code{order = FALSE}, the statistic
- is evaluated with the current matrix ordering allowing tests of other
- meaningful hypothesis of matrix structure than ordering by row and
- column totals (see Almeida-Neto et al. 2008).
-}
+ nestedness according to this metric: (1) complete overlap of 1's
+ from right to left columns and from down to up rows, and (2)
+ decreasing marginal totals between all pairs of columns and all
+ pairs of rows. The nestedness statistic is evaluated separately for
+ columns (\code{N columns}) for rows (\code{N rows}) and combined for
+ the whole matrix (\code{NODF}). If you set \code{order = FALSE},
+ the statistic is evaluated with the current matrix ordering allowing
+ tests of other meaningful hypothesis of matrix structure than
+ ordering by row and column totals (see Almeida-Neto et
+ al. 2008). With \code{weighted = TRUE}, the function finds the
+ weighted version of the index (Almeida-Neto & Ulrich,
+ 2010). However, this requires quantitative null models for adequate
+ testing.}
\value{
The result returned by a nestedness function contains an item called
@@ -121,6 +125,10 @@
\enc{Gumarães}{Gumaraes}, P.R., Loyola, R.D. & Ulrich, W. (2008). A
consistent metric for nestedness analysis in ecological systems:
reconciling concept and measurement. \emph{Oikos} 117, 1227--1239.
+
+ Almeida-Neto, M. & Ulrich, W. (2010). A straightforward
+ computational approac for measuring nestedness using quantitative
+ matrices. \emph{Env. Mod. Software} (in press).
Atmar, W. & Patterson, B.D. (1993). The measurement of order and
disorder in the distribution of species in fragmented
More information about the Vegan-commits
mailing list