1 #' initialization of the EM algorithm
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)
7 #' @return a list with phiInit, rhoInit, piInit, gamInit
9 #' @importFrom methods new
10 #' @importFrom stats cutree dist hclust runif
11 initSmallEM = function(k,X,Y)
17 Zinit1 = array(0, dim=c(n,20))
18 betaInit1 = array(0, dim=c(p,m,k,20))
19 sigmaInit1 = array(0, dim = c(m,m,k,20))
20 phiInit1 = array(0, dim = c(p,m,k,20))
21 rhoInit1 = array(0, dim = c(m,m,k,20))
23 piInit1 = matrix(0,20,k)
24 gamInit1 = array(0, dim=c(n,k,20))
27 require(MASS) #Moore-Penrose generalized inverse of matrix
30 distance_clus = dist(X)
31 tree_hier = hclust(distance_clus)
32 Zinit1[,repet] = cutree(tree_hier, k)
37 Z_indice = seq_len(n)[Z == r] #renvoit les indices où Z==r
38 if (length(Z_indice) == 1) {
39 betaInit1[,,r,repet] = ginv(crossprod(t(X[Z_indice,]))) %*%
40 crossprod(t(X[Z_indice,]), Y[Z_indice,])
42 betaInit1[,,r,repet] = ginv(crossprod(X[Z_indice,])) %*%
43 crossprod(X[Z_indice,], Y[Z_indice,])
45 sigmaInit1[,,r,repet] = diag(m)
46 phiInit1[,,r,repet] = betaInit1[,,r,repet] #/ sigmaInit1[,,r,repet]
47 rhoInit1[,,r,repet] = solve(sigmaInit1[,,r,repet])
48 piInit1[repet,r] = mean(Z == r)
55 dotProduct = tcrossprod(Y[i,]%*%rhoInit1[,,r,repet]-X[i,]%*%phiInit1[,,r,repet])
56 Gam[i,r] = piInit1[repet,r]*det(rhoInit1[,,r,repet])*exp(-0.5*dotProduct)
58 sumGamI = sum(Gam[i,])
59 gamInit1[i,,repet]= Gam[i,] / sumGamI
65 new_EMG = .Call("EMGLLF_core",phiInit1[,,,repet],rhoInit1[,,,repet],piInit1[repet,],
66 gamInit1[,,repet],miniInit,maxiInit,1,0,X,Y,1e-4)
67 LLFEessai = new_EMG$LLF
68 LLFinit1[repet] = LLFEessai[length(LLFEessai)]
71 b = which.max(LLFinit1)
72 phiInit = phiInit1[,,,b]
73 rhoInit = rhoInit1[,,,b]
75 gamInit = gamInit1[,,b]
77 return (list(phiInit=phiInit, rhoInit=rhoInit, piInit=piInit, gamInit=gamInit))