3 #' Description de EMGLLF
5 #' @param phiInit an initialization for phi
6 #' @param rhoInit an initialization for rho
7 #' @param piInit an initialization for pi
8 #' @param gamInit initialization for the a posteriori probabilities
9 #' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10
10 #' @param maxi integer, maximum number of iterations in the EM algorithm, by default = 100
11 #' @param gamma integer for the power in the penaly, by default = 1
12 #' @param lambda regularization parameter in the Lasso estimation
13 #' @param X matrix of covariates (of size n*p)
14 #' @param Y matrix of responses (of size n*m)
15 #' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4
17 #' @return A list ... phi,rho,pi,LLF,S,affec:
18 #' phi : parametre de moyenne renormalisé, calculé par l'EM
19 #' rho : parametre de variance renormalisé, calculé par l'EM
20 #' pi : parametre des proportions renormalisé, calculé par l'EM
21 #' LLF : log vraisemblance associée à cet échantillon, pour les valeurs estimées des paramètres
22 #' S : ... affec : ...
25 EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
26 X, Y, eps, fast = TRUE)
31 return(.EMGLLF_R(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
36 n <- nrow(X) #nombre d'echantillons
37 p <- ncol(X) #nombre de covariables
38 m <- ncol(Y) #taille de Y (multivarié)
39 k <- length(piInit) #nombre de composantes dans le mélange
40 .Call("EMGLLF", phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
41 X, Y, eps, phi = double(p * m * k), rho = double(m * m * k), pi = double(k),
42 LLF = double(maxi), S = double(p * m * k), affec = integer(n), n, p, m, k,
46 # R version - slow but easy to read
47 .EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
52 if (length(dim(phiInit)) == 2)
63 X <- matrix(nrow = n, ncol = p)
66 phi <- array(NA, dim = c(p, m, k))
67 phi[1:p, , ] <- phiInit
71 S <- array(0, dim = c(p, m, k))
75 Gram2 <- array(0, dim = c(p, p, k))
76 ps2 <- array(0, dim = c(p, m, k))
77 X2 <- array(0, dim = c(n, p, k))
78 Y2 <- array(0, dim = c(n, m, k))
83 # Remember last pi,rho,phi values for exit condition in the end of loop
88 # Computations associated to X and Y
91 for (mm in 1:m) Y2[, mm, r] <- sqrt(gam[, r]) * Y[, mm]
92 for (i in 1:n) X2[i, , r] <- sqrt(gam[i, r]) * X[i, ]
93 for (mm in 1:m) ps2[, mm, r] <- crossprod(X2[, , r], Y2[, mm, r])
96 for (s in 1:p) Gram2[j, s, r] <- crossprod(X2[, j, r], X2[, s, r])
103 b <- sapply(1:k, function(r) sum(abs(phi[, , r])))
105 a <- sum(gam %*% log(pi))
107 # While the proportions are nonpositive
109 pi2AllPositive <- FALSE
110 while (!pi2AllPositive)
112 pi2 <- pi + 0.1^kk * ((1/n) * gam2 - pi)
113 pi2AllPositive <- all(pi2 >= 0)
117 # t(m) is the largest value in the grid O.1^k such that it is nonincreasing
118 while (kk < 1000 && -a/n + lambda * sum(pi^gamma * b) < -sum(gam2 * log(pi2))/n +
119 lambda * sum(pi2^gamma * b))
121 pi2 <- pi + 0.1^kk * (1/n * gam2 - pi)
125 pi <- (pi + t * (pi2 - pi))/sum(pi + t * (pi2 - pi))
133 for (i in 1:n) ps <- ps + Y2[i, mm, r] * sum(X2[i, , r] * phi[, mm,
135 nY2 <- sum(Y2[, mm, r]^2)
136 rho[mm, mm, r] <- (ps + sqrt(ps^2 + 4 * nY2 * gam2[r]))/(2 * nY2)
146 S[j, mm, r] <- -rho[mm, mm, r] * ps2[j, mm, r] + sum(phi[-j, mm,
147 r] * Gram2[j, -j, r])
148 if (abs(S[j, mm, r]) <= n * lambda * (pi[r]^gamma))
151 } else if (S[j, mm, r] > n * lambda * (pi[r]^gamma))
153 phi[j, mm, r] <- (n * lambda * (pi[r]^gamma) - S[j, mm, r])/Gram2[j,
157 phi[j, mm, r] <- -(n * lambda * (pi[r]^gamma) + S[j, mm, r])/Gram2[j,
166 # Precompute det(rho[,,r]) for r in 1...k
167 detRho <- sapply(1:k, function(r) det(rho[, , r]))
168 gam1 <- matrix(0, nrow = n, ncol = k)
172 for (r in 1:k) gam1[i, r] <- pi[r] * exp(-0.5 * sum((Y[i, ] %*% rho[,
173 , r] - X[i, ] %*% phi[, , r])^2)) * detRho[r]
175 gam <- gam1/rowSums(gam1)
176 sumLogLLH <- sum(log(rowSums(gam)) - log((2 * base::pi)^(m/2)))
177 sumPen <- sum(pi^gamma * b)
179 llh <- -sumLogLLH/n + lambda * sumPen
180 dist <- ifelse(ite == 1, llh, (llh - last_llh)/(1 + abs(llh)))
181 Dist1 <- max((abs(phi - Phi))/(1 + abs(phi)))
182 Dist2 <- max((abs(rho - Rho))/(1 + abs(rho)))
183 Dist3 <- max((abs(pi - Pi))/(1 + abs(Pi)))
184 dist2 <- max(Dist1, Dist2, Dist3)
186 if (ite >= mini && (dist >= eps || dist2 >= sqrt(eps)))
190 list(phi = phi, rho = rho, pi = pi, llh = llh, S = S)