comment output
[valse.git] / pkg / R / main.R
CommitLineData
859c30ec 1#' runValse
3453829e
BA
2#'
3#' Main function
4#'
5#' @param X matrix of covariates (of size n*p)
6#' @param Y matrix of responses (of size n*m)
7#' @param procedure among 'LassoMLE' or 'LassoRank'
8#' @param selecMod method to select a model among 'DDSE', 'DJump', 'BIC' or 'AIC'
9#' @param gamma integer for the power in the penaly, by default = 1
10#' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10
11#' @param maxi integer, maximum number of iterations in the EM algorithm, by default = 100
12#' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4
13#' @param kmin integer, minimum number of clusters, by default = 2
14#' @param kmax integer, maximum number of clusters, by default = 10
15#' @param rank.min integer, minimum rank in the low rank procedure, by default = 1
16#' @param rank.max integer, maximum rank in the low rank procedure, by default = 5
17#' @param ncores_outer Number of cores for the outer loop on k
18#' @param ncores_inner Number of cores for the inner loop on lambda
19#' @param thresh real, threshold to say a variable is relevant, by default = 1e-8
20#' @param grid_lambda, a vector with regularization parameters if known, by default numeric(0)
75b96714 21#' @param size_coll_mod (Maximum) size of a collection of models, by default 50
3453829e
BA
22#' @param fast TRUE to use compiled C code, FALSE for R code only
23#' @param verbose TRUE to show some execution traces
1196a43d 24#' @param plot TRUE to plot the selected models after run
3453829e 25#'
fb3557f3 26#' @return
75b96714 27#' The selected model (except if 'DDSE' or 'DJump' is used to select a model and the collection of models
28#' has less than 11 models, the function returns the collection as it can not select one - in that case,
29#' it is adviced to use 'AIC' or 'BIC' to select a model)
3453829e
BA
30#'
31#' @examples
fb3557f3
BA
32#' n = 50; m = 10; p = 5
33#' beta = array(0, dim=c(p,m,2))
34#' beta[,,1] = 1
35#' beta[,,2] = 2
36#' data = generateXY(n, c(0.4,0.6), rep(0,p), beta, diag(0.5, p), diag(0.5, m))
37#' X = data$X
38#' Y = data$Y
39#' res = runValse(X, Y)
40#' X <- matrix(runif(100), nrow=50)
41#' Y <- matrix(runif(100), nrow=50)
42#' res = runValse(X, Y)
859c30ec 43#'
3453829e 44#' @export
859c30ec 45runValse <- function(X, Y, procedure = "LassoMLE", selecMod = "DDSE", gamma = 1, mini = 10,
0ba1b11c 46 maxi = 50, eps = 1e-04, kmin = 2, kmax = 3, rank.min = 1, rank.max = 5, ncores_outer = 1,
75b96714 47 ncores_inner = 1, thresh = 1e-08, grid_lambda = numeric(0), size_coll_mod = 50,
3453829e
BA
48 fast = TRUE, verbose = FALSE, plot = TRUE)
49{
50 n <- nrow(X)
51 p <- ncol(X)
52 m <- ncol(Y)
53
0ba1b11c 54 if (verbose)
3453829e
BA
55 print("main loop: over all k and all lambda")
56
57 if (ncores_outer > 1) {
58 cl <- parallel::makeCluster(ncores_outer, outfile = "")
0ba1b11c
BA
59 parallel::clusterExport(cl = cl, envir = environment(), varlist = c("X",
60 "Y", "procedure", "selecMod", "gamma", "mini", "maxi", "eps", "kmin",
61 "kmax", "rank.min", "rank.max", "ncores_outer", "ncores_inner", "thresh",
3453829e
BA
62 "size_coll_mod", "verbose", "p", "m"))
63 }
64
65 # Compute models with k components
66 computeModels <- function(k)
67 {
0ba1b11c 68 if (ncores_outer > 1)
3453829e
BA
69 require("valse") #nodes start with an empty environment
70
0ba1b11c 71 if (verbose)
3453829e
BA
72 print(paste("Parameters initialization for k =", k))
73 # smallEM initializes parameters by k-means and regression model in each
74 # component, doing this 20 times, and keeping the values maximizing the
75 # likelihood after 10 iterations of the EM algorithm.
76 P <- initSmallEM(k, X, Y, fast)
77 if (length(grid_lambda) == 0)
78 {
0ba1b11c 79 grid_lambda <- computeGridLambda(P$phiInit, P$rhoInit, P$piInit, P$gamInit,
3453829e
BA
80 X, Y, gamma, mini, maxi, eps, fast)
81 }
0ba1b11c 82 if (length(grid_lambda) > size_coll_mod)
3453829e
BA
83 grid_lambda <- grid_lambda[seq(1, length(grid_lambda), length.out = size_coll_mod)]
84
0ba1b11c 85 if (verbose)
3453829e
BA
86 print("Compute relevant parameters")
87 # select variables according to each regularization parameter from the grid:
88 # S$selected corresponding to selected variables
0ba1b11c 89 S <- selectVariables(P$phiInit, P$rhoInit, P$piInit, P$gamInit, mini, maxi,
3453829e
BA
90 gamma, grid_lambda, X, Y, thresh, eps, ncores_inner, fast)
91
92 if (procedure == "LassoMLE") {
0ba1b11c 93 if (verbose)
3453829e
BA
94 print("run the procedure Lasso-MLE")
95 # compute parameter estimations, with the Maximum Likelihood Estimator,
96 # restricted on selected variables.
0ba1b11c 97 models <- constructionModelesLassoMLE(P$phiInit, P$rhoInit, P$piInit,
3453829e
BA
98 P$gamInit, mini, maxi, gamma, X, Y, eps, S, ncores_inner, fast, verbose)
99 } else {
0ba1b11c 100 if (verbose)
3453829e
BA
101 print("run the procedure Lasso-Rank")
102 # compute parameter estimations, with the Low Rank Estimator, restricted on
103 # selected variables.
0ba1b11c 104 models <- constructionModelesLassoRank(S, k, mini, maxi, X, Y, eps, rank.min,
3453829e
BA
105 rank.max, ncores_inner, fast, verbose)
106 }
107 # warning! Some models are NULL after running selectVariables
108 models <- models[sapply(models, function(cell) !is.null(cell))]
109 models
110 }
111
112 # List (index k) of lists (index lambda) of models
113 models_list <-
114 if (ncores_outer > 1) {
115 parLapply(cl, kmin:kmax, computeModels)
116 } else {
117 lapply(kmin:kmax, computeModels)
118 }
0ba1b11c 119 if (ncores_outer > 1)
3453829e
BA
120 parallel::stopCluster(cl)
121
122 if (!requireNamespace("capushe", quietly = TRUE))
123 {
124 warning("'capushe' not available: returning all models")
125 return(models_list)
126 }
127
128 # Get summary 'tableauRecap' from models
129 tableauRecap <- do.call(rbind, lapply(seq_along(models_list), function(i)
130 {
131 models <- models_list[[i]]
132 # For a collection of models (same k, several lambda):
133 LLH <- sapply(models, function(model) model$llh[1])
134 k <- length(models[[1]]$pi)
0ba1b11c 135 sumPen <- sapply(models, function(model) k * (dim(model$rho)[1] + sum(model$phi[,
3453829e 136 , 1] != 0) + 1) - 1)
0ba1b11c 137 data.frame(model = paste(i, ".", seq_along(models), sep = ""), pen = sumPen/n,
3453829e
BA
138 complexity = sumPen, contrast = -LLH)
139 }))
140 tableauRecap <- tableauRecap[which(tableauRecap[, 4] != Inf), ]
fb3557f3 141 if (verbose)
3453829e 142 print(tableauRecap)
3453829e 143
fb3557f3
BA
144 if (nrow(tableauRecap) > 10) {
145 modSel <- capushe::capushe(tableauRecap, n)
146 indModSel <- if (selecMod == "DDSE")
147 {
148 as.numeric(modSel@DDSE@model)
149 } else if (selecMod == "Djump")
150 {
151 as.numeric(modSel@Djump@model)
152 } else if (selecMod == "BIC")
153 {
154 modSel@BIC_capushe$model
155 } else if (selecMod == "AIC")
156 {
157 modSel@AIC_capushe$model
158 }
159 listMod <- as.integer(unlist(strsplit(as.character(indModSel), "[.]")))
160 modelSel <- models_list[[listMod[1]]][[listMod[2]]]
161 modelSel$models <- tableauRecap
3453829e 162
fb3557f3
BA
163 if (plot)
164 print(plot_valse(X, Y, modelSel))
165 return(modelSel)
166 }
167 tableauRecap
3453829e 168}