update the model selection step. Beginning for the plots
[valse.git] / pkg / R / valse.R
1 #' Main function
2 #'
3 #' @param X matrix of covariates (of size n*p)
4 #' @param Y matrix of responses (of size n*m)
5 #' @param procedure among 'LassoMLE' or 'LassoRank'
6 #' @param selecMod method to select a model among 'DDSE', 'DJump', 'BIC' or 'AIC'
7 #' @param gamma integer for the power in the penaly, by default = 1
8 #' @param mini integer, minimum number of iterations in the EM algorithm, by default = 10
9 #' @param maxi integer, maximum number of iterations in the EM algorithm, by default = 100
10 #' @param eps real, threshold to say the EM algorithm converges, by default = 1e-4
11 #' @param kmin integer, minimum number of clusters, by default = 2
12 #' @param kmax integer, maximum number of clusters, by default = 10
13 #' @param rang.min integer, minimum rank in the low rank procedure, by default = 1
14 #' @param rang.max integer, maximum rank in the
15 #' @return a list with estimators of parameters
16 #' @export
17 #-----------------------------------------------------------------------
18 valse = function(X,Y,procedure = 'LassoMLE',selecMod = 'DDSE',gamma = 1,mini = 10,
19 maxi = 100,eps = 1e-4,kmin = 2,kmax = 3,
20 rang.min = 1,rang.max = 10) {
21 ##################################
22 #core workflow: compute all models
23 ##################################
24
25 p = dim(X)[2]
26 m = dim(Y)[2]
27 n = dim(X)[1]
28
29 model = list()
30 tableauRecap = array(0, dim=c(1000,4))
31 cpt = 0
32 print("main loop: over all k and all lambda")
33
34 for (k in kmin:kmax){
35 print(k)
36 print("Parameters initialization")
37 #smallEM initializes parameters by k-means and regression model in each component,
38 #doing this 20 times, and keeping the values maximizing the likelihood after 10
39 #iterations of the EM algorithm.
40 init = initSmallEM(k, X, Y)
41 phiInit <<- init$phiInit
42 rhoInit <<- init$rhoInit
43 piInit <<- init$piInit
44 gamInit <<- init$gamInit
45 source('~/valse/pkg/R/gridLambda.R')
46 grid_lambda <<- gridLambda(phiInit, rhoInit, piInit, gamInit, X, Y, gamma, mini, maxi, eps)
47
48 print("Compute relevant parameters")
49 #select variables according to each regularization parameter
50 #from the grid: A1 corresponding to selected variables, and
51 #A2 corresponding to unselected variables.
52
53 params = selectiontotale(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,grid_lambda,X,Y,1e-8,eps)
54 #params2 = selectVariables(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,grid_lambda[seq(1,length(grid_lambda), by=3)],X,Y,1e-8,eps)
55 ## etrange : params et params 2 sont différents ...
56
57 selected <<- params$selected
58 Rho <<- params$Rho
59 Pi <<- params$Pi
60
61 if (procedure == 'LassoMLE') {
62 print('run the procedure Lasso-MLE')
63 #compute parameter estimations, with the Maximum Likelihood
64 #Estimator, restricted on selected variables.
65 model[[k]] = constructionModelesLassoMLE(phiInit, rhoInit,piInit,gamInit,mini,maxi,gamma,X,Y,thresh,eps,selected)
66 llh = matrix(ncol = 2)
67 for (l in seq_along(model[[k]])){
68 llh = rbind(llh, model[[k]][[l]]$llh)
69 }
70 LLH = llh[-1,1]
71 D = llh[-1,2]
72 } else {
73 print('run the procedure Lasso-Rank')
74 #compute parameter estimations, with the Low Rank
75 #Estimator, restricted on selected variables.
76 model = constructionModelesLassoRank(Pi, Rho, mini, maxi, X, Y, eps,
77 A1, rank.min, rank.max)
78
79 ################################################
80 ### Regarder la SUITE
81 phi = runProcedure2()$phi
82 Phi2 = Phi
83 if (dim(Phi2)[1] == 0)
84 {
85 Phi[, , 1:k,] <<- phi
86 } else
87 {
88 Phi <<- array(0, dim = c(p, m, kmax, dim(Phi2)[4] + dim(phi)[4]))
89 Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2
90 Phi[, , 1:k,-(1:(dim(Phi2)[4]))] <<- phi
91 }
92 }
93 tableauRecap[(cpt+1):(cpt+length(model[[k]])), ] = matrix(c(LLH, D, rep(k, length(model[[k]])), 1:length(model[[k]])), ncol = 4)
94 cpt = cpt+length(model[[k]])
95 }
96 print('Model selection')
97 tableauRecap = tableauRecap[rowSums(tableauRecap[, 2:4])!=0,]
98 tableauRecap = tableauRecap[(tableauRecap[,1])!=Inf,]
99 data = cbind(1:dim(tableauRecap)[1], tableauRecap[,2], tableauRecap[,2], tableauRecap[,1])
100 require(capushe)
101 modSel = capushe(data, n)
102 if (selecMod == 'DDSE') {
103 indModSel = as.numeric(modSel@DDSE@model)
104 } else if (selecMod == 'Djump') {
105 indModSel = as.numeric(modSel@Djump@model)
106 } else if (selecMod == 'BIC') {
107 indModSel = modSel@BIC_capushe$model
108 } else if (selecMod == 'AIC') {
109 indModSel = modSel@AIC_capushe$model
110 }
111 return(model[[tableauRecap[indModSel,3]]][[tableauRecap[indModSel,4]]])
112 }