| 1 | #' initialization of the EM algorithm |
| 2 | #' |
| 3 | #' @param k number of components |
| 4 | #' @param X matrix of covariates (of size n*p) |
| 5 | #' @param Y matrix of responses (of size n*m) |
| 6 | #' @param fast boolean to enable or not the C function call |
| 7 | #' |
| 8 | #' @return a list with phiInit, rhoInit, piInit, gamInit |
| 9 | #' |
| 10 | #' @importFrom stats cutree dist hclust runif |
| 11 | #' @export |
| 12 | initSmallEM <- function(k, X, Y, fast) |
| 13 | { |
| 14 | n <- nrow(X) |
| 15 | p <- ncol(X) |
| 16 | m <- ncol(Y) |
| 17 | nIte <- 20 |
| 18 | Zinit1 <- array(0, dim = c(n, nIte)) |
| 19 | betaInit1 <- array(0, dim = c(p, m, k, nIte)) |
| 20 | sigmaInit1 <- array(0, dim = c(m, m, k, nIte)) |
| 21 | phiInit1 <- array(0, dim = c(p, m, k, nIte)) |
| 22 | rhoInit1 <- array(0, dim = c(m, m, k, nIte)) |
| 23 | Gam <- matrix(0, n, k) |
| 24 | piInit1 <- matrix(0, nIte, k) |
| 25 | gamInit1 <- array(0, dim = c(n, k, nIte)) |
| 26 | LLFinit1 <- list() |
| 27 | |
| 28 | # require(MASS) #Moore-Penrose generalized inverse of matrix |
| 29 | for (repet in 1:nIte) |
| 30 | { |
| 31 | distance_clus <- dist(cbind(X, Y)) |
| 32 | tree_hier <- hclust(distance_clus) |
| 33 | Zinit1[, repet] <- cutree(tree_hier, k) |
| 34 | |
| 35 | for (r in 1:k) |
| 36 | { |
| 37 | Z <- Zinit1[, repet] |
| 38 | Z_indice <- seq_len(n)[Z == r] #renvoit les indices ou Z==r |
| 39 | if (length(Z_indice) == 1) { |
| 40 | betaInit1[, , r, repet] <- MASS::ginv(crossprod(t(X[Z_indice, ]))) %*% |
| 41 | crossprod(t(X[Z_indice, ]), Y[Z_indice, ]) |
| 42 | } else { |
| 43 | betaInit1[, , r, repet] <- MASS::ginv(crossprod(X[Z_indice, ])) %*% |
| 44 | crossprod(X[Z_indice, ], Y[Z_indice, ]) |
| 45 | } |
| 46 | sigmaInit1[, , r, repet] <- diag(m) |
| 47 | phiInit1[, , r, repet] <- betaInit1[, , r, repet] #/ sigmaInit1[,,r,repet] |
| 48 | rhoInit1[, , r, repet] <- solve(sigmaInit1[, , r, repet]) |
| 49 | piInit1[repet, r] <- mean(Z == r) |
| 50 | } |
| 51 | |
| 52 | for (i in 1:n) |
| 53 | { |
| 54 | for (r in 1:k) |
| 55 | { |
| 56 | dotProduct <- tcrossprod(Y[i, ] %*% rhoInit1[, , r, repet] |
| 57 | - X[i, ] %*% phiInit1[, , r, repet]) |
| 58 | Gam[i, r] <- piInit1[repet, r] * |
| 59 | det(rhoInit1[, , r, repet]) * exp(-0.5 * dotProduct) |
| 60 | } |
| 61 | sumGamI <- sum(Gam[i, ]) |
| 62 | # TODO: next line is a division by zero if dotProduct is big |
| 63 | gamInit1[i, , repet] <- Gam[i, ]/sumGamI |
| 64 | } |
| 65 | |
| 66 | miniInit <- 10 |
| 67 | maxiInit <- 11 |
| 68 | |
| 69 | init_EMG <- EMGLLF(phiInit1[, , , repet], rhoInit1[, , , repet], piInit1[repet, ], |
| 70 | gamInit1[, , repet], miniInit, maxiInit, gamma = 1, lambda = 0, X, Y, |
| 71 | eps = 1e-04, fast) |
| 72 | LLFinit1[[repet]] <- init_EMG$llh |
| 73 | } |
| 74 | b <- which.min(LLFinit1) |
| 75 | phiInit <- phiInit1[, , , b] |
| 76 | rhoInit <- rhoInit1[, , , b] |
| 77 | piInit <- piInit1[b, ] |
| 78 | gamInit <- gamInit1[, , b] |
| 79 | |
| 80 | list(phiInit = phiInit, rhoInit = rhoInit, piInit = piInit, gamInit = gamInit) |
| 81 | } |