| 1 | #' EMGLLF |
| 2 | #' |
| 3 | #' Description de EMGLLF |
| 4 | #' |
| 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 |
| 16 | #' |
| 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 : ... |
| 23 | #' |
| 24 | #' @export |
| 25 | EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, |
| 26 | X, Y, eps, fast = TRUE) |
| 27 | { |
| 28 | if (!fast) |
| 29 | { |
| 30 | # Function in R |
| 31 | return(.EMGLLF_R(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, |
| 32 | X, Y, eps)) |
| 33 | } |
| 34 | |
| 35 | # Function in C |
| 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, |
| 43 | PACKAGE = "valse") |
| 44 | } |
| 45 | |
| 46 | # R version - slow but easy to read |
| 47 | .EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, |
| 48 | X, Y, eps) |
| 49 | { |
| 50 | # Matrix dimensions: NOTE: phiInit *must* be an array (even if p==1) |
| 51 | n <- dim(Y)[1] |
| 52 | p <- dim(phiInit)[1] |
| 53 | m <- dim(phiInit)[2] |
| 54 | k <- dim(phiInit)[3] |
| 55 | |
| 56 | # Outputs |
| 57 | phi <- array(NA, dim = c(p, m, k)) |
| 58 | phi[1:p, , ] <- phiInit |
| 59 | rho <- rhoInit |
| 60 | pi <- piInit |
| 61 | llh <- -Inf |
| 62 | S <- array(0, dim = c(p, m, k)) |
| 63 | |
| 64 | # Algorithm variables |
| 65 | gam <- gamInit |
| 66 | Gram2 <- array(0, dim = c(p, p, k)) |
| 67 | ps2 <- array(0, dim = c(p, m, k)) |
| 68 | X2 <- array(0, dim = c(n, p, k)) |
| 69 | Y2 <- array(0, dim = c(n, m, k)) |
| 70 | EPS <- 1e-15 |
| 71 | |
| 72 | for (ite in 1:maxi) |
| 73 | { |
| 74 | # Remember last pi,rho,phi values for exit condition in the end of loop |
| 75 | Phi <- phi |
| 76 | Rho <- rho |
| 77 | Pi <- pi |
| 78 | |
| 79 | # Computations associated to X and Y |
| 80 | for (r in 1:k) |
| 81 | { |
| 82 | for (mm in 1:m) |
| 83 | Y2[, mm, r] <- sqrt(gam[, r]) * Y[, mm] |
| 84 | for (i in 1:n) |
| 85 | X2[i, , r] <- sqrt(gam[i, r]) * X[i, ] |
| 86 | for (mm in 1:m) |
| 87 | ps2[, mm, r] <- crossprod(X2[, , r], Y2[, mm, r]) |
| 88 | for (j in 1:p) |
| 89 | { |
| 90 | for (s in 1:p) |
| 91 | Gram2[j, s, r] <- crossprod(X2[, j, r], X2[, s, r]) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | ## M step |
| 96 | |
| 97 | # For pi |
| 98 | b <- sapply(1:k, function(r) sum(abs(phi[, , r]))) |
| 99 | gam2 <- colSums(gam) |
| 100 | a <- sum(gam %*% log(pi)) |
| 101 | |
| 102 | # While the proportions are nonpositive |
| 103 | kk <- 0 |
| 104 | pi2AllPositive <- FALSE |
| 105 | while (!pi2AllPositive) |
| 106 | { |
| 107 | pi2 <- pi + 0.1^kk * ((1/n) * gam2 - pi) |
| 108 | pi2AllPositive <- all(pi2 >= 0) |
| 109 | kk <- kk + 1 |
| 110 | } |
| 111 | |
| 112 | # t(m) is the largest value in the grid O.1^k such that it is nonincreasing |
| 113 | while (kk < 1000 && -a/n + lambda * sum(pi^gamma * b) < |
| 114 | -sum(gam2 * log(pi2))/n + lambda * sum(pi2^gamma * b)) |
| 115 | { |
| 116 | pi2 <- pi + 0.1^kk * (1/n * gam2 - pi) |
| 117 | kk <- kk + 1 |
| 118 | } |
| 119 | t <- 0.1^kk |
| 120 | pi <- (pi + t * (pi2 - pi))/sum(pi + t * (pi2 - pi)) |
| 121 | |
| 122 | # For phi and rho |
| 123 | for (r in 1:k) |
| 124 | { |
| 125 | for (mm in 1:m) |
| 126 | { |
| 127 | ps <- 0 |
| 128 | for (i in 1:n) |
| 129 | ps <- ps + Y2[i, mm, r] * sum(X2[i, , r] * phi[, mm, r]) |
| 130 | nY2 <- sum(Y2[, mm, r]^2) |
| 131 | rho[mm, mm, r] <- (ps + sqrt(ps^2 + 4 * nY2 * gam2[r]))/(2 * nY2) |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | for (r in 1:k) |
| 136 | { |
| 137 | for (j in 1:p) |
| 138 | { |
| 139 | for (mm in 1:m) |
| 140 | { |
| 141 | S[j, mm, r] <- -rho[mm, mm, r] * ps2[j, mm, r] |
| 142 | + sum(phi[-j, mm, r] * Gram2[j, -j, r]) |
| 143 | if (abs(S[j, mm, r]) <= n * lambda * (pi[r]^gamma)) { |
| 144 | phi[j, mm, r] <- 0 |
| 145 | } else if (S[j, mm, r] > n * lambda * (pi[r]^gamma)) { |
| 146 | phi[j, mm, r] <- (n * lambda * (pi[r]^gamma) - S[j, mm, r])/Gram2[j, j, r] |
| 147 | } else { |
| 148 | phi[j, mm, r] <- -(n * lambda * (pi[r]^gamma) + S[j, mm, r])/Gram2[j, j, r] |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | ## E step |
| 155 | |
| 156 | # Precompute det(rho[,,r]) for r in 1...k |
| 157 | detRho <- sapply(1:k, function(r) det(rho[, , r])) |
| 158 | for (i in 1:n) |
| 159 | { |
| 160 | # Update gam[,] |
| 161 | for (r in 1:k) |
| 162 | { |
| 163 | gam[i, r] <- pi[r] * exp(-0.5 |
| 164 | * sum((Y[i, ] %*% rho[, , r] - X[i, ] %*% phi[, , r])^2)) * detRho[r] |
| 165 | } |
| 166 | } |
| 167 | norm_fact <- rowSums(gam) |
| 168 | gam <- gam / norm_fact |
| 169 | sumLogLLH <- sum(log(norm_fact) - log((2 * base::pi)^(m/2))) |
| 170 | sumPen <- sum(pi^gamma * b) |
| 171 | last_llh <- llh |
| 172 | llh <- -sumLogLLH/n + lambda * sumPen |
| 173 | dist <- ifelse(ite == 1, llh, (llh - last_llh)/(1 + abs(llh))) |
| 174 | Dist1 <- max((abs(phi - Phi))/(1 + abs(phi))) |
| 175 | Dist2 <- max((abs(rho - Rho))/(1 + abs(rho))) |
| 176 | Dist3 <- max((abs(pi - Pi))/(1 + abs(Pi))) |
| 177 | dist2 <- max(Dist1, Dist2, Dist3) |
| 178 | |
| 179 | if (ite >= mini && (dist >= eps || dist2 >= sqrt(eps))) |
| 180 | break |
| 181 | } |
| 182 | |
| 183 | list(phi = phi, rho = rho, pi = pi, llh = llh, S = S) |
| 184 | } |