3 #' initialization of the EM algorithm
5 #' @param k number of components
6 #' @param X matrix of covariates (of size n*p)
7 #' @param Y matrix of responses (of size n*m)
8 #' @param fast boolean to enable or not the C function call
10 #' @return a list with phiInit, rhoInit, piInit, gamInit
12 #' @importFrom stats cutree dist hclust runif
15 initSmallEM <- function(k, X, Y, fast)
21 Zinit1 <- array(0, dim = c(n, nIte))
22 betaInit1 <- array(0, dim = c(p, m, k, nIte))
23 sigmaInit1 <- array(0, dim = c(m, m, k, nIte))
24 phiInit1 <- array(0, dim = c(p, m, k, nIte))
25 rhoInit1 <- array(0, dim = c(m, m, k, nIte))
26 Gam <- matrix(0, n, k)
27 piInit1 <- matrix(0, nIte, k)
28 gamInit1 <- array(0, dim = c(n, k, nIte))
31 # require(MASS) #Moore-Penrose generalized inverse of matrix
34 distance_clus <- dist(cbind(X, Y))
35 tree_hier <- hclust(distance_clus)
36 Zinit1[, repet] <- cutree(tree_hier, k)
41 Z_indice <- seq_len(n)[Z == r] #renvoit les indices ou Z==r
42 if (length(Z_indice) == 1) {
43 betaInit1[, , r, repet] <- MASS::ginv(crossprod(t(X[Z_indice, ]))) %*%
44 crossprod(t(X[Z_indice, ]), Y[Z_indice, ])
46 betaInit1[, , r, repet] <- MASS::ginv(crossprod(X[Z_indice, ])) %*%
47 crossprod(X[Z_indice, ], Y[Z_indice, ])
49 sigmaInit1[, , r, repet] <- diag(m)
50 phiInit1[, , r, repet] <- betaInit1[, , r, repet] #/ sigmaInit1[,,r,repet]
51 rhoInit1[, , r, repet] <- solve(sigmaInit1[, , r, repet])
52 piInit1[repet, r] <- mean(Z == r)
59 dotProduct <- tcrossprod(Y[i, ] %*% rhoInit1[, , r, repet]
60 - X[i, ] %*% phiInit1[, , r, repet])
61 Gam[i, r] <- piInit1[repet, r] *
62 det(rhoInit1[, , r, repet]) * exp(-0.5 * dotProduct)
64 sumGamI <- sum(Gam[i, ])
65 # TODO: next line is a division by zero if dotProduct is big
66 gamInit1[i, , repet] <- Gam[i, ]/sumGamI
72 init_EMG <- EMGLLF(phiInit1[, , , repet], rhoInit1[, , , repet], piInit1[repet, ],
73 gamInit1[, , repet], miniInit, maxiInit, gamma = 1, lambda = 0, X, Y,
75 LLFinit1[[repet]] <- init_EMG$llh
77 b <- which.min(LLFinit1)
78 phiInit <- phiInit1[, , , b]
79 rhoInit <- rhoInit1[, , , b]
80 piInit <- piInit1[b, ]
81 gamInit <- gamInit1[, , b]
83 list(phiInit = phiInit, rhoInit = rhoInit, piInit = piInit, gamInit = gamInit)