3 #' Run a generalized EM algorithm developped for mixture of Gaussian regression
4 #' models with variable selection by an extension of the Lasso estimator (regularization parameter lambda).
5 #' Reparametrization is done to ensure invariance by homothetic transformation.
6 #' It returns a collection of models, varying the number of clusters and the sparsity in the regression mean.
8 #' @param phiInit an initialization for phi
9 #' @param rhoInit an initialization for rho
10 #' @param piInit an initialization for pi
11 #' @param gamInit initialization for the a posteriori probabilities
12 #' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10
13 #' @param maxi integer, maximum number of iterations in the EM algorithm, by default = 100
14 #' @param gamma integer for the power in the penaly, by default = 1
15 #' @param lambda regularization parameter in the Lasso estimation
16 #' @param X matrix of covariates (of size n*p)
17 #' @param Y matrix of responses (of size n*m)
18 #' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4
19 #' @param fast boolean to enable or not the C function call
21 #' @return A list (corresponding to the model collection) defined by (phi,rho,pi,LLF,S,affec):
22 #' phi : regression mean for each cluster
23 #' rho : variance (homothetic) for each cluster
24 #' pi : proportion for each cluster
25 #' LLF : log likelihood with respect to the training set
26 #' S : selected variables indexes
27 #' affec : cluster affectation for each observation (of the training set)
30 EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
36 return(.EMGLLF_R(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
41 .Call("EMGLLF", phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
42 X, Y, eps, PACKAGE = "valse")
45 # R version - slow but easy to read
46 .EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
55 # Adjustments required when p==1 or m==1 (var.sel. or output dim 1)
57 phiInit <- array(phiInit, dim=c(p,m,k))
59 rhoInit <- array(rhoInit, dim=c(m,m,k))
66 S <- array(0, dim = c(p, m, k))
70 Gram2 <- array(0, dim = c(p, p, k))
71 ps2 <- array(0, dim = c(p, m, k))
72 X2 <- array(0, dim = c(n, p, k))
73 Y2 <- array(0, dim = c(n, m, k))
77 # Remember last pi,rho,phi values for exit condition in the end of loop
82 # Computations associated to X and Y
86 Y2[, mm, r] <- sqrt(gam[, r]) * Y[, mm]
88 X2[i, , r] <- sqrt(gam[i, r]) * X[i, ]
90 ps2[, mm, r] <- crossprod(X2[, , r], Y2[, mm, r])
94 Gram2[j, s, r] <- crossprod(X2[, j, r], X2[, s, r])
101 b <- sapply(1:k, function(r) sum(abs(phi[, , r])))
103 a <- sum(gam %*% log(pi))
105 # While the proportions are nonpositive
107 pi2AllPositive <- FALSE
108 while (!pi2AllPositive)
110 pi2 <- pi + 0.1^kk * ((1/n) * gam2 - pi)
111 pi2AllPositive <- all(pi2 >= 0)
115 # t(m) is the largest value in the grid O.1^k such that it is nonincreasing
116 while (kk < 1000 && -a/n + lambda * sum(pi^gamma * b) <
117 # na.rm=TRUE to handle 0*log(0)
118 -sum(gam2 * log(pi2), na.rm=TRUE)/n + lambda * sum(pi2^gamma * b))
120 pi2 <- pi + 0.1^kk * (1/n * gam2 - pi)
124 pi <- (pi + t * (pi2 - pi))/sum(pi + t * (pi2 - pi))
133 ps <- ps + Y2[i, mm, r] * sum(X2[i, , r] * phi[, mm, r])
134 nY2 <- sum(Y2[, mm, r]^2)
135 rho[mm, mm, r] <- (ps + sqrt(ps^2 + 4 * nY2 * gam2[r]))/(2 * nY2)
145 S[j, mm, r] <- -rho[mm, mm, r] * ps2[j, mm, r] +
146 sum(phi[-j, mm, r] * Gram2[j, -j, r])
147 if (abs(S[j, mm, r]) <= n * lambda * (pi[r]^gamma)) {
149 } else if (S[j, mm, r] > n * lambda * (pi[r]^gamma)) {
150 phi[j, mm, r] <- (n * lambda * (pi[r]^gamma) - S[j, mm, r])/Gram2[j, j, r]
152 phi[j, mm, r] <- -(n * lambda * (pi[r]^gamma) + S[j, mm, r])/Gram2[j, j, r]
160 # Precompute det(rho[,,r]) for r in 1...k
161 detRho <- sapply(1:k, function(r) gdet(rho[, , r]))
165 # Update gam[,]; use log to avoid numerical problems
166 logGam <- sapply(1:k, function(r) {
167 log(pi[r]) + log(detRho[r]) - 0.5 *
168 sum((Y[i, ] %*% rho[, , r] - X[i, ] %*% phi[, , r])^2)
171 logGam <- logGam - max(logGam) #adjust without changing proportions
172 gam[i, ] <- exp(logGam)
173 norm_fact <- sum(gam[i, ])
174 gam[i, ] <- gam[i, ] / norm_fact
175 sumLogLLH <- sumLogLLH + log(norm_fact) - log((2 * base::pi)^(m/2))
178 sumPen <- sum(pi^gamma * b)
180 llh <- -sumLogLLH/n #+ lambda * sumPen
181 dist <- ifelse(ite == 1, llh, (llh - last_llh)/(1 + abs(llh)))
182 Dist1 <- max((abs(phi - Phi))/(1 + abs(phi)))
183 Dist2 <- max((abs(rho - Rho))/(1 + abs(rho)))
184 Dist3 <- max((abs(pi - Pi))/(1 + abs(Pi)))
185 dist2 <- max(Dist1, Dist2, Dist3)
187 if (ite >= mini && (dist >= eps || dist2 >= sqrt(eps)))
191 affec = apply(gam, 1, which.max)
192 list(phi = phi, rho = rho, pi = pi, llh = llh, S = S, affec=affec)