Skip to content

Commit

Permalink
Support sparse matrix mode
Browse files Browse the repository at this point in the history
  • Loading branch information
statwangz committed Sep 15, 2023
1 parent 30d27ec commit c6d95a1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
24 changes: 21 additions & 3 deletions R/mfairInitialization.R
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,33 @@ createMFAIR <- function(Y, X, Y_center = TRUE, K_max = 1L, project = "MFAIR") {
stop("The number of samples in Y and X should be consistent!")
} # End

if(is.matrix(Y)){
Y_sparse <- FALSE
}else{
Y_sparse <- TRUE
}

# Center the matrix Y
if (Y_center) {
Y_mean <- mean(Y, na.rm = TRUE)
Y <- Y - Y_mean
if(!Y_sparse){
Y_mean <- mean(Y, na.rm = TRUE)
Y <- Y - Y_mean
}
else{
Y_mean <- mean(summary(Y)$x)
Y@x <- Y@x - Y_mean
}
} else {
Y_mean <- 0
}

# Check Y's sparsity
n_missing <- sum(is.na(Y))
if(!Y_sparse){
n_missing <- sum(is.na(Y))
}
else{
n_missing <- length(Y) - length(Y@x)
}
if (n_missing >= 1) {
if (n_missing == length(Y)) {
stop("The main data matrix Y has no observed values!")
Expand All @@ -52,6 +69,7 @@ createMFAIR <- function(Y, X, Y_center = TRUE, K_max = 1L, project = "MFAIR") {
Class = "MFAIR",
Y = Y,
X = as.data.frame(X),
Y_sparse = Y_sparse,
Y_center = Y_center,
Y_mean = Y_mean,
Y_missing = Y_missing,
Expand Down
6 changes: 4 additions & 2 deletions R/mfairObject.R
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
#' Each MFAIR object has a number of slots which store information. Key slots to access are listed below.
#'
#'
#' @slot Y A matrix. The main data matrix of N samples and M features.
#' @slot Y A matrix or Matrix::dgCMatrix. The main data matrix of N samples and M features.
#' @slot X A data.frame. The auxiliary information data frame of N samples and C covariates.
#' @slot Y_sparse Logical. Whether the main data matrix Y is stored in the sparse mode.
#' @slot Y_center Logical. Whether the main data matrix Y is centered.
#' @slot Y_mean Numeric. Mean of the main data matrix Y if centered. Zero if not.
#' @slot Y_missing Logical. Whether the main data matrix Y is partially observed.
Expand Down Expand Up @@ -34,8 +35,9 @@ setClass(

# Define the slots
slots = c(
Y = "matrix",
Y = "matrixORdgCMatrix",
X = "data.frame",
Y_sparse = "logical",
Y_center = "logical",
Y_mean = "numeric",
Y_missing = "logical",
Expand Down

0 comments on commit c6d95a1

Please sign in to comment.