Commit | Line | Data |
---|---|---|
2279a641 BA |
1 | #' constructionModelesLassoRank |
2 | #' | |
43d76c49 | 3 | #' Construct a collection of models with the Lasso-Rank procedure. |
4464301b | 4 | #' |
43d76c49 | 5 | #' @param S output of selectVariables.R |
6 | #' @param k number of components | |
7 | #' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10 | |
8 | #' @param maxi integer, maximum number of iterations in the EM algorithm, by default = 100 | |
9 | #' @param X matrix of covariates (of size n*p) | |
10 | #' @param Y matrix of responses (of size n*m) | |
11 | #' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4 | |
12 | #' @param rank.min integer, minimum rank in the low rank procedure, by default = 1 | |
13 | #' @param rank.max integer, maximum rank in the low rank procedure, by default = 5 | |
14 | #' @param ncores Number of cores, by default = 3 | |
15 | #' @param fast TRUE to use compiled C code, FALSE for R code only | |
16 | #' @param verbose TRUE to show some execution traces | |
17 | #' | |
18 | #' @return a list with several models, defined by phi, rho, pi, llh | |
2279a641 | 19 | #' |
43d76c49 | 20 | #' @export |
7a56cc18 BA |
21 | constructionModelesLassoRank <- function(S, k, mini, maxi, X, Y, eps, rank.min, rank.max, |
22 | ncores, fast = TRUE, verbose = FALSE) | |
7d0865a1 | 23 | { |
7a56cc18 BA |
24 | n <- dim(X)[1] |
25 | p <- dim(X)[2] | |
26 | m <- dim(Y)[2] | |
27 | L <- length(S) | |
7d0865a1 | 28 | |
43d76c49 | 29 | # Possible interesting ranks |
7a56cc18 BA |
30 | deltaRank <- rank.max - rank.min + 1 |
31 | Size <- deltaRank^k | |
32 | RankLambda <- matrix(0, nrow = Size * L, ncol = k + 1) | |
0eb161e3 | 33 | for (r in 1:k) |
43d76c49 | 34 | { |
7a56cc18 BA |
35 | # On veut le tableau de toutes les combinaisons de rangs possibles, et des |
36 | # lambdas Dans la première colonne : on répète (rank.max-rank.min)^(k-1) chaque | |
37 | # chiffre : ça remplit la colonne Dans la deuxieme : on répète | |
38 | # (rank.max-rank.min)^(k-2) chaque chiffre, et on fait ça (rank.max-rank.min)^2 | |
39 | # fois ... Dans la dernière, on répète chaque chiffre une fois, et on fait ça | |
40 | # (rank.min-rank.max)^(k-1) fois. | |
41 | RankLambda[, r] <- rep(rank.min + rep(0:(deltaRank - 1), deltaRank^(r - 1), | |
42 | each = deltaRank^(k - r)), each = L) | |
ef67d338 | 43 | } |
7a56cc18 | 44 | RankLambda[, k + 1] <- rep(1:L, times = Size) |
7d0865a1 | 45 | |
0eb161e3 | 46 | if (ncores > 1) |
43d76c49 | 47 | { |
7a56cc18 BA |
48 | cl <- parallel::makeCluster(ncores, outfile = "") |
49 | parallel::clusterExport(cl, envir = environment(), varlist = c("A1", "Size", | |
50 | "Pi", "Rho", "mini", "maxi", "X", "Y", "eps", "Rank", "m", "phi", "ncores", | |
51 | "verbose")) | |
43d76c49 | 52 | } |
7d0865a1 | 53 | |
43d76c49 | 54 | computeAtLambda <- function(index) |
55 | { | |
7a56cc18 BA |
56 | lambdaIndex <- RankLambda[index, k + 1] |
57 | rankIndex <- RankLambda[index, 1:k] | |
58 | if (ncores > 1) | |
59 | require("valse") #workers start with an empty environment | |
7d0865a1 | 60 | |
43d76c49 | 61 | # 'relevant' will be the set of relevant columns |
7a56cc18 BA |
62 | selected <- S[[lambdaIndex]]$selected |
63 | relevant <- c() | |
64 | for (j in 1:p) | |
65 | { | |
66 | if (length(selected[[j]]) > 0) | |
67 | { | |
68 | relevant <- c(relevant, j) | |
3f145e9a BG |
69 | } |
70 | } | |
7a56cc18 BA |
71 | if (max(rankIndex) < length(relevant)) |
72 | { | |
73 | phi <- array(0, dim = c(p, m, k)) | |
43d76c49 | 74 | if (length(relevant) > 0) |
75 | { | |
7a56cc18 BA |
76 | res <- EMGrank(S[[lambdaIndex]]$Pi, S[[lambdaIndex]]$Rho, mini, maxi, |
77 | X[, relevant], Y, eps, rankIndex, fast) | |
78 | llh <- c(res$LLF, sum(rankIndex * (length(relevant) - rankIndex + | |
79 | m))) | |
80 | phi[relevant, , ] <- res$phi | |
43d76c49 | 81 | } |
7a56cc18 | 82 | list(llh = llh, phi = phi, pi = S[[lambdaIndex]]$Pi, rho = S[[lambdaIndex]]$Rho) |
43d76c49 | 83 | |
84 | } | |
85 | } | |
7d0865a1 | 86 | |
7a56cc18 | 87 | # For each lambda in the grid we compute the estimators |
7d0865a1 BA |
88 | out <- if (ncores > 1) |
89 | { | |
90 | parLapply(cl, seq_len(length(S) * Size), computeAtLambda) | |
91 | } else | |
92 | { | |
93 | lapply(seq_len(length(S) * Size), computeAtLambda) | |
94 | } | |
95 | ||
7a56cc18 | 96 | if (ncores > 1) |
0eb161e3 | 97 | parallel::stopCluster(cl) |
7d0865a1 | 98 | |
43d76c49 | 99 | out |
9ade3f1b | 100 | } |