[Analogue-commits] r212 - in pkg: R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Mon Mar 14 15:39:28 CET 2011


Author: gsimpson
Date: 2011-03-14 15:39:28 +0100 (Mon, 14 Mar 2011)
New Revision: 212

Added:
   pkg/R/waFit.R
Modified:
   pkg/R/wa.R
   pkg/inst/ChangeLog
   pkg/man/wa.Rd
Log:
provides waFit, updates wa and documents the new fitting function workhorse

Modified: pkg/R/wa.R
===================================================================
--- pkg/R/wa.R	2011-03-14 14:37:48 UTC (rev 211)
+++ pkg/R/wa.R	2011-03-14 14:39:28 UTC (rev 212)
@@ -14,9 +14,6 @@
     ## drop species with no information
     if(any(csum <- colSums(x) == 0))
         x <- x[, !csum, drop = FALSE]
-    ## sample summaries
-    n.samp <- nrow(x)
-    n.spp <- ncol(x)
     if(missing(deshrink))
         deshrink <- "inverse"
     deshrink <- match.arg(deshrink)
@@ -27,9 +24,9 @@
         small.tol <- "min"
     small.tol <- match.arg(small.tol)
     ## do the WA
-    fit <- wa.fit(x = x, y = env, tol.dw = tol.dw, useN2 = useN2,
-                  deshrink = deshrink, na.tol = na.tol,
-                  small.tol = small.tol, min.tol = min.tol, f = f)
+    fit <- waFit(x = x, y = env, tol.dw = tol.dw, useN2 = useN2,
+                 deshrink = deshrink, na.tol = na.tol,
+                 small.tol = small.tol, min.tol = min.tol, f = f)
     ## site/sample names need to be reapplied
     names(fit$fitted.values) <- rownames(x)
     ## species names need to be reapplied
@@ -56,7 +53,8 @@
                 coefficients = fit$coefficients,
                 rmse = rmse, r.squared = r.squared,
                 avg.bias = avg.bias, max.bias = max.bias,
-                n.samp = n.samp, n.spp = n.spp,
+                n.samp = fit$n.samp,
+                n.spp = fit$n.spp,
                 deshrink = deshrink, tol.dw = tol.dw,
                 call = .call,
                 orig.x = x, orig.env = env,

Added: pkg/R/waFit.R
===================================================================
--- pkg/R/waFit.R	                        (rev 0)
+++ pkg/R/waFit.R	2011-03-14 14:39:28 UTC (rev 212)
@@ -0,0 +1,39 @@
+`waFit` <- function(x, y, tol.dw, useN2, deshrink, na.tol, small.tol,
+                     min.tol, f) {
+    ## sample summaries
+    n.samp <- nrow(x)
+    n.spp <- ncol(x)
+    ## calculate WA optima for each species in x
+    wa.optima <- w.avg(x, y)
+    ## compute tolerances
+    tolerances <- tol <- w.tol(x, y, wa.optima, useN2 = useN2)
+    ## fix-up tolerances for use in TF computations
+    if(small.tol == "fraction") {
+        if(!(f > 0 && f < 1))
+            stop("'f' must be 0 < f < 1")
+        frac <- f * diff(range(y))
+        if(frac < min.tol)
+            warning("Requested fraction of gradient is < minimum tolerance.")
+    }
+    tol <- fixUpTol(tol, na.tol = na.tol, small.tol = small.tol,
+                    min.tol = min.tol, f = f, env = y)
+    ## calculate WA estimate of env for each site
+    wa.env <- if(tol.dw) {
+        WATpred(x, wa.optima, tol, n.samp, n.spp)
+    } else {
+        WApred(x, wa.optima)
+    }
+    ## taken averages twice so deshrink
+    expanded <- deshrink(y, wa.env, type = deshrink)
+    wa.env <- expanded$env
+    coefficients <- coef(expanded)
+    ## returned object
+    res <- list(wa.optima = wa.optima,
+                tolerances = tolerances,
+                model.tol = tol,
+                fitted.values = wa.env,
+                coefficients = coefficients,
+                n.samp = n.samp,
+                n.spp = n.spp)
+    res
+}

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2011-03-14 14:37:48 UTC (rev 211)
+++ pkg/inst/ChangeLog	2011-03-14 14:39:28 UTC (rev 212)
@@ -55,7 +55,7 @@
 	* fixUpTol: erroneous error criterion would cause CV of WA models
 	with tolerance down-weighting to stop with an error.
 
-	* wa.fit: new function that encapsulates the main WA computations.
+	* waFit: new function that encapsulates the main WA computations.
 	This is currently used by wa() and with the intention of being
 	used in all functions that computed WA transfer function models.
 

Modified: pkg/man/wa.Rd
===================================================================
--- pkg/man/wa.Rd	2011-03-14 14:37:48 UTC (rev 211)
+++ pkg/man/wa.Rd	2011-03-14 14:39:28 UTC (rev 212)
@@ -6,16 +6,18 @@
 \alias{fitted.wa}
 \alias{residuals.wa}
 \alias{coef.wa}
+\alias{waFit}
 \title{Weighted averaging transfer functions}
 \description{
-Implements the weighted averaging transfer function
-methodology. Tolerance down-weighting and inverse and classicial
-deshrinking are supported.
+  Implements the weighted averaging transfer function
+  methodology. Tolerance down-weighting and inverse and classicial
+  deshrinking are supported.
 }
 \usage{
 wa(x, \dots)
 
-\method{wa}{default}(x, env, deshrink = c("inverse", "classical", "expanded", "none"),
+\method{wa}{default}(x, env,
+   deshrink = c("inverse", "classical", "expanded", "none"),
    tol.dw = FALSE, useN2 = TRUE,
    na.tol = c("min","mean","max"),
    small.tol = c("min","fraction","absolute"),
@@ -32,11 +34,14 @@
 \method{residuals}{wa}(object, \dots)
 
 \method{coef}{wa}(object, \dots)
+
+waFit(x, y, tol.dw, useN2, deshrink, na.tol, small.tol,
+      min.tol, f)
 }
 %- maybe also 'usage' for other objects documented here.
 \arguments{
   \item{x}{The species training set data}
-  \item{env}{The response vector}
+  \item{env, y}{The response vector}
   \item{deshrink}{Which deshrinking method to use? One of
     \code{"inverse"} or \code{"classical"}, \code{"expanded"} or
     \code{"none"}}
@@ -106,6 +111,12 @@
     \item{\code{absolute} }{small tolerances are replaced by
       \code{min.tol}.}
   }
+
+  Function \code{waFit} is the workhorse implementing the actual WA
+  computations. It performs no checks on the input data and returns a
+  simple list containing the optima, tolernances, model tolerances,
+  fitted values, coefficients and the numbers of samples and
+  species. See Value below for details of each component.
 }
 \value{
   An object of class \code{"wa"}, a list with the following components:



More information about the Analogue-commits mailing list