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 verbose TRUE to indicate runs + methods numbers
16 #' @return A list of nf aggregates of N results (matrices).
20 #' β <- matrix(c(1,-2,3,1),ncol=2)
22 #' # Bootstrap + computeMu, morpheus VS flexmix ; assumes fargs first 3 elts X,Y,K
23 #' io <- generateSampleIO(n=1000, p=1/2, β=β, b=c(0,0), "logit")
25 #' res <- multiRun(list(X=io$X,Y=io$Y,optargs=list(K=2,jd_nvects=0)), list(
30 #' computeMu(fargs$X[ind,],fargs$Y[ind],fargs$optargs)
36 #' K <- fargs$optargs$K
37 #' dat = as.data.frame( cbind(fargs$Y[ind],fargs$X[ind,]) )
38 #' out = refit( flexmix( cbind(V1, 1 - V1) ~ 0+., data=dat, k=K,
39 #' model=FLXMRglm(family="binomial") ) )
40 #' normalize( matrix(out@@coef[1:(ncol(fargs$X)*K)], ncol=K) )
42 #' prepareArgs = function(fargs) {
43 #' fargs$ind <- sample(1:nrow(fargs$X),replace=TRUE)
47 #' res[[i]] <- alignMatrices(res[[i]], ref=μ, ls_mode="exact")
49 #' # Monte-Carlo + optimParams from X,Y, morpheus VS flexmix ; first args n,p,β,b
50 #' res <- multiRun(list(n=1000,p=1/2,β=β,b=c(0,0),optargs=list(link="logit")),list(
54 #' K <- fargs$optargs$K
55 #' μ <- computeMu(fargs$X, fargs$Y, fargs$optargs)
56 #' V <- list( p=rep(1/K,K-1), β=μ, b=c(0,0) )
57 #' optimParams(V,fargs$optargs)$β
62 #' K <- fargs$optargs$K
63 #' dat <- as.data.frame( cbind(fargs$Y,fargs$X) )
64 #' out <- refit( flexmix( cbind(V1, 1 - V1) ~ 0+., data=dat, k=K,
65 #' model=FLXMRglm(family="binomial") ) )
66 #' sapply( seq_len(K), function(i) as.double( out@@components[[1]][[i]][,1] ) )
68 #' prepareArgs = function(fargs) {
70 #' io = generateSampleIO(fargs$n, fargs$p, fargs$β, fargs$b, fargs$optargs$link)
73 #' fargs$optargs$K = ncol(fargs$β)
74 #' fargs$optargs$M = computeMoments(io$X,io$Y)
78 #' res[[i]] <- alignMatrices(res[[i]], ref=β, ls_mode="exact")}
80 multiRun <- function(fargs, estimParams,
81 prepareArgs = function(x) x, N=10, ncores=3, agg=lapply, verbose=FALSE)
85 # No checks on fargs: supposedly done in estimParams[[i]]()
86 if (!is.list(estimParams))
87 estimParams = list(estimParams)
88 # Verify that the provided parameters estimations are indeed functions
89 lapply(seq_along(estimParams), function(i) {
90 if (!is.function(estimParams[[i]]))
91 stop("estimParams: list of function(fargs)")
93 if (!is.numeric(N) || N < 1)
94 stop("N: positive integer")
96 estimParamAtIndex <- function(index)
98 fargs <- prepareArgs(fargs)
100 cat("Run ",index,"\n")
101 lapply(seq_along(estimParams), function(i) {
103 cat(" Method ",i,"\n")
104 out <- estimParams[[i]](fargs)
114 cl = parallel::makeCluster(ncores, outfile="")
115 parallel::clusterExport(cl, c("fargs","verbose"), environment())
116 list_res = parallel::clusterApplyLB(cl, 1:N, estimParamAtIndex)
117 parallel::stopCluster(cl)
120 list_res = lapply(1:N, estimParamAtIndex)
122 # De-interlace results: output one list per function
123 nf <- length(estimParams)
124 lapply( seq_len(nf), function(i) lapply(seq_len(N), function(j) list_res[[j]][[i]]) )