Folder reorganization
[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 'SlopeHeuristic', 'BIC', '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,selecMod,gamma = 1,mini = 10,
19 maxi = 100,eps = 1e-4,kmin = 2,kmax = 10,
20 rang.min = 1,rang.max = 10) {
21 ##################################
22 #core workflow: compute all models
23 ##################################
24
25 p = dim(phiInit)[1]
26 m = dim(phiInit)[2]
27
28 print("main loop: over all k and all lambda")
29 for (k in kmin:kmax)
30 {
31 print(k)
32
33 print("Parameters initialization")
34 #smallEM initializes parameters by k-means and regression model in each component,
35 #doing this 20 times, and keeping the values maximizing the likelihood after 10
36 #iterations of the EM algorithm.
37 init = initSmallEM(k, X, Y)
38 phiInit <<- init$phiInit
39 rhoInit <<- init$rhoInit
40 piInit <<- init$piInit
41 gamInit <<- init$gamInit
42
43 gridLambda <<- gridLambda(phiInit, rhoInit, piInit, gamInit, X, Y, gamma, mini, maxi, eps)
44
45 print("Compute relevant parameters")
46 #select variables according to each regularization parameter
47 #from the grid: A1 corresponding to selected variables, and
48 #A2 corresponding to unselected variables.
49 params = selectiontotale(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,gridLambda,X,Y,1e-8,eps)
50 A1 <<- params$A1
51 A2 <<- params$A2
52 Rho <<- params$Rho
53 Pi <<- params$Pi
54
55 if (procedure == 'LassoMLE') {
56 print('run the procedure Lasso-MLE')
57 #compute parameter estimations, with the Maximum Likelihood
58 #Estimator, restricted on selected variables.
59 model = constructionModelesLassoMLE(
60 phiInit, rhoInit,piInit,tauInit,mini,maxi,
61 gamma,gridLambda,X,Y,thresh,eps,A1,A2)
62 ################################################
63 ### Regarder la SUITE
64 r1 = runProcedure1()
65 Phi2 = Phi
66 Rho2 = Rho
67 Pi2 = Pi
68
69 if (is.null(dim(Phi2)))
70 #test was: size(Phi2) == 0
71 {
72 Phi[, , 1:k] <<- r1$phi
73 Rho[, , 1:k] <<- r1$rho
74 Pi[1:k,] <<- r1$pi
75 } else
76 {
77 Phi <<-
78 array(0., dim = c(p, m, kmax, dim(Phi2)[4] + dim(r1$phi)[4]))
79 Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2
80 Phi[, , 1:k, dim(Phi2)[4] + 1] <<- r1$phi
81 Rho <<-
82 array(0., dim = c(m, m, kmax, dim(Rho2)[4] + dim(r1$rho)[4]))
83 Rho[, , 1:(dim(Rho2)[3]), 1:(dim(Rho2)[4])] <<- Rho2
84 Rho[, , 1:k, dim(Rho2)[4] + 1] <<- r1$rho
85 Pi <<- array(0., dim = c(kmax, dim(Pi2)[2] + dim(r1$pi)[2]))
86 Pi[1:nrow(Pi2), 1:ncol(Pi2)] <<- Pi2
87 Pi[1:k, ncol(Pi2) + 1] <<- r1$pi
88 }
89 } else {
90 print('run the procedure Lasso-Rank')
91 #compute parameter estimations, with the Low Rank
92 #Estimator, restricted on selected variables.
93 model = constructionModelesLassoRank(Pi, Rho, mini, maxi, X, Y, eps,
94 A1, rank.min, rank.max)
95
96 ################################################
97 ### Regarder la SUITE
98 phi = runProcedure2()$phi
99 Phi2 = Phi
100 if (dim(Phi2)[1] == 0)
101 {
102 Phi[, , 1:k,] <<- phi
103 } else
104 {
105 Phi <<- array(0, dim = c(p, m, kmax, dim(Phi2)[4] + dim(phi)[4]))
106 Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2
107 Phi[, , 1:k,-(1:(dim(Phi2)[4]))] <<- phi
108 }
109 }
110 }
111 print('Model selection')
112 if (selecMod == 'SlopeHeuristic') {
113
114 } else if (selecMod == 'BIC') {
115
116 } else if (selecMod == 'AIC') {
117
118 }
119 }