|
68 | 68 | #' @export |
69 | 69 | msImpute <- function(y, method=c("v2-mnar", "v2", "v1"), |
70 | 70 | group = NULL, |
| 71 | + design = NULL, |
71 | 72 | alpha = 0.2, |
72 | 73 | relax_min_obs=TRUE, |
73 | 74 | rank.max = NULL, lambda = NULL, thresh = 1e-05, |
@@ -127,9 +128,10 @@ msImpute <- function(y, method=c("v2-mnar", "v2", "v1"), |
127 | 128 | yimp <- msImputev1(y, rank.max = rank.max , lambda = estimateLambda(y, rank = rank.max)) # |
128 | 129 | if (method == "v2-mnar"){ |
129 | 130 | message(paste("Compute barycenter of MAR and NMAR distributions", method)) |
130 | | - if (is.null(group)) stop("Please specify the 'group' argument. This is required for the 'v2-mnar' method.") |
| 131 | + if (is.null(group) & is.null(design)) stop("Please specify the 'group' argument. This is required for the 'v2-mnar' method.") |
131 | 132 | ygauss <- gaussimpute(y, width = gauss_width, shift = gauss_shift) |
132 | | - yimp <- l2bary(y=y, ygauss = ygauss, yerank = yimp, group = group, a=alpha) |
| 133 | + # yimp <- l2bary(y=y, ygauss = ygauss, yerank = yimp, group = group, a=alpha) |
| 134 | + yimp <- l2bary(y=y, ygauss = ygauss, yerank = yimp, design = design, a=alpha) |
133 | 135 |
|
134 | 136 | } |
135 | 137 |
|
@@ -234,20 +236,30 @@ estimateLambda <- function(y, rank=NULL) mean(matrixStats::colSds(y, na.rm = TRU |
234 | 236 |
|
235 | 237 | #' @importFrom stats quantile |
236 | 238 | #' @keywords internal |
237 | | -l2bary <- function(y, ygauss, yerank, group, a=0.2){ |
| 239 | +l2bary <- function(y, ygauss, yerank, group, design = NULL, a=0.2){ |
238 | 240 |
|
239 | 241 | pepSds <- matrixStats::rowSds(y, na.rm = TRUE) |
240 | 242 | pepMeans <- rowMeans(y, na.rm = TRUE) |
241 | 243 | pepCVs <- pepSds/pepMeans |
242 | 244 | CV_cutoff <- min(0.2, median(pepCVs)) |
243 | 245 | varq75 <- quantile(pepSds, p = 0.75, na.rm=TRUE) |
244 | 246 | #varq75 <- mean(pepVars) |
245 | | - EBM <- ebm(y, group) |
| 247 | + # EBM <- ebm(y, group) |
| 248 | + mv_design <- apply(design, 2, FUN=function(x) ebm(y, as.factor(x))) |
| 249 | + dirich_alpha_1 <- rowSums(!is.nan(mv_design)) |
| 250 | + dirich_alpha_2 <- ncol(mv_design) - dirich_alpha_1 |
| 251 | + dirich_alpha <- cbind(dirich_alpha_1, dirich_alpha_2) |
| 252 | + |
246 | 253 |
|
247 | 254 | # if entropy is nan and variance is low, it is most likely detection limit missing |
248 | 255 | # w1 <- ifelse(is.nan(EBM) & (pepCVs < CV_cutoff), 1-a, a) |
249 | | - w1 <- ifelse(is.nan(EBM), 1-a, a) |
250 | | - w2 <- 1-w1 |
| 256 | + # w1 <- ifelse(is.nan(EBM), 1-a, a) |
| 257 | + # w2 <- 1-w1 |
| 258 | + |
| 259 | + w <- apply(dirich_alpha, 1, FUN= function(alpha) LaplacesDemon::rdirichlet(1, alpha)) |
| 260 | + w <- t(w) |
| 261 | + w1 <- w[,2] |
| 262 | + w2 <- w[,1] |
251 | 263 |
|
252 | 264 | # yl2 <- list() |
253 | 265 | # for(j in colnames(y)){ |
|
0 commit comments