Remove arg n in plot_valse (deduce from X)
[valse.git] / pkg / R / selectVariables.R
CommitLineData
0ba1b11c 1#' selectVariables
3453829e 2#'
3921ba9b 3#' For a given lambda, construct the sets of relevant variables for each cluster.
3453829e
BA
4#'
5#' @param phiInit an initial estimator for phi (size: p*m*k)
6#' @param rhoInit an initial estimator for rho (size: m*m*k)
7#' @param piInit an initial estimator for pi (size : k)
8#' @param gamInit an initial estimator for gamma
9#' @param mini minimum number of iterations in EM algorithm
10#' @param maxi maximum number of iterations in EM algorithm
11#' @param gamma power in the penalty
12#' @param glambda grid of regularization parameters
13#' @param X matrix of regressors
14#' @param Y matrix of responses
15#' @param thresh real, threshold to say a variable is relevant, by default = 1e-8
16#' @param eps threshold to say that EM algorithm has converged
17#' @param ncores Number or cores for parallel execution (1 to disable)
1196a43d 18#' @param fast boolean to enable or not the C function call
3453829e
BA
19#'
20#' @return a list of outputs, for each lambda in grid: selected,Rho,Pi
21#'
3453829e 22#' @export
0ba1b11c 23selectVariables <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma,
3453829e
BA
24 glambda, X, Y, thresh = 1e-08, eps, ncores = 3, fast)
25{
26 if (ncores > 1) {
27 cl <- parallel::makeCluster(ncores, outfile = "")
0ba1b11c 28 parallel::clusterExport(cl = cl, varlist = c("phiInit", "rhoInit", "gamInit",
3453829e
BA
29 "mini", "maxi", "glambda", "X", "Y", "thresh", "eps"), envir = environment())
30 }
31
32 # Computation for a fixed lambda
33 computeCoefs <- function(lambda)
34 {
0ba1b11c 35 params <- EMGLLF(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
3453829e
BA
36 X, Y, eps, fast)
37
38 p <- ncol(X)
39 m <- ncol(Y)
40
41 # selectedVariables: list where element j contains vector of selected variables
42 # in [1,m]
43 selectedVariables <- lapply(1:p, function(j) {
44 # from boolean matrix mxk of selected variables obtain the corresponding boolean
45 # m-vector, and finally return the corresponding indices
46 if (m>1) {
47 seq_len(m)[apply(abs(params$phi[j, , ]) > thresh, 1, any)]
48 } else {
49 if (any(params$phi[j, , ] > thresh))
50 1
51 else
52 numeric(0)
53 }
54 })
55
56 list(selected = selectedVariables, Rho = params$rho, Pi = params$pi)
57 }
58
59 # For each lambda in the grid, we compute the coefficients
60 out <-
61 if (ncores > 1) {
62 parLapply(cl, glambda, computeCoefs)
63 } else {
64 lapply(glambda, computeCoefs)
65 }
0ba1b11c 66 if (ncores > 1)
3453829e 67 parallel::stopCluster(cl)
0ba1b11c 68
3921ba9b
BA
69 print(out) #DEBUG TRACE
70 # Suppress models which are computed twice
3453829e
BA
71 # sha1_array <- lapply(out, digest::sha1) out[ duplicated(sha1_array) ]
72 selec <- lapply(out, function(model) model$selected)
73 ind_dup <- duplicated(selec)
74 ind_uniq <- which(!ind_dup)
75 out2 <- list()
76 for (l in 1:length(ind_uniq))
77 out2[[l]] <- out[[ind_uniq[l]]]
78 out2
79}