move selectiontotale to appropriate folder (untranslated)
[valse.git] / R / constructionModelesLassoMLE.R
1 constructionModelesLassoMLE = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,
2 X,Y,seuil,tau,A1,A2)
3 {
4 n = dim(X)[1];
5 p = dim(phiInit)[1]
6 m = dim(phiInit)[2]
7 k = dim(phiInit)[3]
8 L = length(glambda)
9
10 #output parameters
11 phi = array(0, dim=c(p,m,k,L))
12 rho = array(0, dim=c(m,m,k,L))
13 pi = matrix(0, k, L)
14 llh = matrix(0, L, 2) #log-likelihood
15
16 for(lambdaIndex in 1:L)
17 {
18 a = A1[,1,lambdaIndex]
19 a = a[a!=0]
20 if(length(a)==0)
21 next
22
23 res = EMGLLF(phiInit[a,,],rhoInit,piInit,gamInit,mini,maxi,gamma,0.,X[,a],Y,tau)
24
25 for (j in 1:length(a))
26 phi[a[j],,,lambdaIndex] = res$phi[j,,]
27 rho[,,,lambdaIndex] = res$rho
28 pi[,lambdaIndex] = res$pi
29
30 dimension = 0
31 for (j in 1:p)
32 {
33 b = A2[j,2:dim(A2)[2],lambdaIndex]
34 b = b[b!=0]
35 if (length(b) > 0)
36 phi[A2[j,1,lambdaIndex],b,,lambdaIndex] = 0.
37 c = A1[j,2:dim(A1)[2],lambdaIndex]
38 dimension = dimension + sum(c!=0)
39 }
40
41 #on veut calculer l'EMV avec toutes nos estimations
42 densite = matrix(0, nrow=n, ncol=L)
43 for (i in 1:n)
44 {
45 for (r in 1:k)
46 {
47 delta = Y[i,]%*%rho[,,r,lambdaIndex] - (X[i,a]%*%phi[a,,r,lambdaIndex]);
48 densite[i,lambdaIndex] = densite[i,lambdaIndex] + pi[r,lambdaIndex] *
49 det(rho[,,r,lambdaIndex])/(sqrt(2*base::pi))^m * exp(-tcrossprod(delta)/2.0)
50 }
51 }
52 llh[lambdaIndex,1] = sum(log(densite[,lambdaIndex]))
53 llh[lambdaIndex,2] = (dimension+m+1)*k-1
54 }
55 return (list("phi"=phi, "rho"=rho, "pi"=pi, "llh" = llh))
56 }