fix for m==1
[valse.git] / pkg / R / constructionModelesLassoMLE.R
CommitLineData
ffdf9447 1#' constructionModelesLassoMLE
2279a641 2#'
5965d116 3#' Construct a collection of models with the Lasso-MLE procedure.
4#'
43d76c49 5#' @param phiInit an initialization for phi, get by initSmallEM.R
6#' @param rhoInit an initialization for rho, get by initSmallEM.R
7#' @param piInit an initialization for pi, get by initSmallEM.R
8#' @param gamInit an initialization for gam, get by initSmallEM.R
9#' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10
10#' @param maxi integer, maximum number of iterations in the EM algorithm, by default = 100
11#' @param gamma integer for the power in the penaly, by default = 1
12#' @param X matrix of covariates (of size n*p)
13#' @param Y matrix of responses (of size n*m)
14#' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4
15#' @param S output of selectVariables.R
16#' @param ncores Number of cores, by default = 3
17#' @param fast TRUE to use compiled C code, FALSE for R code only
18#' @param verbose TRUE to show some execution traces
19#'
20#' @return a list with several models, defined by phi, rho, pi, llh
2279a641 21#'
43d76c49 22#' @export
ffdf9447 23constructionModelesLassoMLE <- function(phiInit, rhoInit, piInit, gamInit, mini,
a3cbbaea 24 maxi, gamma, X, Y, eps, S, ncores = 3, fast, verbose)
1b698c16 25{
ffdf9447
BA
26 if (ncores > 1)
27 {
28 cl <- parallel::makeCluster(ncores, outfile = "")
29 parallel::clusterExport(cl, envir = environment(), varlist = c("phiInit",
30 "rhoInit", "gamInit", "mini", "maxi", "gamma", "X", "Y", "eps", "S",
31 "ncores", "fast", "verbose"))
32 }
1b698c16 33
ffdf9447
BA
34 # Individual model computation
35 computeAtLambda <- function(lambda)
36 {
37 if (ncores > 1)
38 require("valse") #nodes start with an empty environment
1b698c16 39
ffdf9447
BA
40 if (verbose)
41 print(paste("Computations for lambda=", lambda))
1b698c16 42
ea5860f1
BA
43 n <- nrow(X)
44 p <- ncol(X)
45 m <- ncol(Y)
46 k <- length(piInit)
ffdf9447
BA
47 sel.lambda <- S[[lambda]]$selected
48 # col.sel = which(colSums(sel.lambda)!=0) #if boolean matrix
49 col.sel <- which(sapply(sel.lambda, length) > 0) #if list of selected vars
50 if (length(col.sel) == 0)
51 return(NULL)
1b698c16 52
ffdf9447 53 # lambda == 0 because we compute the EMV: no penalization here
ea5860f1
BA
54 res <- EMGLLF(array(phiInit,dim=c(p,m,k))[col.sel, , ], rhoInit, piInit, gamInit,
55 mini, maxi, gamma, 0, as.matrix(X[, col.sel]), Y, eps, fast)
1b698c16 56
ffdf9447
BA
57 # Eval dimension from the result + selected
58 phiLambda2 <- res$phi
59 rhoLambda <- res$rho
60 piLambda <- res$pi
61 phiLambda <- array(0, dim = c(p, m, k))
1b698c16
BA
62 for (j in seq_along(col.sel))
63 phiLambda[col.sel[j], sel.lambda[[j]], ] <- phiLambda2[j, sel.lambda[[j]], ]
ffdf9447 64 dimension <- length(unlist(sel.lambda))
1b698c16 65
ffdf9447
BA
66 # Computation of the loglikelihood
67 densite <- vector("double", n)
68 for (r in 1:k)
69 {
70 if (length(col.sel) == 1)
71 {
1b698c16
BA
72 delta <- (Y %*% rhoLambda[, , r] - (X[, col.sel] %*% t(phiLambda[col.sel, , r])))
73 } else delta <- (Y %*% rhoLambda[, , r] - (X[, col.sel] %*% phiLambda[col.sel, , r]))
ea5860f1 74 densite <- densite + piLambda[r] * gdet(rhoLambda[, , r])/(sqrt(2 * base::pi))^m *
96b591b7 75 exp(-diag(tcrossprod(delta))/2)
ffdf9447
BA
76 }
77 llhLambda <- c(sum(log(densite)), (dimension + m + 1) * k - 1)
78 list(phi = phiLambda, rho = rhoLambda, pi = piLambda, llh = llhLambda)
79 }
1b698c16 80
ffdf9447 81 # For each lambda, computation of the parameters
1b698c16
BA
82 out <-
83 if (ncores > 1) {
84 parLapply(cl, 1:length(S), computeAtLambda)
85 } else {
86 lapply(1:length(S), computeAtLambda)
87 }
88
ffdf9447
BA
89 if (ncores > 1)
90 parallel::stopCluster(cl)
1b698c16 91
ffdf9447 92 out
c3bc4705 93}