From 7c929d3686db189f0babff402c16aee8ca5e7e90 Mon Sep 17 00:00:00 2001 From: "Ryan C. Thompson" Date: Thu, 5 Dec 2024 19:19:02 -0500 Subject: [PATCH] Make fast.prcomp return a valid prcomp object Functions like predict.prcomp expect a prcomp object to have "center" and "scale" attributes. --- R/fast.prcomp.R | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/R/fast.prcomp.R b/R/fast.prcomp.R index e46b8ab..3bdbc13 100644 --- a/R/fast.prcomp.R +++ b/R/fast.prcomp.R @@ -65,7 +65,7 @@ #' # prcomp directly on matrix is SLOW: #' system.time( pr.x <- prcomp(x) ) #' -#' # prcomp.fast is much faster +#' # fast.prcomp is much faster #' system.time( fast.pr.x <- fast.prcomp(x) ) #' #' # and the results are equivalent @@ -82,6 +82,8 @@ fast.prcomp <- function (x, retx = TRUE, center = TRUE, scale. = FALSE, { x <- as.matrix(x) x <- scale(x, center = center, scale = scale.) + cen <- attr(x, "scaled:center") + sc <- attr(x, "scaled:scale") s <- La.svd(x, nu = 0) if (!is.null(tol)) { rank <- sum(s$d > (s$d[1] * tol)) @@ -92,7 +94,8 @@ fast.prcomp <- function (x, retx = TRUE, center = TRUE, scale. = FALSE, dimnames(s$vt) <- list(paste("PC", seq(len = nrow(s$vt)), sep = ""), colnames(x) ) - r <- list(sdev = s$d, rotation = t(s$vt) ) + r <- list(sdev = s$d, rotation = t(s$vt), + center = cen %||% FALSE, scale = sc %||% FALSE) if (retx) r$x <- x %*% t(s$vt) class(r) <- "prcomp"