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
18 #' @return a list with estimators of parameters
21 #' #TODO: a few examples
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, ncores_k=1, ncores_lambda=3, verbose=FALSE)
33 print("main loop: over all k and all lambda")
37 cl = parallel::makeCluster(ncores_k)
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_k","ncores_lambda","verbose","p","m","k","tableauRecap") )
43 # Compute model with k components
44 computeModel <- function(k)
47 require("valse") #nodes start with an empty environment
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)
58 # TODO: 100 = magic number
59 if (length(grid_lambda)>100)
60 grid_lambda = grid_lambda[seq(1, length(grid_lambda), length.out = 100)]
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_lambda)
70 if (procedure == 'LassoMLE')
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)
78 llh = matrix(ncol = 2)
79 for (l in seq_along(model[[k]]))
80 llh = rbind(llh, model[[k]][[l]]$llh)
87 print('run the procedure Lasso-Rank')
88 #compute parameter estimations, with the Low Rank
89 #Estimator, restricted on selected variables.
90 model = constructionModelesLassoRank(S$Pi, S$Rho, mini, maxi, X, Y, eps, A1,
93 ################################################
95 phi = runProcedure2()$phi
97 if (dim(Phi2)[1] == 0)
101 Phi <- array(0, dim = c(p, m, kmax, dim(Phi2)[4] + dim(phi)[4]))
102 Phi[, , 1:(dim(Phi2)[3]), 1:(dim(Phi2)[4])] <<- Phi2
103 Phi[, , 1:k,-(1:(dim(Phi2)[4]))] <<- phi
106 tableauRecap[[k]] = matrix(c(LLH, D, rep(k, length(model[[k]])), 1:length(model[[k]])), ncol = 4))
111 parLapply(cl, kmin:kmax, computeModel)
113 lapply(kmin:kmax, computeModel)
115 parallel::stopCluster(cl)
118 print('Model selection')
119 tableauRecap = do.call( rbind, tableaurecap ) #stack list cells into a matrix
120 tableauRecap = tableauRecap[rowSums(tableauRecap[, 2:4])!=0,]
121 tableauRecap = tableauRecap[(tableauRecap[,1])!=Inf,]
122 data = cbind(1:dim(tableauRecap)[1], tableauRecap[,2], tableauRecap[,2], tableauRecap[,1])
125 modSel = capushe(data, n)
127 if (selecMod == 'DDSE')
128 as.numeric(modSel@DDSE@model)
129 else if (selecMod == 'Djump')
130 as.numeric(modSel@Djump@model)
131 else if (selecMod == 'BIC')
132 modSel@BIC_capushe$model
133 else if (selecMod == 'AIC')
134 modSel@AIC_capushe$model
135 model[[tableauRecap[indModSel,3]]][[tableauRecap[indModSel,4]]]