[Vegan-commits] r2553 - in pkg/vegan: R man
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Wed Jul 10 13:48:43 CEST 2013
Author: jarioksa
Date: 2013-07-10 13:48:43 +0200 (Wed, 10 Jul 2013)
New Revision: 2553
Added:
pkg/vegan/R/dispweight.R
pkg/vegan/man/dispweight.Rd
Removed:
pkg/vegan/R/disp_weight.R
pkg/vegan/man/disp_weight.Rd
Log:
renamed function, returns list with transformed data, assigned class
Deleted: pkg/vegan/R/disp_weight.R
===================================================================
--- pkg/vegan/R/disp_weight.R 2013-07-10 11:48:42 UTC (rev 2552)
+++ pkg/vegan/R/disp_weight.R 2013-07-10 11:48:43 UTC (rev 2553)
@@ -1,59 +0,0 @@
-`disp_weight` <-
- function(comm, group, nperm = 1000)
-{
- # number of replicates per group
- nrep <- tabulate(group)
- # workhorse
- dfun <- function(comm, group, nperm, nrep) {
- ## Calc Dispersion
- # group means
- means <- tapply(comm, group, mean)
- # omit groups with mean == 0
- if(any(means == 0)) {
- comm <- comm[means[group] != 0]
- group <- group[means[group] != 0, drop = TRUE]
- nrep <- nrep[means != 0]
- means <- means[means != 0]
- }
- # group variances
- vars <- tapply(comm, group, var)
- # dispersion
- d <- vars / means
- # average dispersion
- d_hat <- sum(d * (nrep - 1)) / sum(nrep - 1)
-
- ## Test
- # original chisq values
- chi_o <- sum((comm - means[group])^2 / means[group])
-
- # permutations
- # calculate chisq
- pfun <- function(comm, group){
- means <- tapply(comm, group, mean)
- if(any(means == 0)) {
- comm <- comm[means[group] != 0]
- group <- group[means[group] != 0, drop = TRUE]
- means <- means[means != 0]
- }
- chi <- sum((comm - means[group])^2 / means[group])
- return(chi)
- }
- # sum of individuals per group
- sums <- tapply(comm, group, sum)
- # realocate randomly individuals to replications
- perms <- vector('list', length(sums))
- for(i in seq_along(sums)) {
- perms[[i]] <- rmultinom(n = nperm, size = sums[i], prob = rep(1, nrep[i]) / nrep[i])
- }
- perms <- t(do.call(rbind, perms))
- chi_p <- apply(perms, 1, pfun, sort(group))
- p <- (sum(chi_p >= chi_o) + 1) / (nperm + 1)
- out <- list(D = d_hat, p = p, weights = ifelse(p < 0.05, 1/d_hat, 1))
- return(out)
- }
- # apply workhorse to every species
- out <- apply(comm, 2, dfun, group, nperm, nrep)
- # format output
- out <- do.call(rbind.data.frame, out)
- return(out)
-}
\ No newline at end of file
Copied: pkg/vegan/R/dispweight.R (from rev 2552, pkg/vegan/R/disp_weight.R)
===================================================================
--- pkg/vegan/R/dispweight.R (rev 0)
+++ pkg/vegan/R/dispweight.R 2013-07-10 11:48:43 UTC (rev 2553)
@@ -0,0 +1,66 @@
+`dispweight` <-
+ function(comm, group, nperm = 1000)
+{
+ # number of replicates per group
+ nrep <- tabulate(group)
+ # workhorse
+ dfun <- function(comm, group, nperm, nrep) {
+ ## Calc Dispersion
+ # group means
+ means <- tapply(comm, group, mean)
+ # omit groups with mean == 0
+ if(any(means == 0)) {
+ comm <- comm[means[group] != 0]
+ group <- group[means[group] != 0, drop = TRUE]
+ nrep <- nrep[means != 0]
+ means <- means[means != 0]
+ }
+ # group variances
+ vars <- tapply(comm, group, var)
+ # dispersion
+ d <- vars / means
+ # average dispersion
+ d_hat <- sum(d * (nrep - 1)) / sum(nrep - 1)
+
+ ## Test
+ # original chisq values
+ chi_o <- sum((comm - means[group])^2 / means[group])
+
+ # permutations
+ # calculate chisq
+ pfun <- function(comm, group){
+ means <- tapply(comm, group, mean)
+ if(any(means == 0)) {
+ comm <- comm[means[group] != 0]
+ group <- group[means[group] != 0, drop = TRUE]
+ means <- means[means != 0]
+ }
+ chi <- sum((comm - means[group])^2 / means[group])
+ return(chi)
+ }
+ # sum of individuals per group
+ sums <- tapply(comm, group, sum)
+ # realocate randomly individuals to replications
+ perms <- vector('list', length(sums))
+ for(i in seq_along(sums)) {
+ perms[[i]] <- rmultinom(n = nperm, size = sums[i], prob = rep(1, nrep[i]) / nrep[i])
+ }
+ perms <- t(do.call(rbind, perms))
+ chi_p <- apply(perms, 1, pfun, sort(group))
+ p <- (sum(chi_p >= chi_o) + 1) / (nperm + 1)
+ out <- list(D = d_hat, p = p, weights = ifelse(p < 0.05, 1/d_hat, 1))
+ return(out)
+ }
+ # apply workhorse to every species
+ out <- apply(comm, 2, dfun, group, nperm, nrep)
+
+ # format output
+ weights <- unlist(sapply(out, '[', 3))
+ out <- list(D = unlist(sapply(out, '[', 1)),
+ p = unlist(sapply(out, '[', 2)),
+ weights = weights,
+ transformed = sweep(comm, MARGIN = 2, weights, `*`))
+ attr(out, "permutations") <- nperm
+ class(out) <- "dispweight"
+ return(out)
+}
\ No newline at end of file
Deleted: pkg/vegan/man/disp_weight.Rd
===================================================================
--- pkg/vegan/man/disp_weight.Rd 2013-07-10 11:48:42 UTC (rev 2552)
+++ pkg/vegan/man/disp_weight.Rd 2013-07-10 11:48:43 UTC (rev 2553)
@@ -1,56 +0,0 @@
-\encoding{UTF-8}
-\name{disp_weight}
-\alias{disp_weight}
-\title{Dispersion-based weighting of species counts}
-
-\description{Transform abundance data using dispersion-based weighting of species counts}
-
-\usage{disp_weight(comm, group, nperm = 1000)}
-
-\arguments{
- \item{comm}{Community data matrix.}
- \item{group}{Factor describing the group structure.}
- \item{nperm}{Number of permutations}
-}
-
-\details{
-Dispersion weighting can be used when there are \emph{a priori} defined groups
-(eg. lacation of samples).
-
-The dispersion index (D) is calculated as ratio between
-variance and mean and averaged across groups. D can then be used as divisor to
-downweight species abundances.
-
-However species should be only downweighted, when there is evidence for
-over-dispersion (D > 1). This is tested using a permutational Chi-squared test.
-If this test shows statistically significant over-dispersion then the species is
-downweighted by 1/D, else the divisor is 1.
-}
-
-\value{
-A data.frame with three columns:
- \item{D}{Average Dispersion index}
- \item{p}{p-value of permutation test that D = 1}
- \item{weights}{weights to apply to community data}
-}
-
-\references{
-Clarke, K. R., M. G. Chapman, P. J. Somerfield, and H. R. Needham. 2006. Dispersion-based Weighting of Species Counts in Assemblage Analyses. \emph{Marine Ecology Progress Series}, 320, 11–27.
-}
-\author{
- Eduard Szöcs \email{szoe8822 at uni-landau.de}
-}
-
-
-\examples{
-data(dune)
-data(dune.env)
-# calculate weights
-dpw <- disp_weight(dune, dune.env$Management, nperm = 100)
-# calculate transformed data
-sweep(dune, MARGIN = 2, dpw$weights, `*`)
-}
-
-
-\keyword{multivariate}
-
Copied: pkg/vegan/man/dispweight.Rd (from rev 2552, pkg/vegan/man/disp_weight.Rd)
===================================================================
--- pkg/vegan/man/dispweight.Rd (rev 0)
+++ pkg/vegan/man/dispweight.Rd 2013-07-10 11:48:43 UTC (rev 2553)
@@ -0,0 +1,57 @@
+\encoding{UTF-8}
+\name{disp_weight}
+\alias{disp_weight}
+\title{Dispersion-based weighting of species counts}
+
+\description{Transform abundance data using dispersion-based weighting of species counts}
+
+\usage{disp_weight(comm, group, nperm = 1000)}
+
+\arguments{
+ \item{comm}{Community data matrix.}
+ \item{group}{Factor describing the group structure.}
+ \item{nperm}{Number of permutations.}
+}
+
+\details{
+Dispersion weighting can be used when there are \emph{a priori} defined groups
+(eg. lacation of samples).
+
+The dispersion index (D) is calculated as ratio between
+variance and mean and averaged across groups. D can then be used as divisor to
+downweight species abundances.
+
+However species should be only downweighted, when there is evidence for
+over-dispersion (D > 1). This is tested using a permutational Chi-squared test.
+If this test shows statistically significant over-dispersion then the species is
+downweighted by 1/D, else the divisor is 1.
+}
+
+\value{
+A list of class \code{dispweight} with the following items:
+ \item{D}{Average Dispersion index.}
+ \item{p}{p-value of permutation test that D = 1.}
+ \item{weights}{weights applied to community data.}
+ \item{transformed}{transformed community data.}
+}
+
+\references{
+Clarke, K. R., M. G. Chapman, P. J. Somerfield, and H. R. Needham. 2006. Dispersion-based Weighting of Species Counts in Assemblage Analyses. \emph{Marine Ecology Progress Series}, 320, 11–27.
+}
+\author{
+ Eduard Szöcs \email{szoe8822 at uni-landau.de}
+}
+
+
+\examples{
+data(dune)
+data(dune.env)
+# calculate weights
+dpw <- disp_weight(dune, dune.env$Management, nperm = 100)
+# transformed community data
+dpw$transformed
+}
+
+
+\keyword{multivariate}
+
More information about the Vegan-commits
mailing list