fd22a31548067dd20a3ca401f317103dffe240a6
[morpheus.git] / reports / accuracy.R
1 optimBeta <- function(N, n, K, p, beta, b, link, ncores)
2 {
3 library(morpheus)
4 res <- multiRun(
5 list(n=n, p=p, beta=beta, b=b, K=K, link=link),
6 list(
7 # morpheus
8 function(fargs) {
9 library(morpheus)
10 K <- fargs$K
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 # ,
22 # # flexmix
23 # function(fargs) {
24 # library(flexmix)
25 # source("../patch_Bettina/FLXMRglm.R")
26 # K <- fargs$K
27 # dat <- as.data.frame( cbind(fargs$Y,fargs$X) )
28 # res2 <- NULL
29 # tryCatch({
30 # fm <- flexmix( cbind(V1, 1-V1) ~ .-V1, data=dat, k=K,
31 # model = FLXMRglm(family = binomial(link = link)) )
32 # p <- mean(fm@posterior[["scaled"]][,1])
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(p, 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 #model = binomial; 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 K <- 2
91 p <- .5
92 b <- c(-.2, .5)
93 beta <- matrix( c(1,-2, 3,1), ncol=K )
94 } else if (d == 5) {
95 K <- 2
96 p <- .5
97 b <- c(-.2, .5)
98 beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=K )
99 } else if (d == 10) {
100 K <- 3
101 p <- c(.3, .3)
102 b <- c(-.2, 0, .5)
103 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=K )
104 } else if (d == 20) {
105 K <- 3
106 p <- c(.3, .3)
107 b <- c(-.2, 0, .5)
108 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,1,2,-1,0,3,4,-1,-3,0,2, 2,-3,0,1,0,-1,-4,3,2,0,1,1,2,2,-2,-2,3,1,0,0), ncol=K )
109 }
110
111 mr <- optimBeta(N, n, K, p, beta, b, link, ncores)
112 mr_params <- list("N"=N, "nc"=ncores, "n"=n, "K"=K, "d"=d, "link"=link,
113 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b)
114
115 save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,".RData",sep=""))