Merge branch 'master' of auder.net:valse
[valse.git] / pkg / R / constructionModelesLassoMLE_essai.R
CommitLineData
f1b0e0ab 1constructionModelesLassoMLE = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,glambda,X,Y,seuil,tau,selected){
2 #get matrix sizes
3 n = dim(X)[1]
4 p = dim(phiInit)[1]
5 m = dim(phiInit)[2]
6 k = dim(phiInit)[3]
7 L = length(glambda)
8
9 #output parameters
10 phi = array(0, dim = c(p,m,k,L))
11 rho = array(dim = c(m,m,k,L))
12 pi = array( dim = c(k,L))
13 lvraisemblance = array( dim = c(L,2))
14
15 for (lambdaIndex in 1:L){
16 # Procedure Lasso-MLE
17 a = selected[,1,lambdaIndex]
18 a(a==0) = c()
19 if (length(a) != 0){
20 res_EM = EMGLLF(phiInit[a,,],rhoInit,piInit,gamInit,mini,maxi,gamma,0,X[,a],Y,tau)
21 phiLambda = res_EM$phi
22 rhoLambda = res_EM$rho
23 piLambda = res_EM$pi
24 for (j in 1:length(a)){
25 phi[a[j],,,lambdaIndex] = phiLambda[j,,]
26 }
27 rho[,,,lambdaIndex] = rhoLambda
28 pi[,lambdaIndex] = piLambda
29
30 dimension = 0
31 for (j in 1:p){
32 b = A2[j,2:end,lambdaIndex]
33 b[b==0] = c()
34 if (length(b) > 0){
35 phi[A2[j,1,lambdaIndex],b,,lambdaIndex] = 0.0
36 }
37 c = A1[j,2:end,lambdaIndex]
38 c[c==0] = c()
39 dimension = dimension + length(c)
40 }
41
42 #on veut calculer l'EMV avec toutes nos estimations
43 densite = array(n,L)
44 for (i in 1:n){
45 for (r in 1:k){
46 delta = Y[i,]*rho[,,r,lambdaIndex] - X[i,a]*phi[a,,r,lambdaIndex]
47 densite[i,lambdaIndex] = densite[i,lambdaIndex] + pi[r,lambdaIndex]*det(rho[,,r,lambdaIndex])/(sqrt(2*base::pi))^m*exp(-delta %*% delta/2.0)
48 }
49 }
50
51 lvraisemblance(lambdaIndex,1) = sum(log(densite[,lambdaIndex]))
52 lvraisemblance(lambdaIndex,2) = (dimension+m+1)*k-1
53 }
54 }
55}