X-Git-Url: https://git.auder.net/?p=valse.git;a=blobdiff_plain;f=pkg%2FR%2FEMGrank.R;h=9531ae41fb6f369c672f8723a21506d0e80cb064;hp=09171ac56976a72b5e10af0bdd863ade4003150d;hb=3921ba9b5ea85bcc190245ac7da9ee9da1658b9f;hpb=e9db79707709c10947e89756eb5655c0747a2a1d diff --git a/pkg/R/EMGrank.R b/pkg/R/EMGrank.R index 09171ac..9531ae4 100644 --- a/pkg/R/EMGrank.R +++ b/pkg/R/EMGrank.R @@ -1,6 +1,6 @@ #' EMGrank #' -#' Run an generalized EM algorithm developped for mixture of Gaussian regression +#' Run an generalized EM algorithm developped for mixture of Gaussian regression #' models with variable selection by an extension of the low rank estimator. #' Reparametrization is done to ensure invariance by homothetic transformation. #' It returns a collection of models, varying the number of clusters and the rank of the regression mean. @@ -13,13 +13,14 @@ #' @param Y matrix of responses (of size n*m) #' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4 #' @param rank vector of possible ranks +#' @param fast boolean to enable or not the C function call #' #' @return A list (corresponding to the model collection) defined by (phi,LLF): #' phi : regression mean for each cluster #' LLF : log likelihood with respect to the training set #' #' @export -EMGrank <- function(Pi, Rho, mini, maxi, X, Y, eps, rank, fast = TRUE) +EMGrank <- function(Pi, Rho, mini, maxi, X, Y, eps, rank, fast) { if (!fast) { @@ -28,21 +29,16 @@ EMGrank <- function(Pi, Rho, mini, maxi, X, Y, eps, rank, fast = TRUE) } # Function in C - n <- nrow(X) #nombre d'echantillons - p <- ncol(X) #nombre de covariables - m <- ncol(Y) #taille de Y (multivarié) - k <- length(Pi) #nombre de composantes dans le mélange - .Call("EMGrank", Pi, Rho, mini, maxi, X, Y, eps, as.integer(rank), phi = double(p * m * k), - LLF = double(1), n, p, m, k, PACKAGE = "valse") + .Call("EMGrank", Pi, Rho, mini, maxi, X, Y, eps, as.integer(rank), PACKAGE = "valse") } # helper to always have matrices as arg (TODO: put this elsewhere? improve?) --> # Yes, we should use by-columns storage everywhere... [later!] matricize <- function(X) { - if (!is.matrix(X)) + if (!is.matrix(X)) return(t(as.matrix(X))) - return(X) + X } # R version - slow but easy to read @@ -73,15 +69,15 @@ matricize <- function(X) for (r in 1:k) { Z_indice <- seq_len(n)[Z == r] #indices where Z == r - if (length(Z_indice) == 0) + if (length(Z_indice) == 0) next # U,S,V = SVD of (t(Xr)Xr)^{-1} * t(Xr) * Yr - s <- svd(MASS::ginv(crossprod(matricize(X[Z_indice, ]))) %*% - crossprod(matricize(X[Z_indice, ]), matricize(Y[Z_indice, ]))) + s <- svd(MASS::ginv(crossprod(matricize(X[Z_indice, ]))) %*% + crossprod(matricize(X[Z_indice, ]), matricize(Y[Z_indice, ]))) S <- s$d # Set m-rank(r) singular values to zero, and recompose best rank(r) approximation # of the initial product - if (rank[r] < length(S)) + if (rank[r] < length(S)) S[(rank[r] + 1):length(S)] <- 0 phi[, , r] <- s$u %*% diag(S) %*% t(s$v) %*% Rho[, , r] } @@ -111,7 +107,7 @@ matricize <- function(X) # update distance parameter to check algorithm convergence (delta(phi, Phi)) deltaPhi <- c(deltaPhi, max((abs(phi - Phi))/(1 + abs(phi)))) #TODO: explain? - if (length(deltaPhi) > deltaPhiBufferSize) + if (length(deltaPhi) > deltaPhiBufferSize) deltaPhi <- deltaPhi[2:length(deltaPhi)] sumDeltaPhi <- sum(abs(deltaPhi)) @@ -119,5 +115,5 @@ matricize <- function(X) Phi <- phi ite <- ite + 1 } - return(list(phi = phi, LLF = LLF)) + list(phi = phi, LLF = LLF) }