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 | |
23 | valse = function(X,Y,procedure = 'LassoMLE',selecMod = 'DDSE',gamma = 1,mini = 10, | |
24 | maxi = 50,eps = 1e-4,kmin = 2,kmax = 2, | |
4cc632c9 | 25 | rang.min = 1,rang.max = 10, ncores_k=1, ncores_lambda=3, 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 | ||
35 | if (ncores_k > 1) | |
086ca318 | 36 | { |
4cc632c9 BA |
37 | cl = parallel::makeCluster(ncores_k) |
38 | parallel::clusterExport( cl=cl, envir=environment(), varlist=c("X","Y","procedure", | |
39 | "selecMod","gamma","mini","maxi","eps","kmin","kmax","rang.min","rang.max", | |
40 | "ncores_k","ncores_lambda","verbose","p","m","k","tableauRecap") ) | |
41 | } | |
42 | ||
43 | # Compute model with k components | |
44 | computeModel <- function(k) | |
45 | { | |
46 | if (ncores_k > 1) | |
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 BA |
67 | S = selectVariables(P$phiInit,P$rhoInit,P$piInit,P$gamInit,mini,maxi,gamma, |
68 | grid_lambda,X,Y,1e-8,eps,ncores_lambda) | |
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 BA |
76 | model = constructionModelesLassoMLE(phiInit, rhoInit, piInit, gamInit, mini, |
77 | maxi, gamma, X, Y, thresh, eps, S$selected) | |
086ca318 BA |
78 | llh = matrix(ncol = 2) |
79 | for (l in seq_along(model[[k]])) | |
80 | llh = rbind(llh, model[[k]][[l]]$llh) | |
81 | LLH = llh[-1,1] | |
82 | D = llh[-1,2] | |
83 | } | |
84 | else | |
39046da6 | 85 | { |
4cc632c9 BA |
86 | if (verbose) |
87 | print('run the procedure Lasso-Rank') | |
086ca318 BA |
88 | #compute parameter estimations, with the Low Rank |
89 | #Estimator, restricted on selected variables. | |
4cc632c9 BA |
90 | model = constructionModelesLassoRank(S$Pi, S$Rho, mini, maxi, X, Y, eps, A1, |
91 | rank.min, rank.max) | |
92 | ||
086ca318 BA |
93 | ################################################ |
94 | ### Regarder la SUITE | |
95 | phi = runProcedure2()$phi | |
96 | Phi2 = Phi | |
97 | if (dim(Phi2)[1] == 0) | |
98 | Phi[, , 1:k,] <- phi | |
99 | else | |
100 | { | |
101 | Phi <- array(0, dim = c(p, m, kmax, dim(Phi2)[4] + dim(phi)[4])) | |
102 | Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2 | |
103 | Phi[, , 1:k,-(1:(dim(Phi2)[4]))] <<- phi | |
104 | } | |
105 | } | |
4cc632c9 | 106 | tableauRecap[[k]] = matrix(c(LLH, D, rep(k, length(model[[k]])), 1:length(model[[k]])), ncol = 4)) |
086ca318 | 107 | } |
4cc632c9 BA |
108 | |
109 | model <- | |
110 | if (ncores_k > 1) | |
111 | parLapply(cl, kmin:kmax, computeModel) | |
112 | else | |
113 | lapply(kmin:kmax, computeModel) | |
114 | if (ncores_k > 1) | |
115 | parallel::stopCluster(cl) | |
116 | ||
117 | if (verbose) | |
118 | print('Model selection') | |
119 | tableauRecap = do.call( rbind, tableaurecap ) #stack list cells into a matrix | |
086ca318 BA |
120 | tableauRecap = tableauRecap[rowSums(tableauRecap[, 2:4])!=0,] |
121 | tableauRecap = tableauRecap[(tableauRecap[,1])!=Inf,] | |
122 | data = cbind(1:dim(tableauRecap)[1], tableauRecap[,2], tableauRecap[,2], tableauRecap[,1]) | |
4cc632c9 | 123 | |
086ca318 BA |
124 | require(capushe) |
125 | modSel = capushe(data, n) | |
126 | indModSel <- | |
127 | if (selecMod == 'DDSE') | |
128 | as.numeric(modSel@DDSE@model) | |
129 | else if (selecMod == 'Djump') | |
130 | as.numeric(modSel@Djump@model) | |
131 | else if (selecMod == 'BIC') | |
132 | modSel@BIC_capushe$model | |
133 | else if (selecMod == 'AIC') | |
134 | modSel@AIC_capushe$model | |
135 | model[[tableauRecap[indModSel,3]]][[tableauRecap[indModSel,4]]] | |
136 | } |