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