[Analogue-commits] r397 - in pkg: . R inst man

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Sun Dec 22 03:03:05 CET 2013


Author: gsimpson
Date: 2013-12-22 03:03:02 +0100 (Sun, 22 Dec 2013)
New Revision: 397

Added:
   pkg/R/predict.prcurve.R
   pkg/man/predict.prcurve.Rd
Modified:
   pkg/NAMESPACE
   pkg/R/prcurve.R
   pkg/inst/ChangeLog
   pkg/man/prcurve.Rd
Log:
add first pass at a predict method for principal curves

Modified: pkg/NAMESPACE
===================================================================
--- pkg/NAMESPACE	2013-12-16 18:58:00 UTC (rev 396)
+++ pkg/NAMESPACE	2013-12-22 02:03:02 UTC (rev 397)
@@ -185,6 +185,7 @@
 S3method(predict, logitreg)
 S3method(predict, mat)
 S3method(predict, pcr)
+S3method(predict, prcurve)
 S3method(predict, wa)
 S3method(residuals, bootstrap.mat)
 S3method(residuals, mat)

Modified: pkg/R/prcurve.R
===================================================================
--- pkg/R/prcurve.R	2013-12-16 18:58:00 UTC (rev 396)
+++ pkg/R/prcurve.R	2013-12-22 02:03:02 UTC (rev 397)
@@ -23,7 +23,7 @@
                     ## latent = FALSE,
                     ...) {
     ## X should be a matrix, attempt to coerce
-    if(!isTRUE(all.equal(class(X), "matrix")))
+    if(!isTRUE(inherits(X, "matrix")))
         X <- data.matrix(X)
     ## set/select default method for starting configuration
     if(missing(method))
@@ -208,6 +208,7 @@
     config$call <- match.call()
     config$ordination <- ord
     config$data <- X
+    config$stretch <- stretch
     class(config) <- c("prcurve")
     config
 }

Added: pkg/R/predict.prcurve.R
===================================================================
--- pkg/R/predict.prcurve.R	                        (rev 0)
+++ pkg/R/predict.prcurve.R	2013-12-22 02:03:02 UTC (rev 397)
@@ -0,0 +1,24 @@
+`predict.prcurve` <- function(object, newdata, ...) {
+    if(missing(newdata))
+        return(fitted(object))
+
+    ## check the variable names in newdata match with original data
+    ## essentially, this will only work if the names match, hence
+    ## join() likely useful for the user
+    nNew <- colnames(newdata)
+    nData <- colnames(object$data)
+    if (!isTRUE(all.equal(nNew, nData))) {
+        if (isTRUE(all.equal(sort(nNew), sort(nData)))) {
+            newdata <- newdata[, nData]
+        } else {
+            stop("Variables in 'newdata' don't match with training datat.")
+        }
+    }
+
+    ## otherwise project points on to the curve
+    p <- get.lam(data.matrix(newdata), s = object$s, tag = object$tag,
+                 stretch = object$stretch)
+    out <- p$s
+    attr(out, "tag") <- p$tag
+    out
+}

Modified: pkg/inst/ChangeLog
===================================================================
--- pkg/inst/ChangeLog	2013-12-16 18:58:00 UTC (rev 396)
+++ pkg/inst/ChangeLog	2013-12-22 02:03:02 UTC (rev 397)
@@ -6,6 +6,10 @@
 	`analogue:::fitPCR()`, which is not required nor was it intended.
 	Reported by Brian Ripley.
 
+	* predict.prcurve: new function to predict locations on the curve
+	for new observations on the same set of variables. Useful for
+	adding passive species.
+
 Version 0.12-0
 
 	* Released to CRAN December 13th 2013

Modified: pkg/man/prcurve.Rd
===================================================================
--- pkg/man/prcurve.Rd	2013-12-16 18:58:00 UTC (rev 396)
+++ pkg/man/prcurve.Rd	2013-12-22 02:03:02 UTC (rev 397)
@@ -132,6 +132,10 @@
 aber.pc2 <- prcurve(abernethy2, method = "ca", trace = TRUE,
                     vary = TRUE, penalty = 1.4)
 
+## Predict new locations
+take <- abernethy2[1:10, ]
+pred <- predict(aber.pc2, take)
+
 \dontrun{
 ## Fit principal curve using a GAM - currently slow ~10secs
 aber.pc3 <- prcurve(abernethy2, method = "ca", trace = TRUE,

Added: pkg/man/predict.prcurve.Rd
===================================================================
--- pkg/man/predict.prcurve.Rd	                        (rev 0)
+++ pkg/man/predict.prcurve.Rd	2013-12-22 02:03:02 UTC (rev 397)
@@ -0,0 +1,57 @@
+\name{predict.prcurve}
+\alias{predict.prcurve}
+
+\title{Predict locations on a principal curve}
+\description{
+  Locations on a fitted principal curve are predicted by projecting the
+  new observations in \eqn{m} dimensions on to the corresponding closest
+  point on the curve.
+}
+\usage{
+\method{predict}{prcurve}(object, newdata, \dots)
+}
+
+\arguments{
+  \item{object}{
+    an object of class \code{\link{prcurve}}.
+  }
+  \item{newdata}{
+    a matrix or data frame of new observations within the space of the
+    orginal data. Variables are matched against those of the original
+    data via their \code{names} or \code{colnames}. If a data frame is
+    supplied, it is converted to a matrix via \code{\link{data.matrix}}.
+  }
+  \item{\dots}{
+    other arguments passed to other methods. Not currently used.
+  }
+}
+\details{
+  Fitting a principal curve involves two procedures. In one, the current
+  curve is bent towards the data via the fitting of spline functions
+  with distance along the curve as the predictor variable and each
+  variable in turn as the response. The second procedure, a projection
+  step, involves projecting the observed points in \eqn{m} dimensions on
+  to locations along the current curve to which they are closest in the
+  hyperspace.
+
+  Given a fitted curve, the projection step can be used to find new
+  points on the fitted curve by projecting the new points located in the
+  hyperspace on to points on the curve to which they are closest.
+}
+\value{
+  A matrix of points in the space of the original data. Rows correspond
+  to the new samples and columns to the variables (ordered as per the
+  original data used to fit the curve).
+
+  How these points are ordered along the fitted curve is contained in
+  attributed \code{tag}.
+}
+\author{
+  Gavin L. Simpson
+}
+\seealso{
+  See \code{\link{prcurve}} for details on fitting principal curves and
+  an example.
+}
+
+\keyword{ methods }



More information about the Analogue-commits mailing list