Merge branch 'master' of auder.net:valse
[valse.git] / pkg / 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{
07848d25 24 #TODO: parameter ncores (chaque tâche peut aussi demander du parallélisme...)
7064275b
BA
25 cl = parallel::makeCluster( parallel::detectCores() / 4 )
26 parallel::clusterExport(cl=cl,
27 varlist=c("phiInit","rhoInit","gamInit","mini","maxi","glambda","X","Y","seuil","tau"),
28 envir=environment())
29 #Pour chaque lambda de la grille, on calcule les coefficients
07848d25 30 out = parLapply( seq_along(glambda), function(lambdaindex)
09ab3c16 31 {
7064275b
BA
32 p = dim(phiInit)[1]
33 m = dim(phiInit)[2]
07848d25
BA
34
35 params = EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau)
36
7064275b
BA
37 #selectedVariables: list where element j contains vector of selected variables in [1,m]
38 selectedVariables = lapply(1:p, function(j) {
39 #from boolean matrix mxk of selected variables obtain the corresponding boolean m-vector,
40 #and finally return the corresponding indices
41 seq_len(m)[ apply( abs(params$phi[j,,]) > seuil, 1, any ) ]
42 })
09ab3c16 43
7064275b
BA
44 list("selected"=selectedVariables,"Rho"=params$Rho,"Pi"=params$Pi)
45 })
46 parallel::stopCluster(cl)
5955cc25 47 out
09ab3c16 48}