| 1 | optimBeta <- function(N, n, p, beta, b, link, ncores) |
| 2 | { |
| 3 | library(morpheus) |
| 4 | res <- multiRun( |
| 5 | list(n=n, p=p, beta=beta, b=b, link=link), |
| 6 | list( |
| 7 | # morpheus |
| 8 | function(fargs) { |
| 9 | library(morpheus) |
| 10 | K <- ncol(fargs$beta) |
| 11 | M <- computeMoments(fargs$X, fargs$Y) |
| 12 | mu <- computeMu(fargs$X, fargs$Y, list(K=K, M=M)) |
| 13 | op <- optimParams(fargs$X, fargs$Y, K, fargs$link, M, 1) #only 1 OpenMP core |
| 14 | x_init <- list(p=rep(1/K,K-1), beta=mu, b=rep(0,K)) |
| 15 | res2 <- NULL |
| 16 | tryCatch({ |
| 17 | res2 <- do.call(rbind, op$run(x_init)) |
| 18 | }, error = function(e) {}) |
| 19 | res2 |
| 20 | } |
| 21 | , |
| 22 | # flexmix |
| 23 | function(fargs) { |
| 24 | library(flexmix) |
| 25 | source("../patch_Bettina/FLXMRglm.R") |
| 26 | K <- ncol(fargs$beta) |
| 27 | dat <- as.data.frame( cbind(fargs$Y,fargs$X) ) |
| 28 | res2 <- NULL |
| 29 | tryCatch({ |
| 30 | fm <- flexmix( cbind(V1, 1-V1) ~ ., data=dat, k=K, |
| 31 | model = FLXMRglm(family = binomial(link = link)) ) |
| 32 | pf <- colMeans(fm@posterior[["scaled"]]) |
| 33 | out <- refit(fm) |
| 34 | beta_b <- sapply( seq_len(K), function(i) { |
| 35 | as.double( out@components[[1]][[i]][,1] ) |
| 36 | } ) |
| 37 | res2 <- rbind(pf, beta_b[2:nrow(beta_b),], beta_b[1,]) |
| 38 | }, error = function(e) { |
| 39 | res2 <- NA |
| 40 | }) |
| 41 | res2 |
| 42 | } |
| 43 | ), |
| 44 | prepareArgs = function(fargs, index) { |
| 45 | library(morpheus) |
| 46 | io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$link) |
| 47 | fargs$X = io$X |
| 48 | fargs$Y = io$Y |
| 49 | fargs |
| 50 | }, N=N, ncores=ncores, verbose=TRUE) |
| 51 | p <- c(p, 1-sum(p)) |
| 52 | for (i in 1:length(res)) { |
| 53 | for (j in N:1) { |
| 54 | if (is.null(res[[i]][[j]]) || is.na(res[[i]][[j]])) |
| 55 | res[[i]][[j]] <- NULL |
| 56 | } |
| 57 | print(paste("Count valid runs for ",i," = ",length(res[[i]]),sep="")) |
| 58 | res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact") |
| 59 | } |
| 60 | res |
| 61 | } |
| 62 | |
| 63 | # Default values: |
| 64 | link = "logit" |
| 65 | N <- 10 |
| 66 | d <- 2 |
| 67 | n <- 1e4 |
| 68 | ncores <- 1 |
| 69 | |
| 70 | cmd_args <- commandArgs() |
| 71 | for (arg in cmd_args) |
| 72 | { |
| 73 | if (substr(arg,1,1)!='-') { |
| 74 | spl <- strsplit(arg,'=')[[1]] |
| 75 | if (spl[1] == "nc") { |
| 76 | ncores <- as.integer(spl[2]) |
| 77 | } else if (spl[1] == "N") { |
| 78 | N <- as.integer(spl[2]) |
| 79 | } else if (spl[1] == "n") { |
| 80 | n <- as.integer(spl[2]) |
| 81 | } else if (spl[1] == "d") { |
| 82 | d <- as.integer(spl[2]) |
| 83 | } else if (spl[1] == "link") { |
| 84 | link <- spl[2] |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | if (d == 2) { |
| 90 | p <- .5 |
| 91 | b <- c(-.2, .5) |
| 92 | beta <- matrix( c(1,-2, 3,1), ncol=2 ) |
| 93 | } else if (d == 5) { |
| 94 | p <- .5 |
| 95 | b <- c(-.2, .5) |
| 96 | beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=2 ) |
| 97 | } else if (d == 10) { |
| 98 | p <- c(.3, .3) |
| 99 | b <- c(-.2, 0, .5) |
| 100 | beta <- matrix( c(1,2,-1,0,3,4,-1,-3,0,2, 2,-3,0,1,0,-1,-4,3,2,0, -1,1,3,-1,0,0,2,0,1,-2), ncol=3 ) |
| 101 | } |
| 102 | |
| 103 | mr <- optimBeta(N, n, p, beta, b, link, ncores) |
| 104 | mr_params <- list("N"=N, "nc"=ncores, "n"=n, "link"=link, |
| 105 | "p"=c(p,1-sum(p)), "beta"=beta, "b"=b) |
| 106 | |
| 107 | save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,".RData",sep="")) |