| Title: | Principal Surface Contour Biplots |
|---|---|
| Description: | Fits principal surfaces, the two-dimensional generalisation of the principal curves of Hastie and Stuetzle (1989) <doi:10.1080/01621459.1989.10478797>, and displays them as biplots in which each variable is read off the contour lines of its fitted surface coordinate function. Sample predictivity, the proportion of a sample's squared length that is reconstructed by the fitted surface, is reported as a per-sample diagnostic of how well the surface represents the data. |
| Authors: | Raeesa Ganey [aut, cre, cph] |
| Maintainer: | Raeesa Ganey <[email protected]> |
| License: | MIT + file LICENSE |
| Version: | 2.0 |
| Built: | 2026-10-09 10:39:25 UTC |
| Source: | https://github.com/raeesaganey91/prinsurf |
For each variable, the root-mean-square difference between the value read
from the variable's contour lines at each sample's biplot position (as
predict.prinsurf does) and the sample's actual value. This
measures how accurately a variable can be recovered by reading its contours,
combining the surface's lack of fit for that variable with the small loss
from interpolating between contour lines; lower is better, in the units of
the working data. Samples that fall outside the supported part of the
contour grid cannot be read and are excluded; their number is returned as the
attribute "n.unread".
contour_predictive_error(object)contour_predictive_error(object)
object |
A |
A named numeric vector of per-variable RMS contour-reading errors,
with the mean over all variables as the attribute "overall" and the
number of unreadable samples as the attribute "n.unread".
predictivity for whole-sample reconstruction.
Returns the fitted surface values for every sample
and variable: the point on the surface at which sample sits, written
back in the coordinates of the original variables. Row is the
surface's reconstruction of sample – what the fit says the sample
would be if it lay exactly on the surface – and the residual
is the part of the sample the surface does not
capture, which is what predictivity summarises.
## S3 method for class 'prinsurf' fitted(object, ...)## S3 method for class 'prinsurf' fitted(object, ...)
object |
A |
... |
Ignored. |
Values are in the working units used for fitting: centred, and divided by
each variable's standard deviation if the surface was fitted with
scale = TRUE. This is the scale on which residuals and
predictivity are computed. predict.prinsurf
differs in two ways: it reads values off the plotted contour grid rather than
evaluating the coordinate functions exactly, and it returns them on the
variables' original scales.
An matrix of fitted values, in working
(centred, optionally scaled) units.
predictivity for the per-sample quality of this
reconstruction, and predict.prinsurf for the values a reader
obtains from the contours.
Draws the sample coordinates. If vars is given, draws one panel per
named variable, each showing the sample coordinates together with that
variable's contour lines - the surface's analogue of a biplot axis, read by
interpolating between contour lines rather than by projection onto a single
calibrated axis. With no vars, a single panel of sample coordinates is
drawn with no contours.
## S3 method for class 'prinsurf' plot( x, vars = NULL, group = NULL, col_contour = "grey40", nlevels = 6, pch = 16, cex = 0.7, asp = 1, main = NULL, outer_main = NULL, ... )## S3 method for class 'prinsurf' plot( x, vars = NULL, group = NULL, col_contour = "grey40", nlevels = 6, pch = 16, cex = 0.7, asp = 1, main = NULL, outer_main = NULL, ... )
x |
A |
vars |
Variables to contour, one panel each (default: none – a bare scatter of sample coordinates). |
group |
Optional factor to colour the sample points. |
col_contour |
Colour for the contour lines. |
nlevels |
Number of contour levels per panel. |
pch, cex
|
Point symbol and size for samples. |
asp |
Aspect ratio of each panel; the default |
main |
Panel title(s), replacing the default (the variable's name, or no
title when |
outer_main |
A single title for the figure as a whole, drawn in the
outer margin above the panels. Can be combined with |
... |
Passed to each panel's initial |
Invisibly, vars.
set.seed(1) s <- runif(120, -1, 1); t <- runif(120, -1, 1) X <- cbind(x = s, y = t, z = 0.7 * s + 0.9 * t^2) + matrix(rnorm(360, 0, 0.03), 120, 3) fit <- prinsurf(X, max.iter = 6) ## default: each panel is titled with its variable's name plot(fit, vars = c("x", "z")) ## your own panel titles, and a title for the figure as a whole plot(fit, vars = c("x", "z"), main = c("First coordinate", "Third coordinate"), outer_main = "Principal-surface biplot") ## a title on a single, contour-free panel plot(fit, main = "Sample coordinates")set.seed(1) s <- runif(120, -1, 1); t <- runif(120, -1, 1) X <- cbind(x = s, y = t, z = 0.7 * s + 0.9 * t^2) + matrix(rnorm(360, 0, 0.03), 120, 3) fit <- prinsurf(X, max.iter = 6) ## default: each panel is titled with its variable's name plot(fit, vars = c("x", "z")) ## your own panel titles, and a title for the figure as a whole plot(fit, vars = c("x", "z"), main = c("First coordinate", "Third coordinate"), outer_main = "Principal-surface biplot") ## a title on a single, contour-free panel plot(fit, main = "Sample coordinates")
Reads every variable's value for every sample off that variable's contour
lines at the sample's biplot position – the same
interpolation used to draw the contours in plot.prinsurf.
Values come from the contour grid alone: a sample whose position is not
covered by the supported part of the grid has no contours to read, and is
returned as NA for every variable.
## S3 method for class 'prinsurf' predict(object, ...)## S3 method for class 'prinsurf' predict(object, ...)
object |
A |
... |
Ignored. |
An matrix of values read from the contours, on the
variables' original scales, with NA rows where the biplot cannot be
read.
contour_predictive_error to measure this reading
against the variables' actual values, and fitted.prinsurf
for the surface's own reconstruction of the data.
For each sample, the proportion of its squared length reconstructed by the
fitted surface, . This is the principal-surface analogue of biplot sample
predictivity and is read from the sample's position on the surface.
predictivity(object)predictivity(object)
object |
A |
A numeric vector of per-sample predictivities (with the overall mean as
the attribute "overall").
Fits a two-dimensional principal surface to a numeric data matrix using the
iterative expectation / projection algorithm of Hastie and Stuetzle (1989),
with the coordinate functions estimated by local regression (loess).
prinsurf(X, max.iter = 10, span = 0.6, scale = FALSE, verbose = FALSE)prinsurf(X, max.iter = 10, span = 0.6, scale = FALSE, verbose = FALSE)
X |
A numeric matrix or data frame, |
max.iter |
Maximum number of expectation/projection iterations. |
span |
The |
scale |
Logical; if |
verbose |
Logical; print the relative change and residual sum of squares at each iteration. |
An object of class "prinsurf": a list with elements
lambda (the surface coordinates of the samples),
fj.mat (the fitted surface coordinates of the samples in the working
units), models (the per-variable loess coordinate functions),
varnames, center, scale, span and
iterations.
Hastie, T. and Stuetzle, W. (1989) Principal curves. Journal of the American Statistical Association 84, 502–516.
set.seed(1) s <- runif(120, -1, 1); t <- runif(120, -1, 1) X <- cbind(x = s, y = t, z = 0.7 * s + 0.9 * t^2) + matrix(rnorm(360, 0, 0.03), 120, 3) fit <- prinsurf(X, max.iter = 6) fitset.seed(1) s <- runif(120, -1, 1); t <- runif(120, -1, 1) X <- cbind(x = s, y = t, z = 0.7 * s + 0.9 * t^2) + matrix(rnorm(360, 0, 0.03), 120, 3) fit <- prinsurf(X, max.iter = 6) fit