Comment each function in R
[valse.git] / pkg / R / selectVariables.R
CommitLineData
0ba1b11c 1#' selectVariables
3453829e 2#'
e9db7970 3#' It is a function which constructs, for a given lambda, the sets for each cluster of relevant variables.
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)
18#'
19#' @return a list of outputs, for each lambda in grid: selected,Rho,Pi
20#'
21#' @examples TODO
22#'
23#' @export
24#'
0ba1b11c 25selectVariables <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma,
3453829e
BA
26 glambda, X, Y, thresh = 1e-08, eps, ncores = 3, fast)
27{
28 if (ncores > 1) {
29 cl <- parallel::makeCluster(ncores, outfile = "")
0ba1b11c 30 parallel::clusterExport(cl = cl, varlist = c("phiInit", "rhoInit", "gamInit",
3453829e
BA
31 "mini", "maxi", "glambda", "X", "Y", "thresh", "eps"), envir = environment())
32 }
33
34 # Computation for a fixed lambda
35 computeCoefs <- function(lambda)
36 {
0ba1b11c 37 params <- EMGLLF(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
3453829e
BA
38 X, Y, eps, fast)
39
40 p <- ncol(X)
41 m <- ncol(Y)
42
43 # selectedVariables: list where element j contains vector of selected variables
44 # in [1,m]
45 selectedVariables <- lapply(1:p, function(j) {
46 # from boolean matrix mxk of selected variables obtain the corresponding boolean
47 # m-vector, and finally return the corresponding indices
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 }
56 })
57
58 list(selected = selectedVariables, Rho = params$rho, Pi = params$pi)
59 }
60
61 # For each lambda in the grid, we compute the coefficients
62 out <-
63 if (ncores > 1) {
64 parLapply(cl, glambda, computeCoefs)
65 } else {
66 lapply(glambda, computeCoefs)
67 }
0ba1b11c 68 if (ncores > 1)
3453829e 69 parallel::stopCluster(cl)
0ba1b11c 70
3453829e
BA
71 print(out)
72 # Suppress models which are computed twice En fait, ca ca fait la comparaison de
73 # tous les parametres On veut juste supprimer ceux qui ont les memes variables
74 # sélectionnées
75 # sha1_array <- lapply(out, digest::sha1) out[ duplicated(sha1_array) ]
76 selec <- lapply(out, function(model) model$selected)
77 ind_dup <- duplicated(selec)
78 ind_uniq <- which(!ind_dup)
79 out2 <- list()
80 for (l in 1:length(ind_uniq))
81 out2[[l]] <- out[[ind_uniq[l]]]
82 out2
83}