[Vegan-commits] r531 - in pkg: . R man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Oct 22 05:47:44 CEST 2008


Author: psolymos
Date: 2008-10-22 05:47:43 +0200 (Wed, 22 Oct 2008)
New Revision: 531

Modified:
   pkg/DESCRIPTION
   pkg/R/beals.R
   pkg/man/beals.Rd
Log:
beals function modified


Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2008-10-20 09:10:16 UTC (rev 530)
+++ pkg/DESCRIPTION	2008-10-22 03:47:43 UTC (rev 531)
@@ -1,7 +1,7 @@
 Package: vegan
 Title: Community Ecology Package
 Version: 1.16-2
-Date: October 17, 2008
+Date: October 21, 2008
 Author: Jari Oksanen, Roeland Kindt, Pierre Legendre, Bob O'Hara, Gavin L. Simpson, 
    Peter Solymos, M. Henry H. Stevens, Helene Wagner  
 Maintainer: Jari Oksanen <jari.oksanen at oulu.fi>

Modified: pkg/R/beals.R
===================================================================
--- pkg/R/beals.R	2008-10-20 09:10:16 UTC (rev 530)
+++ pkg/R/beals.R	2008-10-22 03:47:43 UTC (rev 531)
@@ -1,90 +1,67 @@
-
 ##############################################################
 ##  COMPUTES BEALS SMOOTHING FOR ALL SPECIES IN TABLE        #
 ##  This is a more complete function than the previous one   #
 ##  in the vegan package. The parameter values that give the #
-##  equivalence are 'beals(x, x, mode=0,incSp=TRUE)'         #
+##  equivalence are 'beals(x, NA, x, 0, include=TRUE)'       #
 ##                                                           #
-##  x     matrix to be replaced by beals values              #
-##  refX  matrix to be used as source for joint occurrences  #
-##  incSp flag to include target species in the computation  #
-##  mode  sets the way to use abundance values               #
+##  'x'     matrix to be replaced by beals values            #
+##  'reference' matrix to be used as source for joint occurrences#
+##  'type'  sets the way to use abundance values             #
 ##       0 - presence/absence                                #
 ##       1 - abundances for conditioned probabilities        #
 ##       2 - abundances for weighted average                 #
 ##       3 - abundances for both                             #
+##  'species' a column index used to compute Beals function  #
+##        for a single species. The default (NA) indicates   #
+##        all species.                                       #
+##  'include' flag to include target species in the computation#
 ##############################################################
