Fix multistart script
[morpheus.git] / reports / multistart.R
CommitLineData
1d014a86
BA
1library(morpheus)
2
86f257f8 3testMultistart <- function(N, n, p, beta, b, link, nstart, ncores)
0ad4c8de 4{
778a955e 5 res <- multiRun(
86f257f8 6 list(n=n, p=p, beta=beta, b=b, link=link, nstart=nstart),
0ad4c8de
BA
7 list(
8 function(fargs) {
9 # 1 start
10 library(morpheus)
86f257f8
BA
11 K <- ncol(fargs$beta)
12 op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M)
0ad4c8de 13 x_init <- list(p=rep(1/K,K-1), beta=fargs$mu, b=rep(0,K))
778a955e 14 res2 <- NULL
0ad4c8de 15 tryCatch({
778a955e 16 res2 <- do.call(rbind, op$run(x_init))
01bd5c5b 17 }, error = function(e) {})
778a955e 18 res2
0ad4c8de
BA
19 },
20 function(fargs) {
21 # B starts
22 library(morpheus)
86f257f8
BA
23 K <- ncol(fargs$beta)
24 d <- nrow(fargs$beta)
25 op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M)
0ad4c8de
BA
26 best_val <- Inf
27 best_par <- list()
86f257f8 28 for (i in 1:fargs$nstart)
0ad4c8de
BA
29 {
30 #x_init <- list(p=rep(1/K,K-1), beta=i*fargs$mu, b=rep(0,K))
31 M <- matrix(rnorm(d*K), nrow=d, ncol=K)
32 M <- t(t(M) / sqrt(colSums(M^2)))
33 x_init <- list(p=rep(1/K,K-1), beta=M, b=rep(0,K))
01bd5c5b
BA
34 par <- NULL
35 tryCatch({
0ad4c8de 36 par <- op$run(x_init)
01bd5c5b
BA
37 }, error = function(e) {})
38 if (!is.null(par))
0ad4c8de
BA
39 {
40 val <- op$f( op$linArgs(par) )
41 if (val < best_val)
42 {
43 best_par <- par
44 best_val <- val
45 }
46 }
47 }
0e0fa9ff 48 do.call(rbind,best_par) #return NULL on empty list
0ad4c8de
BA
49 }
50 ),
51 prepareArgs = function(fargs, index) {
52 library(morpheus)
86f257f8
BA
53 io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$link)
54 fargs$M <- computeMoments(io$X, io$Y)
55 mu <- computeMu(io$X, io$Y, list(M=fargs$M))
0ad4c8de 56 fargs$mu <- mu
86f257f8
BA
57 fargs$X <- io$X
58 fargs$Y <- io$Y
01bd5c5b 59 fargs
0ad4c8de 60 }, N=N, ncores=ncores, verbose=TRUE)
0e0fa9ff
BA
61 p <- c(p, 1-sum(p))
62 for (i in 1:length(res)) {
63 for (j in N:1) {
64 if (is.null(res[[i]][[j]]) || is.na(res[[i]][[j]]))
65 res[[i]][[j]] <- NULL
66 }
67 print(paste("Count valid runs for ",i," = ",length(res[[i]]),sep=""))
e134ce3c 68 res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact")
0e0fa9ff 69 }
778a955e 70 res
0ad4c8de
BA
71}
72
1d014a86
BA
73# Default values:
74link = "logit"
0ad4c8de 75N <- 10
1d014a86
BA
76n <- 1e4
77ncores <- 1
78nstart <- 3 #nstart-1 random starting points for each MC run
79
80cmd_args <- commandArgs()
81for (arg in cmd_args)
82{
83 if (substr(arg,1,1)!='-')
84 {
85 spl <- strsplit(arg,'=')[[1]]
86 if (spl[1] == "nc") {
87 ncores <- as.integer(spl[2])
88 } else if (spl[1] == "N") {
89 N <- as.integer(spl[2])
90 } else if (spl[1] == "n") {
91 n <- as.integer(spl[2])
92 } else if (spl[1] == "d") {
93 d <- as.integer(spl[2])
94 } else if (spl[1] == "link") {
95 link <- spl[2]
96 } else if (spl[1] == "nstart") {
97 nstart <- spl[2]
98 }
99 }
100}
1d014a86 101
86f257f8
BA
102if (d == 2) {
103 p <- .5
104 b <- c(-.2, .5)
105 beta <- matrix( c(1,-2, 3,1), ncol=2 )
106} else if (d == 5) {
107 p <- .5
108 b <- c(-.2, .5)
109 beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=2 )
110} else if (d == 10) {
111 p <- c(.3, .3)
112 b <- c(-.2, 0, .5)
113 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 )
114}
115
116mr <- testMultistart(N, n, p, beta, b, link, nstart, ncores)
117mr_params <- list("N"=N, "nc"=ncores, "n"=n, "link"=link,
0ad4c8de 118 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "nstart"=nstart)
1d014a86 119
778a955e 120save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",nstart,".RData",sep=""))