update the model selection step. Beginning for the plots
[valse.git] / pkg / R / valse.R
CommitLineData
c7dab9ff 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'
3f62d540 6#' @param selecMod method to select a model among 'DDSE', 'DJump', 'BIC' or 'AIC'
c7dab9ff 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#-----------------------------------------------------------------------
3f62d540 18valse = function(X,Y,procedure = 'LassoMLE',selecMod = 'DDSE',gamma = 1,mini = 10,
19 maxi = 100,eps = 1e-4,kmin = 2,kmax = 3,
c7dab9ff 20 rang.min = 1,rang.max = 10) {
21 ##################################
22 #core workflow: compute all models
23 ##################################
24
3f62d540 25 p = dim(X)[2]
26 m = dim(Y)[2]
51485a7d 27 n = dim(X)[1]
c7dab9ff 28
3f62d540 29 model = list()
30 tableauRecap = array(0, dim=c(1000,4))
51485a7d 31 cpt = 0
c7dab9ff 32 print("main loop: over all k and all lambda")
3f62d540 33
34 for (k in kmin:kmax){
c7dab9ff 35 print(k)
c7dab9ff 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
51485a7d 45 source('~/valse/pkg/R/gridLambda.R')
3f62d540 46 grid_lambda <<- gridLambda(phiInit, rhoInit, piInit, gamInit, X, Y, gamma, mini, maxi, eps)
c7dab9ff 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.
51485a7d 52
3f62d540 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)
51485a7d 55 ## etrange : params et params 2 sont différents ...
56
f1b0e0ab 57 selected <<- params$selected
c7dab9ff 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.
51485a7d 65 model[[k]] = constructionModelesLassoMLE(phiInit, rhoInit,piInit,gamInit,mini,maxi,gamma,X,Y,thresh,eps,selected)
3f62d540 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]
c7dab9ff 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 }
51485a7d 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]])
c7dab9ff 95 }
96 print('Model selection')
3f62d540 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)
c7dab9ff 106 } else if (selecMod == 'BIC') {
3f62d540 107 indModSel = modSel@BIC_capushe$model
c7dab9ff 108 } else if (selecMod == 'AIC') {
3f62d540 109 indModSel = modSel@AIC_capushe$model
c7dab9ff 110 }
3f62d540 111 return(model[[tableauRecap[indModSel,3]]][[tableauRecap[indModSel,4]]])
c7dab9ff 112}