fix for m==1
[valse.git] / pkg / R / selectVariables.R
CommitLineData
ffdf9447 1#' selectVariables
bb551124 2#'
7064275b 3#' It is a function which construct, for a given lambda, the sets of relevant variables.
e01c9b1f 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)
ffdf9447 7#' @param piInit\tan initial estimator for pi (size : k)
e01c9b1f 8#' @param gamInit an initial estimator for gamma
ffdf9447
BA
9#' @param mini\t\tminimum number of iterations in EM algorithm
10#' @param maxi\t\tmaximum number of iterations in EM algorithm
11#' @param gamma\t power in the penalty
e01c9b1f 12#' @param glambda grid of regularization parameters
ffdf9447
BA
13#' @param X\t\t\t matrix of regressors
14#' @param Y\t\t\t matrix of responses
43d76c49 15#' @param thresh real, threshold to say a variable is relevant, by default = 1e-8
ffdf9447 16#' @param eps\t\t threshold to say that EM algorithm has converged
4cc632c9 17#' @param ncores Number or cores for parallel execution (1 to disable)
e01c9b1f 18#'
7064275b 19#' @return a list of outputs, for each lambda in grid: selected,Rho,Pi
cad71b2c
BA
20#'
21#' @examples TODO
e01c9b1f 22#'
cad71b2c 23#' @export
bb551124 24#'
ffdf9447 25selectVariables <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma,
a3cbbaea 26 glambda, X, Y, thresh = 1e-08, eps, ncores = 3, fast)
1b698c16
BA
27{
28 if (ncores > 1) {
ffdf9447
BA
29 cl <- parallel::makeCluster(ncores, outfile = "")
30 parallel::clusterExport(cl = cl, varlist = c("phiInit", "rhoInit", "gamInit",
31 "mini", "maxi", "glambda", "X", "Y", "thresh", "eps"), envir = environment())
fb6e49cb 32 }
1b698c16 33
fb6e49cb 34 # Computation for a fixed lambda
35 computeCoefs <- function(lambda)
36 {
ffdf9447
BA
37 params <- EMGLLF(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
38 X, Y, eps, fast)
1b698c16 39
ea5860f1
BA
40 p <- ncol(X)
41 m <- ncol(Y)
1b698c16 42
ffdf9447
BA
43 # selectedVariables: list where element j contains vector of selected variables
44 # in [1,m]
1b698c16 45 selectedVariables <- lapply(1:p, function(j) {
ffdf9447
BA
46 # from boolean matrix mxk of selected variables obtain the corresponding boolean
47 # m-vector, and finally return the corresponding indices
ea5860f1
BA
48 if (m>1) {
49 seq_len(m)[apply(abs(params$phi[j, , ]) > thresh, 1, any)]
50 } else {
51 if (any(params$phi[j, , ] > thresh))
52 1
53 else
54 numeric(0)
55 }
fb6e49cb 56 })
1b698c16 57
ffdf9447 58 list(selected = selectedVariables, Rho = params$rho, Pi = params$pi)
fb6e49cb 59 }
1b698c16 60
fb6e49cb 61 # For each lambda in the grid, we compute the coefficients
a3cbbaea
BA
62 out <-
63 if (ncores > 1) {
64 parLapply(cl, glambda, computeCoefs)
65 } else {
66 lapply(glambda, computeCoefs)
67 }
ffdf9447 68 if (ncores > 1)
fb6e49cb 69 parallel::stopCluster(cl)
ffdf9447
BA
70 # Suppress models which are computed twice En fait, ca ca fait la comparaison de
71 # tous les parametres On veut juste supprimer ceux qui ont les memes variables
a3cbbaea
BA
72 # sélectionnées
73 # sha1_array <- lapply(out, digest::sha1) out[ duplicated(sha1_array) ]
ffdf9447
BA
74 selec <- lapply(out, function(model) model$selected)
75 ind_dup <- duplicated(selec)
76 ind_uniq <- which(!ind_dup)
77 out2 <- list()
78 for (l in 1:length(ind_uniq))
ffdf9447 79 out2[[l]] <- out[[ind_uniq[l]]]
fb6e49cb 80 out2
09ab3c16 81}