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