X-Git-Url: https://git.auder.net/?p=valse.git;a=blobdiff_plain;f=pkg%2FR%2FconstructionModelesLassoRank.R;h=a37a7a6b79ea4d7f6721daedf712774177bad6b6;hp=5da26e3594573ee7df9a31e9fe08c19e9eb121f2;hb=6af1d4897dbab92a7be05068e0e15823378965d9;hpb=43d76c49d2f98490abc782c7e8a8b94baee40247 diff --git a/pkg/R/constructionModelesLassoRank.R b/pkg/R/constructionModelesLassoRank.R index 5da26e3..a37a7a6 100644 --- a/pkg/R/constructionModelesLassoRank.R +++ b/pkg/R/constructionModelesLassoRank.R @@ -1,7 +1,7 @@ #' constructionModelesLassoRank #' #' Construct a collection of models with the Lasso-Rank procedure. -#' +#' #' @param S output of selectVariables.R #' @param k number of components #' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10 @@ -14,82 +14,86 @@ #' @param ncores Number of cores, by default = 3 #' @param fast TRUE to use compiled C code, FALSE for R code only #' @param verbose TRUE to show some execution traces -#' -#' @return a list with several models, defined by phi, rho, pi, llh +#' +#' @return a list with several models, defined by phi (the regression parameter reparametrized), +#' rho (the covariance parameter reparametrized), pi (the proportion parameter is the mixture model), llh +#' (the value of the loglikelihood function for this estimator on the training dataset). The list is given +#' for several levels of sparsity, given by several regularization parameters computed automatically, +#' and several ranks (between rank.min and rank.max). #' #' @export -constructionModelesLassoRank = function(S, k, mini, maxi, X, Y, eps, rank.min, - rank.max, ncores, fast=TRUE, verbose=FALSE) +constructionModelesLassoRank <- function(S, k, mini, maxi, X, Y, eps, rank.min, rank.max, + ncores, fast, verbose) { - n = dim(X)[1] - p = dim(X)[2] - m = dim(Y)[2] - L = length(S) - + n <- nrow(X) + p <- ncol(X) + m <- ncol(Y) + L <- length(S) + # Possible interesting ranks - deltaRank = rank.max - rank.min + 1 - Size = deltaRank^k - RankLambda = matrix(0, nrow=Size*L, ncol=k+1) + deltaRank <- rank.max - rank.min + 1 + Size <- deltaRank^k + RankLambda <- matrix(0, nrow = Size * L, ncol = k + 1) for (r in 1:k) { - # On veut le tableau de toutes les combinaisons de rangs possibles, et des lambdas - # Dans la première colonne : on répète (rank.max-rank.min)^(k-1) chaque chiffre : - # ça remplit la colonne - # Dans la deuxieme : on répète (rank.max-rank.min)^(k-2) chaque chiffre, - # et on fait ça (rank.max-rank.min)^2 fois - # ... - # Dans la dernière, on répète chaque chiffre une fois, - # et on fait ça (rank.min-rank.max)^(k-1) fois. - RankLambda[,r] = rep(rank.min + rep(0:(deltaRank-1), deltaRank^(r-1), each=deltaRank^(k-r)), each = L) + # On veut le tableau de toutes les combinaisons de rangs possibles, et des + # lambdas Dans la premiere colonne : on repete (rank.max-rank.min)^(k-1) chaque + # chiffre : ca remplit la colonne Dans la deuxieme : on repete + # (rank.max-rank.min)^(k-2) chaque chiffre, et on fait ca (rank.max-rank.min)^2 + # fois ... Dans la derniere, on repete chaque chiffre une fois, et on fait ca + # (rank.min-rank.max)^(k-1) fois. + RankLambda[, r] <- rep(rank.min + rep(0:(deltaRank - 1), deltaRank^(r - 1), + each = deltaRank^(k - r)), each = L) } - RankLambda[,k+1] = rep(1:L, times = Size) - + RankLambda[, k + 1] <- rep(1:L, times = Size) + if (ncores > 1) { - cl = parallel::makeCluster(ncores, outfile='') - parallel::clusterExport( cl, envir=environment(), - varlist=c("A1","Size","Pi","Rho","mini","maxi","X","Y","eps", - "Rank","m","phi","ncores","verbose") ) + cl <- parallel::makeCluster(ncores, outfile = "") + parallel::clusterExport(cl, envir = environment(), varlist = c("A1", "Size", + "Pi", "Rho", "mini", "maxi", "X", "Y", "eps", "Rank", "m", "phi", "ncores", + "verbose")) } - + computeAtLambda <- function(index) { - lambdaIndex = RankLambda[index,k+1] - rankIndex = RankLambda[index,1:k] + lambdaIndex <- RankLambda[index, k + 1] + rankIndex <- RankLambda[index, 1:k] if (ncores > 1) - require("valse") #workers start with an empty environment - + require("valse") #workers start with an empty environment + # 'relevant' will be the set of relevant columns - selected = S[[lambdaIndex]]$selected - relevant = c() - for (j in 1:p){ - if (length(selected[[j]])>0){ - relevant = c(relevant,j) - } + selected <- S[[lambdaIndex]]$selected + relevant <- c() + for (j in 1:p) + { + if (length(selected[[j]]) > 0) + relevant <- c(relevant, j) } - if (max(rankIndex) 0) { - res = EMGrank(S[[lambdaIndex]]$Pi, S[[lambdaIndex]]$Rho, mini, maxi, - X[,relevant], Y, eps, rankIndex, fast) - llh = c( res$LLF, sum(rankIndex * (length(relevant)- rankIndex + m)) ) - phi[relevant,,] = res$phi + res <- EMGrank(S[[lambdaIndex]]$Pi, S[[lambdaIndex]]$Rho, mini, maxi, + X[, relevant], Y, eps, rankIndex, fast) + llh <- c(res$LLF, sum(rankIndex * (length(relevant) - rankIndex + m))) + phi[relevant, , ] <- res$phi } - list("llh"=llh, "phi"=phi, "pi" = S[[lambdaIndex]]$Pi, "rho" = S[[lambdaIndex]]$Rho) - + list(llh = llh, phi = phi, pi = S[[lambdaIndex]]$Pi, rho = S[[lambdaIndex]]$Rho) } } - - #For each lambda in the grid we compute the estimators - out = - if (ncores > 1) - parLapply(cl, seq_len(length(S)*Size), computeAtLambda) - else - lapply(seq_len(length(S)*Size), computeAtLambda) - + + # For each lambda in the grid we compute the estimators + out <- + if (ncores > 1) { + parallel::parLapply(cl, seq_len(length(S) * Size), computeAtLambda) + } else { + lapply(seq_len(length(S) * Size), computeAtLambda) + } + if (ncores > 1) parallel::stopCluster(cl) - + out }