prepare wrappers EMGLLF.R --> EMGLLF.c
[valse.git] / pkg / R / selectiontotale.R
CommitLineData
e54d1bb9 1#Return a list of outputs, for each lambda in grid: selected,Rho,Pi
12381c92 2selectiontotale = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,thresh,tau, parallel = FALSE){
3 if (parallel) {
4 require(parallel)
5 cl = parallel::makeCluster( parallel::detectCores() / 4) # <-- ça devrait être un argument
e54d1bb9 6 parallel::clusterExport(cl=cl,
7 varlist=c("phiInit","rhoInit","gamInit","mini","maxi","glambda","X","Y","thresh","tau"),
8 envir=environment())
9 #Pour chaque lambda de la grille, on calcule les coefficients
12381c92 10 out = parLapply(cl, 1:length(glambda), function(lambdaIndex)
e54d1bb9 11 {
12 params =
13 EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau)
14
15 p = dim(phiInit)[1]
16 m = dim(phiInit)[2]
17 #selectedVariables: list where element j contains vector of selected variables in [1,m]
18 selectedVariables = lapply(1:p, function(j) {
19 #from boolean matrix mxk of selected variables obtain the corresponding boolean m-vector,
20 #and finally return the corresponding indices
21 seq_len(m)[ apply( abs(params$phi[j,,]) > thresh, 1, any ) ]
22 })
23
24 list("selected"=selectedVariables,"Rho"=params$Rho,"Pi"=params$Pi)
25 })
26 parallel::stopCluster(cl)
12381c92 27 }
28 else {
29 selectedVariables = list()
30 Rho = list()
31 Pi = list()
31444abc 32 cpt = 1
12381c92 33 #Pour chaque lambda de la grille, on calcule les coefficients
34 for (lambdaIndex in 1:length(glambda)){
64b28e3e 35 print(lambdaIndex)
12381c92 36 params =
37 EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau)
38 p = dim(phiInit)[1]
39 m = dim(phiInit)[2]
40 #selectedVariables: list where element j contains vector of selected variables in [1,m]
51485a7d 41 if (sum(params$phi) != 0){
31444abc 42 selectedVariables[[cpt]] = sapply(1:p, function(j) {
43 #from boolean matrix mxk of selected variables obtain the corresponding boolean m-vector,
44 #and finally return the corresponding indices
45 c(seq_len(m)[ apply( abs(params$phi[j,,]) > thresh, 1, any ) ], rep(0, m-length(seq_len(m)[ apply( abs(params$phi[j,,]) > thresh, 1, any ) ] ) ))
46 })
47 if (length(unique(selectedVariables)) == length(selectedVariables)){
48 Rho[[cpt]] = params$rho
49 Pi[[cpt]] = params$pi
50 cpt = cpt+1
51 }
51485a7d 52 }
12381c92 53 }
54 list("selected"=selectedVariables,"Rho"=Rho,"Pi"=Pi)
55 }
56}