Skip to content

Commit abbeec5

Browse files
committed
dirichlet prior over missing value class mixture weights #8
1 parent a586a93 commit abbeec5

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

R/msImpute.R

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
#' @export
6969
msImpute <- function(y, method=c("v2-mnar", "v2", "v1"),
7070
group = NULL,
71+
design = NULL,
7172
alpha = 0.2,
7273
relax_min_obs=TRUE,
7374
rank.max = NULL, lambda = NULL, thresh = 1e-05,
@@ -127,9 +128,10 @@ msImpute <- function(y, method=c("v2-mnar", "v2", "v1"),
127128
yimp <- msImputev1(y, rank.max = rank.max , lambda = estimateLambda(y, rank = rank.max)) #
128129
if (method == "v2-mnar"){
129130
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.")
131132
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)
133135

134136
}
135137

@@ -234,20 +236,30 @@ estimateLambda <- function(y, rank=NULL) mean(matrixStats::colSds(y, na.rm = TRU
234236

235237
#' @importFrom stats quantile
236238
#' @keywords internal
237-
l2bary <- function(y, ygauss, yerank, group, a=0.2){
239+
l2bary <- function(y, ygauss, yerank, group, design = NULL, a=0.2){
238240

239241
pepSds <- matrixStats::rowSds(y, na.rm = TRUE)
240242
pepMeans <- rowMeans(y, na.rm = TRUE)
241243
pepCVs <- pepSds/pepMeans
242244
CV_cutoff <- min(0.2, median(pepCVs))
243245
varq75 <- quantile(pepSds, p = 0.75, na.rm=TRUE)
244246
#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+
246253

247254
# if entropy is nan and variance is low, it is most likely detection limit missing
248255
# 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]
251263

252264
# yl2 <- list()
253265
# for(j in colnames(y)){

0 commit comments

Comments
 (0)