Remove arg n in plot_valse (deduce from X)
[valse.git] / pkg / R / EMGLLF.R
index 6ee7ba7..1633821 100644 (file)
@@ -1,6 +1,9 @@
-#' EMGLLF 
+#' EMGLLF
 #'
-#' Description de EMGLLF
+#' Run a generalized EM algorithm developped for mixture of Gaussian regression
+#' models with variable selection by an extension of the Lasso estimator (regularization parameter lambda).
+#' Reparametrization is done to ensure invariance by homothetic transformation.
+#' It returns a collection of models, varying the number of clusters and the sparsity in the regression mean.
 #'
 #' @param phiInit an initialization for phi
 #' @param rhoInit an initialization for rho
 #' @param X matrix of covariates (of size n*p)
 #' @param Y matrix of responses (of size n*m)
 #' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4
+#' @param fast boolean to enable or not the C function call
 #'
-#' @return A list ... phi,rho,pi,LLF,S,affec:
-#'   phi : parametre de moyenne renormalisé, calculé par l'EM
-#'   rho : parametre de variance renormalisé, calculé par l'EM
-#'   pi : parametre des proportions renormalisé, calculé par l'EM
-#'   LLF : log vraisemblance associée à cet échantillon, pour les valeurs estimées des paramètres
-#'   S : ... affec : ...
+#' @return A list (corresponding to the model collection) defined by (phi,rho,pi,LLF,S,affec):
+#'   phi : regression mean for each cluster
+#'   rho : variance (homothetic) for each cluster
+#'   pi : proportion for each cluster
+#'   LLF : log likelihood with respect to the training set
+#'   S : selected variables indexes
+#'   affec : cluster affectation for each observation (of the training set)
 #'
 #' @export
-EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, 
+EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
   X, Y, eps, fast)
 {
   if (!fast)
   {
     # Function in R
-    return(.EMGLLF_R(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, 
+    return(.EMGLLF_R(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
       X, Y, eps))
   }
 
   # Function in C
-  n <- nrow(X)  #nombre d'echantillons
-  p <- ncol(X)  #nombre de covariables
-  m <- ncol(Y)  #taille de Y (multivarié)
-  k <- length(piInit)  #nombre de composantes dans le mélange
-  .Call("EMGLLF", phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, 
-    X, Y, eps, phi = double(p * m * k), rho = double(m * m * k), pi = double(k), 
-    LLF = double(maxi), S = double(p * m * k), affec = integer(n), n, p, m, k, 
-    PACKAGE = "valse")
+  .Call("EMGLLF", phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
+    X, Y, eps, PACKAGE = "valse")
 }
 
 # R version - slow but easy to read
-.EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda, 
+.EMGLLF_R <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
   X, Y, eps)
 {
-  # Matrix dimensions: NOTE: phiInit *must* be an array (even if p==1)
-  n <- dim(Y)[1]
-  p <- dim(phiInit)[1]
-  m <- dim(phiInit)[2]
-  k <- dim(phiInit)[3]
+  # Matrix dimensions
+  n <- nrow(X)
+  p <- ncol(X)
+  m <- ncol(Y)
+  k <- length(piInit)
+
+  # Adjustments required when p==1 or m==1 (var.sel. or output dim 1)
+  if (p==1 || m==1)
+    phiInit <- array(phiInit, dim=c(p,m,k))
+  if (m==1)
+    rhoInit <- array(rhoInit, dim=c(m,m,k))
 
   # Outputs
-  phi <- array(NA, dim = c(p, m, k))
-  phi[1:p, , ] <- phiInit
+  phi <- phiInit
   rho <- rhoInit
   pi <- piInit
   llh <- -Inf
@@ -67,7 +71,6 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
   ps2 <- array(0, dim = c(p, m, k))
   X2 <- array(0, dim = c(n, p, k))
   Y2 <- array(0, dim = c(n, m, k))
-  EPS <- 1e-15
 
   for (ite in 1:maxi)
   {
@@ -155,7 +158,7 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
     ## E step
 
     # Precompute det(rho[,,r]) for r in 1...k
-    detRho <- sapply(1:k, function(r) det(rho[, , r]))
+    detRho <- sapply(1:k, function(r) gdet(rho[, , r]))
     sumLogLLH <- 0
     for (i in 1:n)
     {
@@ -174,7 +177,7 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
 
     sumPen <- sum(pi^gamma * b)
     last_llh <- llh
-    llh <- -sumLogLLH/n + lambda * sumPen
+    llh <- -sumLogLLH/n #+ lambda * sumPen
     dist <- ifelse(ite == 1, llh, (llh - last_llh)/(1 + abs(llh)))
     Dist1 <- max((abs(phi - Phi))/(1 + abs(phi)))
     Dist2 <- max((abs(rho - Rho))/(1 + abs(rho)))
@@ -185,5 +188,6 @@ EMGLLF <- function(phiInit, rhoInit, piInit, gamInit, mini, maxi, gamma, lambda,
       break
   }
 
-  list(phi = phi, rho = rho, pi = pi, llh = llh, S = S)
+       affec = apply(gam, 1, which.max)
+  list(phi = phi, rho = rho, pi = pi, llh = llh, S = S, affec=affec)
 }