Commit | Line | Data |
---|---|---|
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 |
23 | valse = 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 BA |
30 | |
31 | tableauRecap = list() | |
32 | if (verbose) | |
33 | print("main loop: over all k and all lambda") | |
34 | ||
2279a641 | 35 | if (ncores_outer > 1) |
086ca318 | 36 | { |
2279a641 | 37 | cl = parallel::makeCluster(ncores_outer) |
4cc632c9 BA |
38 | parallel::clusterExport( cl=cl, envir=environment(), varlist=c("X","Y","procedure", |
39 | "selecMod","gamma","mini","maxi","eps","kmin","kmax","rang.min","rang.max", | |
2279a641 | 40 | "ncores_outer","ncores_inner","verbose","p","m","k","tableauRecap") ) |
4cc632c9 BA |
41 | } |
42 | ||
43 | # Compute model with k components | |
44 | computeModel <- function(k) | |
45 | { | |
2279a641 | 46 | if (ncores_outer > 1) |
4cc632c9 BA |
47 | require("valse") #nodes start with an empty environment |
48 | ||
49 | if (verbose) | |
50 | print(paste("Parameters initialization for k =",k)) | |
086ca318 BA |
51 | #smallEM initializes parameters by k-means and regression model in each component, |
52 | #doing this 20 times, and keeping the values maximizing the likelihood after 10 | |
53 | #iterations of the EM algorithm. | |
4cc632c9 BA |
54 | P = initSmallEM(k, X, Y) |
55 | grid_lambda <- computeGridLambda(P$phiInit, P$rhoInit, P$piInit, P$gamInit, X, Y, | |
56 | gamma, mini, maxi, eps) | |
57 | ||
58 | # TODO: 100 = magic number | |
086ca318 BA |
59 | if (length(grid_lambda)>100) |
60 | grid_lambda = grid_lambda[seq(1, length(grid_lambda), length.out = 100)] | |
4cc632c9 BA |
61 | |
62 | if (verbose) | |
63 | print("Compute relevant parameters") | |
086ca318 BA |
64 | #select variables according to each regularization parameter |
65 | #from the grid: A1 corresponding to selected variables, and | |
66 | #A2 corresponding to unselected variables. | |
4cc632c9 | 67 | S = selectVariables(P$phiInit,P$rhoInit,P$piInit,P$gamInit,mini,maxi,gamma, |
2279a641 | 68 | grid_lambda,X,Y,1e-8,eps,ncores_inner) |
4cc632c9 | 69 | |
086ca318 | 70 | if (procedure == 'LassoMLE') |
39046da6 | 71 | { |
4cc632c9 BA |
72 | if (verbose) |
73 | print('run the procedure Lasso-MLE') | |
086ca318 BA |
74 | #compute parameter estimations, with the Maximum Likelihood |
75 | #Estimator, restricted on selected variables. | |
4cc632c9 | 76 | model = constructionModelesLassoMLE(phiInit, rhoInit, piInit, gamInit, mini, |
2279a641 | 77 | maxi, gamma, X, Y, thresh, eps, S$selected, ncores_inner, verbose) |
086ca318 BA |
78 | } |
79 | else | |
39046da6 | 80 | { |
4cc632c9 BA |
81 | if (verbose) |
82 | print('run the procedure Lasso-Rank') | |
086ca318 BA |
83 | #compute parameter estimations, with the Low Rank |
84 | #Estimator, restricted on selected variables. | |
4cc632c9 | 85 | model = constructionModelesLassoRank(S$Pi, S$Rho, mini, maxi, X, Y, eps, A1, |
2279a641 | 86 | rank.min, rank.max, ncores_inner, verbose) |
4cc632c9 | 87 | |
086ca318 BA |
88 | ################################################ |
89 | ### Regarder la SUITE | |
2279a641 BA |
90 | # phi = runProcedure2()$phi |
91 | # Phi2 = Phi | |
92 | # if (dim(Phi2)[1] == 0) | |
93 | # Phi[, , 1:k,] <- phi | |
94 | # else | |
95 | # { | |
96 | # Phi <- array(0, dim = c(p, m, kmax, dim(Phi2)[4] + dim(phi)[4])) | |
97 | # Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2 | |
98 | # Phi[, , 1:k,-(1:(dim(Phi2)[4]))] <<- phi | |
99 | # } | |
086ca318 | 100 | } |
2279a641 | 101 | model |
086ca318 | 102 | } |
4cc632c9 | 103 | |
2279a641 | 104 | model_list <- |
4cc632c9 BA |
105 | if (ncores_k > 1) |
106 | parLapply(cl, kmin:kmax, computeModel) | |
107 | else | |
108 | lapply(kmin:kmax, computeModel) | |
109 | if (ncores_k > 1) | |
110 | parallel::stopCluster(cl) | |
111 | ||
2279a641 BA |
112 | # Get summary "tableauRecap" from models |
113 | tableauRecap = t( sapply( seq_along(model_list), function(model) { | |
114 | llh = matrix(ncol = 2) | |
115 | for (l in seq_along(model)) | |
116 | llh = rbind(llh, model[[l]]$llh) | |
117 | LLH = llh[-1,1] | |
118 | D = llh[-1,2] | |
119 | c(LLH, D, rep(k, length(model)), 1:length(model)) | |
120 | } ) ) | |
121 | ||
4cc632c9 BA |
122 | if (verbose) |
123 | print('Model selection') | |
2279a641 | 124 | tableauRecap = do.call( rbind, tableauRecap ) #stack list cells into a matrix |
086ca318 BA |
125 | tableauRecap = tableauRecap[rowSums(tableauRecap[, 2:4])!=0,] |
126 | tableauRecap = tableauRecap[(tableauRecap[,1])!=Inf,] | |
127 | data = cbind(1:dim(tableauRecap)[1], tableauRecap[,2], tableauRecap[,2], tableauRecap[,1]) | |
4cc632c9 | 128 | |
086ca318 BA |
129 | require(capushe) |
130 | modSel = capushe(data, n) | |
131 | indModSel <- | |
132 | if (selecMod == 'DDSE') | |
133 | as.numeric(modSel@DDSE@model) | |
134 | else if (selecMod == 'Djump') | |
135 | as.numeric(modSel@Djump@model) | |
136 | else if (selecMod == 'BIC') | |
137 | modSel@BIC_capushe$model | |
138 | else if (selecMod == 'AIC') | |
139 | modSel@AIC_capushe$model | |
140 | model[[tableauRecap[indModSel,3]]][[tableauRecap[indModSel,4]]] | |
141 | } |