Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP irlba method for sparse matrices #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ Depends:
Imports:
BiocGenerics,
Rcpp (>= 0.11.3),
MASS
MASS,
irlba
Suggests:
matrixStats,
lattice,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ import(Biobase)
import(BiocGenerics)
import(methods)
importFrom(Rcpp,evalCpp)
importFrom(irlba,prcomp_irlba)
useDynLib(pcaMethods)
52 changes: 46 additions & 6 deletions R/pca.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
listPcaMethods <- function(which=c("all", "linear", "nonlinear")) {
switch(match.arg(which),
all={
return(c("svd", "nipals", "rnipals", "bpca", "ppca",
return(c("svd", "irlba", "nipals", "rnipals", "bpca", "ppca",
"svdImpute", "robustPca", "nlpca", "llsImpute",
"llsImputeAll"))
},
linear={
return(c("svd", "nipals", "rnipals", "bpca", "ppca",
return(c("svd", "irlba", "nipals", "rnipals", "bpca", "ppca",
"svdImpute", "robustPca"))
},
nonlinear={
Expand Down Expand Up @@ -123,11 +123,13 @@ pca <- function(object, method, nPcs=2,
}
else if(inherits(object, "ExpressionSet")) {
Matrix <- t(exprs(object))
} else
Matrix <- as.matrix(object, rownames.force=TRUE)
} else if (inherits(object, "sparseMatrix"))
Matrix <- object
else
Matrix <- as.matrix(object, rownames.force=TRUE)

if(!is.null(subset))
Matrix <- Matrix[,subset]
Matrix <- Matrix[, subset, drop = FALSE]

cv <- match.arg(cv)
scale <- match.arg(scale)
Expand All @@ -150,6 +152,8 @@ pca <- function(object, method, nPcs=2,
if(missing(method)) {
if(any(missing))
method <- 'nipals'
else if(inherits(Matrix, 'sparseMatrix'))
method <- 'irlba'
else
method <- 'svd'
}
Expand All @@ -165,6 +169,15 @@ pca <- function(object, method, nPcs=2,
svd={
res <- svdPca(prepres$data, nPcs=nPcs,...)
},
irlba=if(inherits(prepres$data, "sparseMatrix")) {
# The matrix was not actually preprocessed, we just pass the arguments on
res <- irlbaPca(prepres$data, nPcs=nPcs,
scale=prepres$scale == "uv",
center=prepres$center,
...)
} else {
res <- irlbaPca(prepres$data, nPcs=nPcs, ...)
},
nipals={
res <- nipalsPca(prepres$data, nPcs=nPcs, ...)
},
Expand Down Expand Up @@ -350,7 +363,8 @@ plotPcs <- function(object,
##' @param verbose Verbose complaints to matrix structure
##' @param ... Only used for passing through arguments.
##' @return A \code{pcaRes} object.
##' @seealso \code{prcomp}, \code{princomp}, \code{pca}
##' @seealso \code{\link[stats]{prcomp}}, \code{\link[stats]{princomp}},
##' \code{\link{pca}}
##' @examples
##' data(metaboliteDataComplete)
##' mat <- prep(t(metaboliteDataComplete))
Expand Down Expand Up @@ -378,6 +392,32 @@ svdPca <- function(Matrix, nPcs=2,
return(res)
}


##' @importFrom irlba prcomp_irlba
irlbaPca <- function(
Matrix,
nPcs = 2,
varLimit = 1,
verbose = interactive(),
center = TRUE,
scale. = FALSE,
...
) {
pcs <- prcomp_irlba(Matrix, center = center, scale. = scale)
imp <- summary(pcs)$importance
if (varLimit < 1)
nPcs <- sum(imp[3, ] < varLimit) + 1

res <- new("pcaRes")
res@scores <- cbind(pcs$x[, seq_len(nPcs)])
res@loadings <- cbind(pcs$rotation[, seq_len(nPcs)])
res@R2cum <- imp[3, seq_len(nPcs)]
res@varLimit <- varLimit
res@method <- "irlba"
res
}


##' Get a confidence ellipse for uncorrelated bivariate data
##'
##' As described in 'Introduction to multi and megavariate data analysis
Expand Down
7 changes: 6 additions & 1 deletion R/prep.R
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ prep <- function(object, scale=c("none", "pareto", "vector", "uv"),
center=TRUE, eps=1e-12, simple=TRUE, reverse=FALSE, ...) {
if(inherits(object, "ExpressionSet"))
obj <- t(exprs(object))
else
else if(!inherits(object, 'sparseMatrix'))
obj <- as.matrix(object)
else if (!is.null(scale) && !(scale %in% c("none", "uv"))) {
warning("Sparse matrices only support the 'uv' scaling. Converting to a dense matrix")
obj <- as.matrix(object)
} else # let irlba handle sparse matrices for now
return(list(data = object, center = center, scale = scale))

if(is.null(center))
center <- FALSE
Expand Down
3 changes: 2 additions & 1 deletion man/svdPca.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.