Commit | Line | Data |
---|---|---|
7064275b | 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) | |
09ab3c16 | 7 | #' @param piInit an initial estimator for pi (size : k) |
e01c9b1f | 8 | #' @param gamInit an initial estimator for gamma |
09ab3c16 BA |
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 | |
e01c9b1f | 12 | #' @param glambda grid of regularization parameters |
09ab3c16 BA |
13 | #' @param X matrix of regressors |
14 | #' @param Y matrix of responses | |
15 | #' @param thres threshold to consider a coefficient to be equal to 0 | |
16 | #' @param tau threshold to say that EM algorithm has converged | |
e01c9b1f | 17 | #' |
7064275b | 18 | #' @return a list of outputs, for each lambda in grid: selected,Rho,Pi |
cad71b2c BA |
19 | #' |
20 | #' @examples TODO | |
e01c9b1f | 21 | #' |
cad71b2c | 22 | #' @export |
bb551124 BA |
23 | #' |
24 | selectVariables = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda, | |
25 | X,Y,thresh,tau, ncores=1) #ncores==1 ==> no // | |
09ab3c16 | 26 | { |
bb551124 BA |
27 | if (ncores > 1) |
28 | { | |
29 | cl = parallel::makeCluster(ncores) | |
30 | parallel::clusterExport(cl=cl, | |
31 | varlist=c("phiInit","rhoInit","gamInit","mini","maxi","glambda","X","Y","thresh","tau"), | |
32 | envir=environment()) | |
33 | } | |
34 | ||
35 | # Calcul pour un lambda | |
36 | computeCoefs <-function(lambda) | |
09ab3c16 | 37 | { |
bb551124 BA |
38 | params = EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,tau) |
39 | ||
7064275b BA |
40 | p = dim(phiInit)[1] |
41 | m = dim(phiInit)[2] | |
07848d25 | 42 | |
7064275b | 43 | #selectedVariables: list where element j contains vector of selected variables in [1,m] |
51485a7d | 44 | selectedVariables = sapply(1:p, function(j) { ## je me suis permise de changer le type, |
45 | ##une liste de liste ca devenait compliqué je trouve pour choper ce qui nous intéresse | |
7064275b BA |
46 | #from boolean matrix mxk of selected variables obtain the corresponding boolean m-vector, |
47 | #and finally return the corresponding indices | |
51485a7d | 48 | #seq_len(m)[ apply( abs(params$phi[j,,]) > thresh, 1, any ) ] |
49 | c(seq_len(m)[ apply( abs(params$phi[j,,]) > thresh, 1, any ) ], | |
50 | rep(0, m-length(seq_len(m)[ apply( abs(params$phi[j,,]) > thresh, 1, any ) ] ) )) | |
7064275b | 51 | }) |
09ab3c16 | 52 | |
51485a7d | 53 | list("selected"=selectedVariables,"Rho"=params$rho,"Pi"=params$pi) |
bb551124 BA |
54 | } |
55 | ||
56 | # Pour chaque lambda de la grille, on calcule les coefficients | |
57 | out <- | |
51485a7d | 58 | if (ncores > 1){ |
59 | parLapply(cl, seq_along(glambda, computeCoefs))} | |
60 | else lapply(seq_along(glambda), computeCoefs) | |
61 | if (ncores > 1){ | |
62 | parallel::stopCluster(cl)} | |
5955cc25 | 63 | out |
09ab3c16 | 64 | } |