-
-beals <- function(x, refX = x, mode=0, incSp = TRUE)
+beals<-function(x, species=NA, reference=x, type=0, include=TRUE)
 {
-    if(is.null(refX))
-        refX <- x
-    refX <- as.matrix(refX)
-    x <- as.matrix(x)
-    
-    if(mode==0 || mode ==2) refX <- ifelse(refX > 0, 1, 0)
-    if(mode==0 || mode ==1) x <- ifelse(x > 0, 1, 0)
-    
-    ## Computes conditioned probabilities
-    M <- crossprod(ifelse(refX > 0, 1, 0), refX)
-    C <- diag(M)
-    M <- sweep(M, 2, replace(C,C==0,1), "/")
-    if(!incSp) {
-        for (i in 1:ncol(refX))
-            M[i,i] <- 0
+    refX <- reference
+    # this checks whether it was chosen from available options
+    mode <- as.numeric(match.arg(as.character(type), c("0","1","2","3")))
+    spIndex <- species
+    incSp <- include
+#    if(is.null(refX))              # I think this is unnecessary because of the default above
+#            refX<-x                # and it should look like missing(refX) and not is.null(refX)
+   refX <- as.matrix(refX)
+   x <- as.matrix(x)
+   if(mode==0 || mode ==2) refX <- ifelse(refX > 0, 1, 0)
+   if(mode==0 || mode ==1) x <- ifelse(x > 0, 1, 0)
+   #Computes conditioned probabilities
+   if(is.na(spIndex)){
+       M <- crossprod(ifelse(refX > 0, 1, 0),refX)
+      C <-diag(M)
+     M <- sweep(M, 2, replace(C,C==0,1), "/")
+     if(!incSp)
+         for (i in 1:ncol(refX))
+            M[i,i]<-0
+    } else {
+       C<-colSums(refX)
+       M<-crossprod(refX,ifelse(refX > 0, 1, 0)[,spIndex])
+       M<-M/replace(C,C==0,1)
+       if(!incSp)
+          M[spIndex]<-0
     }
-    
-    ## Average of conditioned probabilities
-    S <- rowSums(x)
-    b <-x
-    for (i in 1:nrow(x)) {
-        b[i, ] <- rowSums(sweep(M, 2, x[i, ], "*"))
-    }
-    
-    SM <- rep(S,ncol(x))
-    if(!incSp)
-        SM <- SM - x
-    b <- b/replace(SM,SM==0,1)
-    b
+   #Average of conditioned probabilities
+   S <- rowSums(x)
+   if(is.na(spIndex)) {
+       b <-x
+       for (i in 1:nrow(x)) {
+       b[i, ] <- rowSums(sweep(M, 2, x[i, ], "*"))
+       }
+       SM<-rep(S,ncol(x))
+       if(!incSp)
+           SM<-SM-x
+       b <- b/replace(SM,SM==0,1)
+   } else {
+       b<-rowSums(sweep(x,2,M,"*"))
+       if(!incSp)
+           S<-S-x[,spIndex]
+       b <- b/replace(S,S==0,1)
+   }
+#   attr(b, "smoothtype") <- mode
+#   class(b) <- c("beals", class(b))        # this can later be used to write the methods, i.e. beals test, etc.
+   return(b)
 }
-
-
-##############################################################
-## COMPUTES BEALS SMOOTHING FOR A GIVEN SPECIES              #
-##                                                           #
-##  x     matrix to be replaced by beals values              #
-##  refX  matrix to be used as source for joint occurrences  #
-##  incSp flag to include species in the computation         #
-##  mode  sets the way to use abundance values               #
-##       0 - presence/absence                                #
-##       1 - abundances for conditioned probabilities        #
-##       2 - abundances for weighted average                 #
-##       3 - abundances for both                             #
-##############################################################
-
-beals.sp <- function(x, spIndex, refX = x, mode=0, incSp = FALSE)
-{
-    if(is.null(refX))
-        refX <- x
-    refX <- as.matrix(refX)
-	
-    if(mode==0 || mode ==2) refX <- ifelse(refX > 0, 1, 0)
-    if(mode==0 || mode ==1) x <- ifelse(x > 0, 1, 0)
-	
-	
-    ## Computes conditioned probabilities
-    C <- colSums(refX)
-    M <- crossprod(refX,ifelse(refX > 0, 1, 0)[,spIndex])
-    M <- M/replace(C,C==0,1)
-    if(!incSp)
-        M[spIndex]<-0
-	
-    ## Average of conditioned probabilities
-    b <- rowSums(sweep(x,2,M,"*"))
-    S <- rowSums(x)
-	
-    if (!incSp)
-        S <- S - x[,spIndex]
-    b <- b/replace(S,S==0,1)
-    b
-}

Modified: pkg/man/beals.Rd
===================================================================
--- pkg/man/beals.Rd	2008-10-20 09:10:16 UTC (rev 530)
+++ pkg/man/beals.Rd	2008-10-22 03:47:43 UTC (rev 531)
@@ -1,7 +1,6 @@
 \encoding{UTF-8}
 \name{beals}
 \alias{beals}
-\alias{beals.sp}
 \alias{swan}
 
 \title{Beals Smoothing and Degree of Absence }
@@ -14,29 +13,27 @@
   with smoothed values. 
 }
 \usage{
-beals(x, refX = x, mode = 0, incSp = TRUE)
-beals.sp(x, spIndex, refX = x, mode = 0, incSp = FALSE)
+beals(x, species = NA, reference = x, type = 0, include = TRUE)
 swan(x)
 }
 \arguments{
   \item{x}{Community data frame or matrix }
-  \item{spIndex}{Index of the single species whose Beals smoothing
-  values are to be computed. } 
-  \item{refX}{ Community data frame or matrix to be used to compute
-  joint occurrences. If \code{refX=NULL} then \code{x} is used to
+  \item{species}{ Column index used to compute Beals function for a single species. The default (\code{NA}) indicates that the function will be computed for all species.} 
+  \item{reference}{ Community data frame or matrix to be used to compute
+  joint occurrences. By default, \code{x} is used as reference to
   compute the joint occurrences.} 
-  \item{incSp}{ This flag indicates whether the target species has to be
-  included when computing the mean of the conditioned probabilities. The
-  original Beals (1984) definition is equivalent to \code{incSp=TRUE},
-  while the formulation of \enc{Münzbergová}{Munzbergova} and Herben is
-  equal to \code{incSp=FALSE}. }  
-  \item{mode }{Specifies if and how abundance values have to be
-  used. \code{mode = 0} presence/absence mode. \code{mode = 1}
-  abundances in \code{refX} (or \code{x}) are used to compute
-  conditioned probabilities. \code{mode = 2} abundances in \code{x} are
+  \item{type}{Specifies if and how abundance values have to be
+  used. \code{type = 0} presence/absence mode. \code{type = 1}
+  abundances in \code{reference} (or \code{x}) are used to compute
+  conditioned probabilities. \code{type = 2} abundances in \code{x} are
   used to compute weighted average of conditioned
-  probabilities. \code{mode = 3} abundances are used to compute both
+  probabilities. \code{type = 3} abundances are used to compute both
   conditioned probabilities and the weighted average.}  
+  \item{include}{This logical flag indicates whether the target species has to be
+  included when computing the mean of the conditioned probabilities. The
+  original Beals (1984) definition is equivalent to \code{include=TRUE},
+  while the formulation of \enc{Münzbergová}{Munzbergova} and Herben is
+  equal to \code{include=FALSE}. }  
 }
 \details{
 
@@ -65,9 +62,9 @@
   belong to the species pool (Ewald 2002) or to identify suitable
   unoccupied patches in metapopulation analysis
   (\enc{Münzbergová}{Munzbergova} & Herben 
-  2004).  In this case, the function shold be called with \code{incSp
-  = FALSE} for cross-validatory smoothing for species, and function
-  \code{beals.sp} can be used if onl one species was studied.
+  2004).  In this case, the function shold be called with \code{include
+  = FALSE} for cross-validatory smoothing for species, and argument
+  \code{species} can be used if only one species was studied.
 
   Swan (1970) suggested replacing zero values with degrees of absence of
   a species in a community data matrix. Swan expressed the method in
@@ -79,7 +76,7 @@
   \code{\link{stepacross}}), but very rarely used. 
 }
 \value{
-  The function returns a transformed data matrix.
+  The function returns a transformed data matrix or a vector in case of asking Beals smoothing for a single species.
 }
 \references{
   
@@ -115,16 +112,16 @@
 ## Default
 x <- beals(dune)
 ## Remove target species
-x <- beals(dune, incSp = FALSE)
+x <- beals(dune, include = FALSE)
 ## Smoothed values against presence or absence of species
 pa <- decostand(dune, "pa")
 boxplot(as.vector(x) ~ unlist(pa), xlab="Presence", ylab="Beals")
 ## Remove the bias of tarbet species: Yields lower values.
-beals(dune, mode =3, incSp = FALSE)
+beals(dune, type =3, include = FALSE)
 ## Uses abundance information.
 ## Vector with beals smoothing values corresponding to the first species
 ## in dune.
-beals.sp(dune, spIndex=1, incSp=TRUE) 
+beals(dune, species=1, include=TRUE) 
 }
 \keyword{ manip }
 \keyword{ smooth }



More information about the Vegan-commits mailing list