Skip to content

Commit f6dbb50

Browse files
committed
add relax_min_obs to remove min 4 obs requirement (issue #4), change a (the weight) param to alpha in msImpute.R
1 parent 217a06c commit f6dbb50

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

R/msImpute.R

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#' @param method character. Allowed values are \code{"v2"} for \code{msImputev2} imputation (enhanced version) for MAR.
3030
#' \code{method="v2-mnar"} (modified low-rank approx for MNAR), and \code{"v1"} initial release of \code{msImpute}
3131
#' @param group character or factor vector of length \code{ncol(y)}
32-
#' @param a numeric. the weight parameter. default to 0.2. Weights the MAR-imputed distribution in the imputation scheme.
32+
#' @param alpha numeric. The weight parameter. Default to 0.2. Weights the MAR-imputed distribution in the imputation scheme.
3333
#' @param rank.max Numeric. This restricts the rank of the solution. is set to min(dim(\code{y})-1) by default in "v1".
3434
#' @param lambda Numeric. Nuclear-norm regularization parameter. Controls the low-rank property of the solution
3535
#' to the matrix completion problem. By default, it is determined at the scaling step. If set to zero
@@ -56,7 +56,7 @@
5656
#' data(pxd010943)
5757
#' y <- log2(data.matrix(pxd010943))
5858
#' group <- gsub("_[1234]","", colnames(y))
59-
#' yimp <- msImpute(y, method="v2-mnar", group=group)
59+
#' yimp <- msImpute(y, method="v2-mnar", group=group, max.rank=2)
6060
#' @seealso selectFeatures
6161
#' @author Soroor Hediyeh-zadeh
6262
#' @references
@@ -67,7 +67,8 @@
6767
#' @export
6868
msImpute <- function(y, method=c("v2-mnar", "v2", "v1"),
6969
group = NULL,
70-
a = 0.2,
70+
alpha = 0.2,
71+
relax_min_obs=FALSE,
7172
rank.max = NULL, lambda = NULL, thresh = 1e-05,
7273
maxit = 100, trace.it = FALSE, warm.start = NULL,
7374
final.svd = TRUE, biScale_maxit=20, gauss_width = 0.3, gauss_shift = 1.8) {
@@ -76,7 +77,15 @@ msImpute <- function(y, method=c("v2-mnar", "v2", "v1"),
7677

7778

7879
if(any(is.nan(y) | is.infinite(y))) stop("Inf or NaN values encountered.")
79-
if(any(rowSums(!is.na(y)) <= 3)) stop("Peptides with excessive NAs are detected. Please revisit your fitering step. At least 4 non-missing measurements are required for any peptide.")
80+
critical_obs <- NULL
81+
if(relax_min_obs & any(rowSums(!is.na(y)) <= 3)) {
82+
83+
stop("Peptides with excessive NAs are detected. Please revisit your fitering step (at least 4 non-missing measurements are required for any peptide) or set relax_min_obs=TRUE.")}
84+
else{
85+
critical_obs <- which(rowSums(!is.na(y)) <= 3)
86+
message("Features with less than 4 non-missing measurements detected. These will be treated as MNAR.")
87+
}
88+
8089
if(any(y < 0, na.rm = TRUE)){
8190
warning("Negative values encountered in imputed data. Please consider revising filtering and/or normalisation steps.")
8291
}
@@ -86,6 +95,10 @@ msImpute <- function(y, method=c("v2-mnar", "v2", "v1"),
8695

8796
if(method=="v1"){
8897
message(paste("Running msImpute version", method))
98+
if (!is.null(critical_obs)) {
99+
y_critical_obs <- y[critical_obs,]
100+
y <- y[-critical_obs,]
101+
}
89102
yimp <- scaleData(y, maxit = biScale_maxit)
90103
yimp <- msImputev1(yimp,
91104
rank.max = rank.max, lambda = lambda, thresh = thresh,
@@ -102,15 +115,23 @@ msImpute <- function(y, method=c("v2-mnar", "v2", "v1"),
102115
message(paste("Compute barycenter of MAR and NMAR distributions", method))
103116
if (is.null(group)) stop("Please specify the 'group' argument. This is required for the 'v2-mnar' method.")
104117
ygauss <- gaussimpute(y, width = gauss_width, shift = gauss_shift)
105-
yimp <- l2bary(y=y, ygauss = ygauss, yerank = yimp, group = group, a=a)
118+
yimp <- l2bary(y=y, ygauss = ygauss, yerank = yimp, group = group, a=alpha)
106119

107120
}
108121

109122

110-
}
111123

124+
}
112125

113126
yimp[!is.na(y)] <- y[!is.na(y)]
127+
if (!is.null(critical_obs)){
128+
yimp_critical_obs <- gaussimpute(y_critical_obs, width = gauss_width, shift = gauss_shift)
129+
yimp_critical_obs[!is.na(y_critical_obs)] <- y_critical_obs[!is.na(y_critical_obs)]
130+
yimp <- rbind(yimp,yimp_critical_obs)
131+
}
132+
133+
134+
114135
return(yimp)
115136

116137

0 commit comments

Comments
 (0)