3 #' Estimate N times some parameters, outputs of some list of functions. This method is
4 #' thus very generic, allowing typically bootstrap or Monte-Carlo estimations of matrices
5 #' μ or β. Passing a list of functions opens the possibility to compare them on a fair
6 #' basis (exact same inputs). It's even possible to compare methods on some deterministic
7 #' design of experiments.
9 #' @param fargs List of arguments for the estimation functions
10 #' @param estimParams List of nf function(s) to apply on fargs - shared signature
11 #' @param prepareArgs Prepare arguments for the functions inside estimParams
12 #' @param N Number of runs
13 #' @param ncores Number of cores for parallel runs (<=1: sequential)
14 #' @param agg Aggregation method (default: lapply)
15 #' @param verbose TRUE to indicate runs + methods numbers
17 #' @return A list of nf aggregates of N results (matrices).
21 #' β <- matrix(c(1,-2,3,1),ncol=2)
23 #' # Bootstrap + computeMu, morpheus VS flexmix ; assumes fargs first 3 elts X,Y,K
24 #' io <- generateSampleIO(n=1000, p=1/2, β=β, b=c(0,0), "logit")
26 #' res <- multiRun(list(X=io$X,Y=io$Y,optargs=list(K=2)), list(
31 #' computeMu(fargs$X[ind,],fargs$Y[ind],fargs$optargs)
37 #' K <- fargs$optargs$K
38 #' dat = as.data.frame( cbind(fargs$Y[ind],fargs$X[ind,]) )
39 #' out = refit( flexmix( cbind(V1, 1 - V1) ~ 0+., data=dat, k=K,
40 #' model=FLXMRglm(family="binomial") ) )
41 #' normalize( matrix(out@@coef[1:(ncol(fargs$X)*K)], ncol=K) )
43 #' prepareArgs = function(fargs,index) {
45 #' fargs$ind <- 1:nrow(fargs$X)
47 #' fargs$ind <- sample(1:nrow(fargs$X),replace=TRUE)
51 #' res[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact")
53 #' # Monte-Carlo + optimParams from X,Y, morpheus VS flexmix ; first args n,p,β,b
54 #' res <- multiRun(list(n=1000,p=1/2,β=β,b=c(0,0),optargs=list(link="logit")),list(
58 #' K <- fargs$optargs$K
59 #' μ <- computeMu(fargs$X, fargs$Y, fargs$optargs)
60 #' optimParams(fargs$K,fargs$link,fargs$optargs)$run(list(β=μ))$β
65 #' K <- fargs$optargs$K
66 #' dat <- as.data.frame( cbind(fargs$Y,fargs$X) )
67 #' out <- refit( flexmix( cbind(V1, 1 - V1) ~ 0+., data=dat, k=K,
68 #' model=FLXMRglm(family="binomial") ) )
69 #' sapply( seq_len(K), function(i) as.double( out@@components[[1]][[i]][,1] ) )
71 #' prepareArgs = function(fargs,index) {
73 #' io = generateSampleIO(fargs$n, fargs$p, fargs$β, fargs$b, fargs$optargs$link)
76 #' fargs$K = ncol(fargs$β)
77 #' fargs$link = fargs$optargs$link
78 #' fargs$optargs$M = computeMoments(io$X,io$Y)
82 #' res[[i]] <- alignMatrices(res[[i]], ref=β, ls_mode="exact")}
84 multiRun <- function(fargs, estimParams,
85 prepareArgs = function(x,i) x, N=10, ncores=3, agg=lapply, verbose=FALSE)
89 # No checks on fargs: supposedly done in estimParams[[i]]()
90 if (!is.list(estimParams))
91 estimParams = list(estimParams)
92 # Verify that the provided parameters estimations are indeed functions
93 lapply(seq_along(estimParams), function(i) {
94 if (!is.function(estimParams[[i]]))
95 stop("estimParams: list of function(fargs)")
97 if (!is.numeric(N) || N < 1)
98 stop("N: positive integer")
100 estimParamAtIndex <- function(index)
102 fargs <- prepareArgs(fargs, index)
104 cat("Run ",index,"\n")
105 lapply(seq_along(estimParams), function(i) {
107 cat(" Method ",i,"\n")
108 out <- estimParams[[i]](fargs)
118 cl = parallel::makeCluster(ncores, outfile="")
119 parallel::clusterExport(cl, c("fargs","verbose"), environment())
120 list_res = parallel::clusterApplyLB(cl, 1:N, estimParamAtIndex)
121 parallel::stopCluster(cl)
124 list_res = lapply(1:N, estimParamAtIndex)
126 # De-interlace results: output one list per function
127 nf <- length(estimParams)
128 lapply( seq_len(nf), function(i) lapply(seq_len(N), function(j) list_res[[j]][[i]]) )