merge selectVariables.R doc and selectiontotale.R code into selectVariables.R
[valse.git] / R / selectVariables.R
1 #' selectVariables
2 #' It is a function which construct, for a given lambda, the sets of relevant variables.
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)
6 #' @param piInit an initial estimator for pi (size : k)
7 #' @param gamInit an initial estimator for gamma
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
11 #' @param glambda grid of regularization parameters
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
16 #'
17 #' @return a list of outputs, for each lambda in grid: selected,Rho,Pi
18 #'
19 #' @examples TODO
20 #'
21 #' @export
22 selectVariables = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau)
23 {
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)
30 {
31 params = EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau)
32
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 })
41
42 list("selected"=selectedVariables,"Rho"=params$Rho,"Pi"=params$Pi)
43 })
44 parallel::stopCluster(cl)
45 }