Merge branch 'master' of auder.net:valse
[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 selected <<- params$selected
51 Rho <<- params$Rho
52 Pi <<- params$Pi
53
54 if (procedure == 'LassoMLE') {
55 print('run the procedure Lasso-MLE')
56 #compute parameter estimations, with the Maximum Likelihood
57 #Estimator, restricted on selected variables.
58 model = constructionModelesLassoMLE(
59 phiInit, rhoInit,piInit,tauInit,mini,maxi,
60 gamma,gridLambda,X,Y,thresh,eps,selected)
61 ################################################
62 ### Regarder la SUITE
63 r1 = runProcedure1()
64 Phi2 = Phi
65 Rho2 = Rho
66 Pi2 = Pi
67
68 if (is.null(dim(Phi2)))
69 #test was: size(Phi2) == 0
70 {
71 Phi[, , 1:k] <<- r1$phi
72 Rho[, , 1:k] <<- r1$rho
73 Pi[1:k,] <<- r1$pi
74 } else
75 {
76 Phi <<-
77 array(0., dim = c(p, m, kmax, dim(Phi2)[4] + dim(r1$phi)[4]))
78 Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2
79 Phi[, , 1:k, dim(Phi2)[4] + 1] <<- r1$phi
80 Rho <<-
81 array(0., dim = c(m, m, kmax, dim(Rho2)[4] + dim(r1$rho)[4]))
82 Rho[, , 1:(dim(Rho2)[3]), 1:(dim(Rho2)[4])] <<- Rho2
83 Rho[, , 1:k, dim(Rho2)[4] + 1] <<- r1$rho
84 Pi <<- array(0., dim = c(kmax, dim(Pi2)[2] + dim(r1$pi)[2]))
85 Pi[1:nrow(Pi2), 1:ncol(Pi2)] <<- Pi2
86 Pi[1:k, ncol(Pi2) + 1] <<- r1$pi
87 }
88 } else {
89 print('run the procedure Lasso-Rank')
90 #compute parameter estimations, with the Low Rank
91 #Estimator, restricted on selected variables.
92 model = constructionModelesLassoRank(Pi, Rho, mini, maxi, X, Y, eps,
93 A1, rank.min, rank.max)
94
95 ################################################
96 ### Regarder la SUITE
97 phi = runProcedure2()$phi
98 Phi2 = Phi
99 if (dim(Phi2)[1] == 0)
100 {
101 Phi[, , 1:k,] <<- phi
102 } else
103 {
104 Phi <<- array(0, dim = c(p, m, kmax, dim(Phi2)[4] + dim(phi)[4]))
105 Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2
106 Phi[, , 1:k,-(1:(dim(Phi2)[4]))] <<- phi
107 }
108 }
109 }
110 print('Model selection')
111 if (selecMod == 'SlopeHeuristic') {
112
113 } else if (selecMod == 'BIC') {
114
115 } else if (selecMod == 'AIC') {
116
117 }
118 }