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)
6 #' @param tau threshold to stop EM algorithm
8 #' @return a list with phiInit, rhoInit, piInit, gamInit
10 initSmallEM = function(k,X,Y,tau)
16 Zinit1 = array(0, dim=c(n,20))
17 betaInit1 = array(0, dim=c(p,m,k,20))
18 sigmaInit1 = array(0, dim = c(m,m,k,20))
19 phiInit1 = array(0, dim = c(p,m,k,20))
20 rhoInit1 = array(0, dim = c(m,m,k,20))
22 piInit1 = matrix(0,20,k)
23 gamInit1 = array(0, dim=c(n,k,20))
26 require(MASS) #Moore-Penrose generalized inverse of matrix
29 distance_clus = dist(X)
30 tree_hier = hclust(distance_clus)
31 Zinit1[,repet] = cutree(tree_hier, k)
37 Z_vec = Z_bin$vec #vecteur 0 et 1 aux endroits o? Z==r
38 Z_indice = Z_bin$indice #renvoit les indices o? Z==r
40 betaInit1[,,r,repet] = ginv( crossprod(X[Z_indice,]) ) %*% crossprod(X[Z_indice,], Y[Z_indice,])
41 sigmaInit1[,,r,repet] = diag(m)
42 phiInit1[,,r,repet] = betaInit1[,,r,repet] #/ sigmaInit1[,,r,repet]
43 rhoInit1[,,r,repet] = solve(sigmaInit1[,,r,repet])
44 piInit1[repet,r] = sum(Z_vec)/n
51 dotProduct = tcrossprod(Y[i,]%*%rhoInit1[,,r,repet]-X[i,]%*%phiInit1[,,r,repet])
52 Gam[i,r] = piInit1[repet,r]*det(rhoInit1[,,r,repet])*exp(-0.5*dotProduct)
54 sumGamI = sum(Gam[i,])
55 gamInit1[i,,repet]= Gam[i,] / sumGamI
61 new_EMG = .Call("EMGLLF_core",phiInit1[,,,repet],rhoInit1[,,,repet],piInit1[repet,],gamInit1[,,repet],miniInit,maxiInit,1,0,X,Y,tau)
62 LLFEessai = new_EMG$LLF
63 LLFinit1[repet] = LLFEessai[length(LLFEessai)]
66 b = which.max(LLFinit1)
67 phiInit = phiInit1[,,,b]
68 rhoInit = rhoInit1[,,,b]
70 gamInit = gamInit1[,,b]
72 return (list(phiInit=phiInit, rhoInit=rhoInit, piInit=piInit, gamInit=gamInit))