1 EMGLLF_R = function(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,lambda,X,Y,tau)
14 S = array(0, dim=c(p,m,k))
17 Gram2 = array(0, dim=c(p,p,k))
18 ps2 = array(0, dim=c(p,m,k))
20 X2 = array(0, dim=c(n,p,k))
21 Y2 = array(0, dim=c(n,m,k))
28 ps1 = array(0, dim=c(n,m,k))
32 while(ite <= mini || (ite<= maxi && (dist>= tau || dist2 >= sqrt(tau))))
38 #calcul associé à Y et X
42 Y2[,mm,r] = sqrt(gam[,r]) * Y[,mm]
44 X2[i,,r] = sqrt(gam[i,r]) * X[i,]
46 ps2[,mm,r] = crossprod(X2[,,r],Y2[,mm,r])
50 Gram2[j,s,r] = crossprod(X2[,j,r], X2[,s,r])
60 b[r] = sum(abs(phi[,,r]))
62 a = sum(gam %*% log(pi))
64 #tant que les props sont negatives
66 pi2AllPositive = FALSE
67 while (!pi2AllPositive)
69 pi2 = pi + 0.1^kk * ((1/n)*gam2 - pi)
70 pi2AllPositive = all(pi2 >= 0)
74 #t(m) la plus grande valeur dans la grille O.1^k tel que ce soit décroissante ou constante
75 while( kk < 1000 && -a/n + lambda * sum(pi^gamma * b) <
76 -sum(gam2 * log(pi2))/n + lambda * sum(pi2^gamma * b) )
78 pi2 = pi + 0.1^kk * (1/n*gam2 - pi)
82 pi = (pi + t*(pi2-pi)) / sum(pi + t*(pi2-pi))
91 ps1[i,mm,r] = Y2[i,mm,r] * sum(X2[i,,r] * phi[,mm,r])
93 ps[mm,r] = sum(ps1[,mm,r])
94 nY2[mm,r] = sum(Y2[,mm,r]^2)
95 rho[mm,mm,r] = (ps[mm,r]+sqrt(ps[mm,r]^2+4*nY2[mm,r]*gam2[r])) / (2*nY2[mm,r])
105 S[j,mm,r] = -rho[mm,mm,r]*ps2[j,mm,r] + sum(phi[-j,mm,r] * Gram2[j,-j,r])
106 if (abs(S[j,mm,r]) <= n*lambda*(pi[r]^gamma))
108 else if(S[j,mm,r] > n*lambda*(pi[r]^gamma))
109 phi[j,mm,r] = (n*lambda*(pi[r]^gamma)-S[j,mm,r]) / Gram2[j,j,r]
111 phi[j,mm,r] = -(n*lambda*(pi[r]^gamma)+S[j,mm,r]) / Gram2[j,j,r]
123 #precompute sq norms to numerically adjust their values
126 sqNorm2[r] = sum( (Y[i,]%*%rho[,,r]-X[i,]%*%phi[,,r])^2 )
132 Gam[i,r] = pi[r] * exp(-0.5*sqNorm2[r]) * det(rho[,,r])
133 sumLLF1 = sumLLF1 + Gam[i,r] / (2*base::pi)^(m/2)
135 sumLogLLF2 = sumLogLLF2 + log(sumLLF1)
136 sumGamI = sum(Gam[i,])
138 gam[i,] = Gam[i,] / sumGamI
143 sumPen = sum(pi^gamma * b)
144 LLF[ite] = -sumLogLLF2/n + lambda*sumPen
145 dist = ifelse( ite == 1, LLF[ite], (LLF[ite]-LLF[ite-1]) / (1+abs(LLF[ite])) )
146 Dist1 = max( (abs(phi-Phi)) / (1+abs(phi)) )
147 Dist2 = max( (abs(rho-Rho)) / (1+abs(rho)) )
148 Dist3 = max( (abs(pi-Pi)) / (1+abs(Pi)) )
149 dist2 = max(Dist1,Dist2,Dist3)
154 affec = apply(gam, 1, which.max)
155 return(list("phi"=phi, "rho"=rho, "pi"=pi, "LLF"=LLF, "S"=S, "affec" = affec ))