add a few TODOs in constructionModelesEMGLLF.R
[valse.git] / R / selectVariables.R
CommitLineData
7064275b
BA
1#' selectVariables
2#' It is a function which construct, for a given lambda, the sets of relevant variables.
e01c9b1f 3#'
4#' @param phiInit an initial estimator for phi (size: p*m*k)
5#' @param rhoInit an initial estimator for rho (size: m*m*k)
09ab3c16 6#' @param piInit an initial estimator for pi (size : k)
e01c9b1f 7#' @param gamInit an initial estimator for gamma
09ab3c16
BA
8#' @param mini minimum number of iterations in EM algorithm
9#' @param maxi maximum number of iterations in EM algorithm
10#' @param gamma power in the penalty
e01c9b1f 11#' @param glambda grid of regularization parameters
09ab3c16
BA
12#' @param X matrix of regressors
13#' @param Y matrix of responses
14#' @param thres threshold to consider a coefficient to be equal to 0
15#' @param tau threshold to say that EM algorithm has converged
e01c9b1f 16#'
7064275b 17#' @return a list of outputs, for each lambda in grid: selected,Rho,Pi
cad71b2c
BA
18#'
19#' @examples TODO
e01c9b1f 20#'
cad71b2c 21#' @export
7064275b 22selectVariables = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau)
09ab3c16 23{
7064275b
BA
24 cl = parallel::makeCluster( parallel::detectCores() / 4 )
25 parallel::clusterExport(cl=cl,
26 varlist=c("phiInit","rhoInit","gamInit","mini","maxi","glambda","X","Y","seuil","tau"),
27 envir=environment())
28 #Pour chaque lambda de la grille, on calcule les coefficients
29 out = parLapply( 1:L, function(lambdaindex)
09ab3c16 30 {
7064275b 31 params = EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau)
09ab3c16 32
7064275b
BA
33 p = dim(phiInit)[1]
34 m = dim(phiInit)[2]
35 #selectedVariables: list where element j contains vector of selected variables in [1,m]
36 selectedVariables = lapply(1:p, function(j) {
37 #from boolean matrix mxk of selected variables obtain the corresponding boolean m-vector,
38 #and finally return the corresponding indices
39 seq_len(m)[ apply( abs(params$phi[j,,]) > seuil, 1, any ) ]
40 })
09ab3c16 41
7064275b
BA
42 list("selected"=selectedVariables,"Rho"=params$Rho,"Pi"=params$Pi)
43 })
44 parallel::stopCluster(cl)
5955cc25 45 out
09ab3c16 46}