From f1b0e0abc369eed5b917a5fe70a05592067a78d2 Mon Sep 17 00:00:00 2001
From: emilie <emilie@devijver.org>
Date: Thu, 16 Mar 2017 16:58:39 +0100
Subject: [PATCH] some few things

---
 R/constructionModelesLassoMLE_essai.R | 55 +++++++++++++++++++++++++++
 R/valse.R                             |  5 +--
 2 files changed, 57 insertions(+), 3 deletions(-)
 create mode 100644 R/constructionModelesLassoMLE_essai.R

diff --git a/R/constructionModelesLassoMLE_essai.R b/R/constructionModelesLassoMLE_essai.R
new file mode 100644
index 0000000..e295d0c
--- /dev/null
+++ b/R/constructionModelesLassoMLE_essai.R
@@ -0,0 +1,55 @@
+constructionModelesLassoMLE = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau,selected){
+  #get matrix sizes
+  n = dim(X)[1]
+  p = dim(phiInit)[1]
+  m = dim(phiInit)[2]
+  k = dim(phiInit)[3]
+  L = length(glambda)
+  
+  #output parameters
+  phi = array(0, dim = c(p,m,k,L))
+  rho = array(dim = c(m,m,k,L))
+  pi = array( dim = c(k,L))
+  lvraisemblance = array( dim = c(L,2))
+  
+  for (lambdaIndex in 1:L){
+    # Procedure Lasso-MLE  
+    a = selected[,1,lambdaIndex]
+    a(a==0) = c()
+    if (length(a) != 0){
+      res_EM = EMGLLF(phiInit[a,,],rhoInit,piInit,gamInit,mini,maxi,gamma,0,X[,a],Y,tau)
+      phiLambda = res_EM$phi
+      rhoLambda = res_EM$rho
+      piLambda = res_EM$pi
+      for (j in 1:length(a)){
+        phi[a[j],,,lambdaIndex] = phiLambda[j,,]
+      }
+      rho[,,,lambdaIndex] = rhoLambda
+      pi[,lambdaIndex] = piLambda
+      
+      dimension = 0
+      for (j in 1:p){
+        b = A2[j,2:end,lambdaIndex]
+        b[b==0] = c()
+        if (length(b) > 0){
+        phi[A2[j,1,lambdaIndex],b,,lambdaIndex] = 0.0
+        }
+        c = A1[j,2:end,lambdaIndex]
+        c[c==0] = c()
+        dimension = dimension + length(c)
+      }
+      
+      #on veut calculer l'EMV avec toutes nos estimations
+      densite = array(n,L)
+      for (i in 1:n){
+        for (r in 1:k){
+          delta = Y[i,]*rho[,,r,lambdaIndex] - X[i,a]*phi[a,,r,lambdaIndex]
+          densite[i,lambdaIndex] = densite[i,lambdaIndex] + pi[r,lambdaIndex]*det(rho[,,r,lambdaIndex])/(sqrt(2*base::pi))^m*exp(-delta %*% delta/2.0)
+        }
+      }
+      
+      lvraisemblance(lambdaIndex,1) = sum(log(densite[,lambdaIndex]))
+      lvraisemblance(lambdaIndex,2) = (dimension+m+1)*k-1
+    }
+  }
+}
\ No newline at end of file
diff --git a/R/valse.R b/R/valse.R
index f84c2c5..445ea26 100644
--- a/R/valse.R
+++ b/R/valse.R
@@ -47,8 +47,7 @@ valse = function(X,Y,procedure,selecMod,gamma = 1,mini = 10,
     #from the grid: A1 corresponding to selected variables, and
     #A2 corresponding to unselected variables.
     params = selectiontotale(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,gridLambda,X,Y,1e-8,eps)
-    A1 <<- params$A1
-    A2 <<- params$A2
+    selected <<- params$selected
     Rho <<- params$Rho
     Pi <<- params$Pi
     
@@ -58,7 +57,7 @@ valse = function(X,Y,procedure,selecMod,gamma = 1,mini = 10,
       #Estimator, restricted on selected variables.
       model = constructionModelesLassoMLE(
         phiInit, rhoInit,piInit,tauInit,mini,maxi,
-        gamma,gridLambda,X,Y,thresh,eps,A1,A2)
+        gamma,gridLambda,X,Y,thresh,eps,selected)
       ################################################
       ### Regarder la SUITE
       r1 = runProcedure1()
-- 
2.44.0