Commit | Line | Data |
---|---|---|
086ca318 BA |
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, | |
24 | maxi = 50,eps = 1e-4,kmin = 2,kmax = 2, | |
25 | rang.min = 1,rang.max = 10) | |
26 | { | |
27 | #################### | |
28 | # compute all models | |
29 | #################### | |
30 | ||
31 | p = dim(X)[2] | |
32 | m = dim(Y)[2] | |
33 | n = dim(X)[1] | |
34 | ||
35 | model = list() | |
36 | tableauRecap = array(0, dim=c(1000,4)) | |
37 | cpt = 0 | |
38 | print("main loop: over all k and all lambda") | |
39 | ||
40 | for (k in kmin:kmax) | |
41 | { | |
42 | print(k) | |
43 | print("Parameters initialization") | |
44 | #smallEM initializes parameters by k-means and regression model in each component, | |
45 | #doing this 20 times, and keeping the values maximizing the likelihood after 10 | |
46 | #iterations of the EM algorithm. | |
47 | init = initSmallEM(k, X, Y) | |
48 | phiInit <- init$phiInit | |
49 | rhoInit <- init$rhoInit | |
50 | piInit <- init$piInit | |
51 | gamInit <- init$gamInit | |
52 | grid_lambda <- computeGridLambda(phiInit, rhoInit, piInit, gamInit, X, Y, gamma, mini, maxi, eps) | |
53 | ||
54 | if (length(grid_lambda)>100) | |
55 | grid_lambda = grid_lambda[seq(1, length(grid_lambda), length.out = 100)] | |
56 | print("Compute relevant parameters") | |
57 | #select variables according to each regularization parameter | |
58 | #from the grid: A1 corresponding to selected variables, and | |
59 | #A2 corresponding to unselected variables. | |
60 | ||
61 | params = selectiontotale(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,grid_lambda,X,Y,1e-8,eps) | |
62 | #params2 = selectVariables(phiInit,rhoInit,piInit,gamInit,mini,maxi,gamma,grid_lambda[seq(1,length(grid_lambda), by=3)],X,Y,1e-8,eps) | |
63 | ## etrange : params et params 2 sont différents ... | |
64 | selected <- params$selected | |
65 | Rho <- params$Rho | |
66 | Pi <- params$Pi | |
67 | ||
68 | if (procedure == 'LassoMLE') | |
39046da6 | 69 | { |
086ca318 BA |
70 | print('run the procedure Lasso-MLE') |
71 | #compute parameter estimations, with the Maximum Likelihood | |
72 | #Estimator, restricted on selected variables. | |
73 | model[[k]] = constructionModelesLassoMLE(phiInit, rhoInit,piInit,gamInit,mini,maxi,gamma,X,Y,thresh,eps,selected) | |
74 | llh = matrix(ncol = 2) | |
75 | for (l in seq_along(model[[k]])) | |
76 | llh = rbind(llh, model[[k]][[l]]$llh) | |
77 | LLH = llh[-1,1] | |
78 | D = llh[-1,2] | |
79 | } | |
80 | else | |
39046da6 | 81 | { |
086ca318 BA |
82 | print('run the procedure Lasso-Rank') |
83 | #compute parameter estimations, with the Low Rank | |
84 | #Estimator, restricted on selected variables. | |
85 | model = constructionModelesLassoRank(Pi, Rho, mini, maxi, X, Y, eps, | |
86 | A1, rank.min, rank.max) | |
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 | tableauRecap[(cpt+1):(cpt+length(model[[k]])), ] = matrix(c(LLH, D, rep(k, length(model[[k]])), 1:length(model[[k]])), ncol = 4) | |
102 | cpt = cpt+length(model[[k]]) | |
103 | } | |
104 | print('Model selection') | |
105 | tableauRecap = tableauRecap[rowSums(tableauRecap[, 2:4])!=0,] | |
106 | tableauRecap = tableauRecap[(tableauRecap[,1])!=Inf,] | |
107 | data = cbind(1:dim(tableauRecap)[1], tableauRecap[,2], tableauRecap[,2], tableauRecap[,1]) | |
108 | require(capushe) | |
109 | modSel = capushe(data, n) | |
110 | indModSel <- | |
111 | if (selecMod == 'DDSE') | |
112 | as.numeric(modSel@DDSE@model) | |
113 | else if (selecMod == 'Djump') | |
114 | as.numeric(modSel@Djump@model) | |
115 | else if (selecMod == 'BIC') | |
116 | modSel@BIC_capushe$model | |
117 | else if (selecMod == 'AIC') | |
118 | modSel@AIC_capushe$model | |
119 | model[[tableauRecap[indModSel,3]]][[tableauRecap[indModSel,4]]] | |
120 | } |