several modifs - pkg looks better (but untested)
[valse.git] / pkg / R / main.R
CommitLineData
086ca318
BA
1#' valse
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 rang.min integer, minimum rank in the low rank procedure, by default = 1
16#' @param rang.max integer, maximum rank in the
17#'
18#' @return a list with estimators of parameters
19#'
20#' @examples
21#' #TODO: a few examples
22#' @export
2279a641
BA
23valse = function(X, Y, procedure='LassoMLE', selecMod='DDSE', gamma=1, mini=10, maxi=50,
24 eps=1e-4, kmin=2, kmax=2, rang.min=1, rang.max=10, ncores_outer=1, ncores_inner=3,
25 verbose=FALSE)
086ca318 26{
086ca318
BA
27 p = dim(X)[2]
28 m = dim(Y)[2]
29 n = dim(X)[1]
4cc632c9 30
4cc632c9
BA
31 if (verbose)
32 print("main loop: over all k and all lambda")
33
2279a641 34 if (ncores_outer > 1)
086ca318 35 {
2279a641 36 cl = parallel::makeCluster(ncores_outer)
4cc632c9
BA
37 parallel::clusterExport( cl=cl, envir=environment(), varlist=c("X","Y","procedure",
38 "selecMod","gamma","mini","maxi","eps","kmin","kmax","rang.min","rang.max",
2279a641 39 "ncores_outer","ncores_inner","verbose","p","m","k","tableauRecap") )
4cc632c9
BA
40 }
41
0eb161e3
BA
42 # Compute models with k components
43 computeModels <- function(k)
4cc632c9 44 {
2279a641 45 if (ncores_outer > 1)
4cc632c9
BA
46 require("valse") #nodes start with an empty environment
47
48 if (verbose)
49 print(paste("Parameters initialization for k =",k))
0eb161e3 50 #smallEM initializes parameters by k-means and regression model in each component,
086ca318
BA
51 #doing this 20 times, and keeping the values maximizing the likelihood after 10
52 #iterations of the EM algorithm.
4cc632c9
BA
53 P = initSmallEM(k, X, Y)
54 grid_lambda <- computeGridLambda(P$phiInit, P$rhoInit, P$piInit, P$gamInit, X, Y,
55 gamma, mini, maxi, eps)
4cc632c9 56 # TODO: 100 = magic number
086ca318
BA
57 if (length(grid_lambda)>100)
58 grid_lambda = grid_lambda[seq(1, length(grid_lambda), length.out = 100)]
4cc632c9
BA
59
60 if (verbose)
61 print("Compute relevant parameters")
086ca318 62 #select variables according to each regularization parameter
0eb161e3
BA
63 #from the grid: S$selected corresponding to selected variables
64 S = selectVariables(P$phiInit, P$rhoInit, P$piInit, P$gamInit, mini, maxi, gamma,
65 grid_lambda, X, Y, 1e-8, eps, ncores_inner) #TODO: 1e-8 as arg?! eps?
4cc632c9 66
086ca318 67 if (procedure == 'LassoMLE')
39046da6 68 {
4cc632c9
BA
69 if (verbose)
70 print('run the procedure Lasso-MLE')
086ca318
BA
71 #compute parameter estimations, with the Maximum Likelihood
72 #Estimator, restricted on selected variables.
0eb161e3 73 models <- constructionModelesLassoMLE(phiInit, rhoInit, piInit, gamInit, mini,
2279a641 74 maxi, gamma, X, Y, thresh, eps, S$selected, ncores_inner, verbose)
086ca318
BA
75 }
76 else
39046da6 77 {
4cc632c9
BA
78 if (verbose)
79 print('run the procedure Lasso-Rank')
086ca318
BA
80 #compute parameter estimations, with the Low Rank
81 #Estimator, restricted on selected variables.
0eb161e3 82 models <- constructionModelesLassoRank(S$Pi, S$Rho, mini, maxi, X, Y, eps, A1,
2279a641 83 rank.min, rank.max, ncores_inner, verbose)
086ca318 84 }
0eb161e3 85 models
086ca318 86 }
4cc632c9 87
0eb161e3
BA
88 # List (index k) of lists (index lambda) of models
89 models_list <-
4cc632c9 90 if (ncores_k > 1)
0eb161e3 91 parLapply(cl, kmin:kmax, computeModels)
4cc632c9 92 else
0eb161e3 93 lapply(kmin:kmax, computeModels)
4cc632c9
BA
94 if (ncores_k > 1)
95 parallel::stopCluster(cl)
96
0eb161e3
BA
97 if (! requireNamespace("capushe", quietly=TRUE))
98 {
99 warning("'capushe' not available: returning all models")
100 return (models_list)
101 }
102
103 # Get summary "tableauRecap" from models ; TODO: jusqu'à ligne 114 à mon avis là c'est faux :/
104 tableauRecap = t( sapply( models_list, function(models) {
105 llh = do.call(rbind, lapply(models, function(model) model$llh)
2279a641
BA
106 LLH = llh[-1,1]
107 D = llh[-1,2]
108 c(LLH, D, rep(k, length(model)), 1:length(model))
0eb161e3 109 ) } ) )
4cc632c9
BA
110 if (verbose)
111 print('Model selection')
086ca318 112 tableauRecap = tableauRecap[rowSums(tableauRecap[, 2:4])!=0,]
0eb161e3 113 tableauRecap = tableauRecap[!is.infinite(tableauRecap[,1]),]
086ca318 114 data = cbind(1:dim(tableauRecap)[1], tableauRecap[,2], tableauRecap[,2], tableauRecap[,1])
4cc632c9 115
0eb161e3 116 modSel = capushe::capushe(data, n)
086ca318
BA
117 indModSel <-
118 if (selecMod == 'DDSE')
119 as.numeric(modSel@DDSE@model)
120 else if (selecMod == 'Djump')
121 as.numeric(modSel@Djump@model)
122 else if (selecMod == 'BIC')
123 modSel@BIC_capushe$model
124 else if (selecMod == 'AIC')
125 modSel@AIC_capushe$model
126 model[[tableauRecap[indModSel,3]]][[tableauRecap[indModSel,4]]]
127}