From 928d1c52c786ec94be64c4bc8123ab8a98727b6e Mon Sep 17 00:00:00 2001 From: Benjamin Auder <benjamin.auder@somewhere> Date: Thu, 23 Feb 2017 19:42:18 +0100 Subject: [PATCH] draft of selectiontotale.R --- R/selectiontotale.R | 25 +++++++++++++++++++++ R/selectiontotale.m | 54 --------------------------------------------- 2 files changed, 25 insertions(+), 54 deletions(-) create mode 100644 R/selectiontotale.R delete mode 100644 R/selectiontotale.m diff --git a/R/selectiontotale.R b/R/selectiontotale.R new file mode 100644 index 0000000..673bc3b --- /dev/null +++ b/R/selectiontotale.R @@ -0,0 +1,25 @@ +#Return a list of outputs, for each lambda in grid: selected,Rho,Pi +selectiontotale = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau) +{ + cl = parallel::makeCluster( parallel::detectCores() / 4 ) + parallel::clusterExport(cl=cl, + varlist=c("phiInit","rhoInit","gamInit","mini","maxi","glambda","X","Y","seuil","tau"), + envir=environment()) + #Pour chaque lambda de la grille, on calcule les coefficients + out = parLapply( 1:L, function(lambdaindex) + { + params = EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda[lambdaIndex],X,Y,tau) + + p = dim(phiInit)[1] + m = dim(phiInit)[2] + #selectedVariables: list where element j contains vector of selected variables in [1,m] + selectedVariables = lapply(1:p, function(j) { + #from boolean matrix mxk of selected variables obtain the corresponding boolean m-vector, + #and finally return the corresponding indices + seq_len(m)[ apply( abs(params$phi[j,,]) > seuil, 1, any ) ] + }) + + list("selected"=selectedVariables,"Rho"=params$Rho,"Pi"=params$Pi) + }) + parallel::stopCluster(cl) +} diff --git a/R/selectiontotale.m b/R/selectiontotale.m deleted file mode 100644 index 5d36a96..0000000 --- a/R/selectiontotale.m +++ /dev/null @@ -1,54 +0,0 @@ -function[A1,A2,Rho,Pi] = selectiontotale(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau) - - [p,m,k] = size(phiInit); - L = length(glambda); - A1 = zeros(p,m+1,L,'int64'); - A2 = zeros(p,m+1,L,'int64'); - Rho = zeros(m,m,k,L); - Pi = zeros(k,L); - - %Pour chaque lambda de la grille, on calcule les coefficients - for lambdaIndex=1:L - [phi,rho,pi,~,~] = EMGLLF(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda(lambdaIndex),X,Y,tau); - - %Si un des coefficients est supérieur au seuil, on garde cette variable - selectedVariables = zeros(p,m); - discardedVariables = zeros(p,m); - atLeastOneSelectedVariable = false; - for j=1:p - cpt=1; - cpt2=1; - for mm=1:m - if max(abs(phi(j,mm,:))) > seuil - selectedVariables(j,cpt) = mm; - cpt = cpt+1; - atLeastOneSelectedVariable = true; - else - discardedVariables(j,cpt2) = mm; - cpt2 = cpt2+1; - end - end - end - - %Si aucun des coefficients n'a été gardé on renvoit la matrice nulle - %Et si on enlevait ces colonnes de zéro ??? Indices des colonnes vides - if atLeastOneSelectedVariable - vec = []; - for j=1:p - if selectedVariables(j,1) ~= 0 - vec = [vec;j]; - end - end - - %Sinon on renvoit les numéros des coefficients utiles - A1(:,1,lambdaIndex) = [vec;zeros(p-length(vec),1)]; - A1(1:length(vec),2:m+1,lambdaIndex) = selectedVariables(vec,:); - A2(:,1,lambdaIndex) = 1:p; - A2(:,2:m+1,lambdaIndex) = discardedVariables; - Rho(:,:,:,lambdaIndex) = rho; - Pi(:,lambdaIndex) = pi; - end - - end - -end -- 2.44.0