| predict.spca | R Documentation |
Apply a fitted sparse PCA transformation to new observations using the stored training means, scales, and loading coefficients.
## S3 method for class 'spca'
predict(object, newdata, ...)
## S3 method for class 'rspca'
predict(object, newdata, ...)
## S3 method for class 'robspca'
predict(object, newdata, ...)
object |
A fitted object returned by |
newdata |
A real numeric matrix, numeric data frame, or real Matrix object with the same variables as the training data. Missing and nonfinite values are not allowed. If both training and new data have column names, names must be unique and identify the exact same variable set; columns are reordered to training order. Otherwise supply columns in training order. If omitted, return the stored training scores. |
... |
Additional arguments; currently ignored. |
Prediction first applies the stored column means and scale factors,
skipping operations stored as FALSE, and then multiplies by
object$loadings. It does not estimate new preprocessing
parameters. Centering sparse input can be applied as a correction to
the score product; the returned score matrix is dense.
Predictions for robust fits use this same linear score map. The
method does not infer new outliers or subtract the training
sparse matrix from new observations. The stored training scores
and new scores are in component coordinates, not original variable
coordinates. Multiplication by t(object$transform) reconstructs
the low-rank part in processed coordinates; undo column scaling and
centering to restore original units.
A dense numeric matrix with one row per new observation and
one column per fitted component, including 1 by k
and 0 by k matrices for single-row and empty batches.
Available new-data row names are preserved. With no newdata,
returns object$scores, including its retained training-row order.
Fit preprocessing and loading coefficients on the training observations.
Then supply future observations in the same measurement units to
predict. Re-centering each new batch by its own mean changes
the coordinate system and removes shifts relative to the training
population; that is not the transformation fitted by the model.
For validation, keep the training and validation data separate when estimating means, scales, components, and tuning parameters. New rows need all fitted variables, even when some variables have zero loadings; the public input contract preserves the complete training variable set. An outcome or identifier column should be removed before prediction.
Named variables are matched exactly, including their spelling. With
unnamed input, the caller is responsible for the column order. One-row
and zero-row batches are supported and retain matrix dimensions; use
drop = FALSE when subsetting a matrix to a single row. Missing
new observations are rejected rather than silently dropped, so output
rows correspond one for one to the supplied batch. Missing training
rows, by contrast, were omitted during fitting.
spca, rspca,
robspca.
# Hold out every fifth row for this small illustration.
training <- seq_len(nrow(iris)) %% 5 != 0
fit <- spca(iris[training, 1:4], k = 2, alpha = 0.01,
scale = TRUE, verbose = FALSE)
newX <- as.matrix(iris[!training, 1:4])
scores <- predict(fit, newX)
head(scores)
# Reproduce the map with the training means, scales, and coefficients.
processed <- scale(newX, center = fit$center, scale = fit$scale)
all.equal(scores, processed %*% fit$loadings)
# A complete named variable set can be supplied in a different order.
reordered <- predict(fit, newX[, 4:1])
all.equal(scores, reordered)
dim(predict(fit, newX[1, , drop = FALSE]))
dim(predict(fit, newX[FALSE, , drop = FALSE]))
identical(predict(fit), fit$scores)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.