8ce5117498b58c33b2cda93b80c814fce8b443af
[valse.git] / pkg / R / main.R
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, 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)
26 {
27 p = dim(X)[2]
28 m = dim(Y)[2]
29 n = dim(X)[1]
30
31 tableauRecap = list()
32 if (verbose)
33 print("main loop: over all k and all lambda")
34
35 if (ncores_outer > 1)
36 {
37 cl = parallel::makeCluster(ncores_outer)
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_outer","ncores_inner","verbose","p","m","k","tableauRecap") )
41 }
42
43 # Compute model with k components
44 computeModel <- function(k)
45 {
46 if (ncores_outer > 1)
47 require("valse") #nodes start with an empty environment
48
49 if (verbose)
50 print(paste("Parameters initialization for k =",k))
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.
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
59 if (length(grid_lambda)>100)
60 grid_lambda = grid_lambda[seq(1, length(grid_lambda), length.out = 100)]
61
62 if (verbose)
63 print("Compute relevant parameters")
64 #select variables according to each regularization parameter
65 #from the grid: A1 corresponding to selected variables, and
66 #A2 corresponding to unselected variables.
67 S = selectVariables(P$phiInit,P$rhoInit,P$piInit,P$gamInit,mini,maxi,gamma,
68 grid_lambda,X,Y,1e-8,eps,ncores_inner)
69
70 if (procedure == 'LassoMLE')
71 {
72 if (verbose)
73 print('run the procedure Lasso-MLE')
74 #compute parameter estimations, with the Maximum Likelihood
75 #Estimator, restricted on selected variables.
76 model = constructionModelesLassoMLE(phiInit, rhoInit, piInit, gamInit, mini,
77 maxi, gamma, X, Y, thresh, eps, S$selected, ncores_inner, verbose)
78 }
79 else
80 {
81 if (verbose)
82 print('run the procedure Lasso-Rank')
83 #compute parameter estimations, with the Low Rank
84 #Estimator, restricted on selected variables.
85 model = constructionModelesLassoRank(S$Pi, S$Rho, mini, maxi, X, Y, eps, A1,
86 rank.min, rank.max, ncores_inner, verbose)
87
88 ################################################
89 ### Regarder la SUITE
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 # }
100 }
101 model
102 }
103
104 model_list <-
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
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
122 if (verbose)
123 print('Model selection')
124 tableauRecap = do.call( rbind, tableauRecap ) #stack list cells into a matrix
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])
128
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 }