1 #' EMGLLF
2 #'
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.
7 #'
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
20 #'
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)
28 #'
29 #' @export
30 EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
31 X, Y, eps, fast)
32 {
33 if (!fast)
34 {
35 # Function in R
36 return(.EMGLLF_R(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
37 X, Y, eps))
38 }
40 # Function in C
41 .Call("EMGLLF", phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
42 X, Y, eps, PACKAGE = "valse")
43 }
45 # R version - slow but easy to read
46 .EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
47 X, Y, eps)
48 {
49 # Matrix dimensions
50 n <- nrow(X)
51 p <- ncol(X)
52 m <- ncol(Y)
53 k <- length(piInit)
55 # Adjustments required when p==1 or m==1 (var.sel. or output dim 1)
56 if (p==1 || m==1)
57 phiInit <- array(phiInit, dim=c(p,m,k))
58 if (m==1)
59 rhoInit <- array(rhoInit, dim=c(m,m,k))
61 # Outputs
62 phi <- phiInit
63 rho <- rhoInit
64 pi <- piInit
65 llh <- -Inf
66 S <- array(0, dim = c(p, m, k))
68 # Algorithm variables
69 gam <- gamInit
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))
75 for (ite in 1:maxi)
76 {
77 # Remember last pi,rho,phi values for exit condition in the end of loop
78 Phi <- phi
79 Rho <- rho
80 Pi <- pi
82 # Computations associated to X and Y
83 for (r in 1:k)
84 {
85 for (mm in 1:m)
86 Y2[, mm, r] <- sqrt(gam[, r]) * Y[, mm]
87 for (i in 1:n)
88 X2[i, , r] <- sqrt(gam[i, r]) * X[i, ]
89 for (mm in 1:m)
90 ps2[, mm, r] <- crossprod(X2[, , r], Y2[, mm, r])
91 for (j in 1:p)
92 {
93 for (s in 1:p)
94 Gram2[j, s, r] <- crossprod(X2[, j, r], X2[, s, r])
95 }
96 }
98 ## M step
100 # For pi
101 b <- sapply(1:k, function(r) sum(abs(phi[, , r])))
102 gam2 <- colSums(gam)
103 a <- sum(gam %*% log(pi))
105 # While the proportions are nonpositive
106 kk <- 0
107 pi2AllPositive <- FALSE
108 while (!pi2AllPositive)
109 {
110 pi2 <- pi + 0.1^kk * ((1/n) * gam2 - pi)
111 pi2AllPositive <- all(pi2 >= 0)
112 kk <- kk + 1
113 }
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))
119 {
120 pi2 <- pi + 0.1^kk * (1/n * gam2 - pi)
121 kk <- kk + 1
122 }
123 t <- 0.1^kk
124 pi <- (pi + t * (pi2 - pi))/sum(pi + t * (pi2 - pi))
126 # For phi and rho
127 for (r in 1:k)
128 {
129 for (mm in 1:m)
130 {
131 ps <- 0
132 for (i in 1:n)
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)
136 }
137 }
139 for (r in 1:k)
140 {
141 for (j in 1:p)
142 {
143 for (mm in 1:m)
144 {
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)) {
148 phi[j, mm, r] <- 0
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]
151 } else {
152 phi[j, mm, r] <- -(n * lambda * (pi[r]^gamma) + S[j, mm, r])/Gram2[j, j, r]
153 }
154 }
155 }
156 }
158 ## E step
160 # Precompute det(rho[,,r]) for r in 1...k
161 detRho <- sapply(1:k, function(r) gdet(rho[, , r]))
162 sumLogLLH <- 0
163 for (i in 1:n)
164 {
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)
169 })
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))
176 }
178 sumPen <- sum(pi^gamma * b)
179 last_llh <- llh
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)))
188 break
189 }
191 affec = apply(gam, 1, which.max)
192 list(phi = phi, rho = rho, pi = pi, llh = llh, S = S, affec=affec)
193 }