Fix default weights, fix reporting scripts
[morpheus.git] / reports / accuracy.R
CommitLineData
98b8a5dd 1optimBeta <- function(N, n, K, p, beta, b, link, weights, ncores)
1d014a86
BA
2{
3 library(morpheus)
4 res <- multiRun(
98b8a5dd 5 list(n=n, p=p, beta=beta, b=b, optargs=list(K=K, link=link, weights=weights)),
1d014a86
BA
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)
0ad4c8de
BA
14 op <- optimParams(K,fargs$optargs$link,fargs$optargs)
15 x_init <- list(p=rep(1/K,K-1), beta=mu, b=rep(0,K))
9007ccc1 16 tryCatch({
0ad4c8de 17 res2 <- do.call(rbind, op$run(x_init))
9007ccc1
BA
18 }, error = function(e) {
19 res2 <- NA
20 })
21 res2
22 }
23# ,
24# # flexmix
25# function(fargs) {
26# library(flexmix)
27# source("../patch_Bettina/FLXMRglm.R")
28# K <- fargs$optargs$K
29# dat <- as.data.frame( cbind(fargs$Y,fargs$X) )
30# res2 <- NULL
31# tryCatch({
32# fm <- flexmix( cbind(V1, 1-V1) ~ .-V1, data=dat, k=K,
33# model = FLXMRglm(family = binomial(link = link)) )
34# p <- mean(fm@posterior[["scaled"]][,1])
35# out <- refit(fm)
36# beta_b <- sapply( seq_len(K), function(i) {
37# as.double( out@components[[1]][[i]][,1] )
38# } )
39# res2 <- rbind(p, beta_b[2:nrow(beta_b),], beta_b[1,])
40# }, error = function(e) {
41# res2 <- NA
42# })
43# res2
44# }
45 ),
1d014a86
BA
46 prepareArgs = function(fargs, index) {
47 library(morpheus)
48 io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$optargs$link)
49 fargs$X = io$X
50 fargs$Y = io$Y
51 fargs$optargs$K = ncol(fargs$beta)
1d014a86
BA
52 fargs
53 }, N=N, ncores=ncores, verbose=TRUE)
54 p <- c(p, 1-sum(p))
9007ccc1
BA
55 for (i in 1:length(res)) {
56 for (j in N:1) {
57 if (is.null(res[[i]][[j]]) || is.na(res[[i]][[j]]))
58 res[[i]][[j]] <- NULL
59 }
60 print(paste("Count valid runs for ",i," = ",length(res[[i]]),sep=""))
1d014a86 61 res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact")
9007ccc1 62 }
1d014a86
BA
63 res
64}
65
66#model = binomial; default values:
67link = "logit"
68N <- 10
69d <- 2
70n <- 1e4
71ncores <- 1
b46623ad 72strw <- "1-1-1"
98b8a5dd 73weights <- c(1,1,1)
1d014a86
BA
74
75cmd_args <- commandArgs()
76for (arg in cmd_args)
77{
78 if (substr(arg,1,1)!='-') {
79 spl <- strsplit(arg,'=')[[1]]
80 if (spl[1] == "nc") {
81 ncores <- as.integer(spl[2])
82 } else if (spl[1] == "N") {
83 N <- as.integer(spl[2])
84 } else if (spl[1] == "n") {
85 n <- as.integer(spl[2])
86 } else if (spl[1] == "d") {
87 d <- as.integer(spl[2])
88 } else if (spl[1] == "link") {
89 link <- spl[2]
98b8a5dd 90 } else if (spl[1] == "weights") {
b46623ad
BA
91 strw <- spl[2]
92 weights <- as.numeric(unlist(strsplit(spl[2], ",")))
1d014a86
BA
93 }
94 }
95}
96
97if (d == 2) {
98 K <- 2
99 p <- .5
100 b <- c(-.2, .5)
101 beta <- matrix( c(1,-2, 3,1), ncol=K )
102} else if (d == 5) {
103 K <- 2
104 p <- .5
105 b <- c(-.2, .5)
106 beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=K )
107} else if (d == 10) {
108 K <- 3
109 p <- c(.3, .3)
110 b <- c(-.2, 0, .5)
111 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 )
112} else if (d == 20) {
113 K <- 3
114 p <- c(.3, .3)
115 b <- c(-.2, 0, .5)
116 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 )
117}
118
98b8a5dd 119mr <- optimBeta(N, n, K, p, beta, b, link, weights, ncores)
0ad4c8de 120mr_params <- list("N"=N, "nc"=ncores, "n"=n, "K"=K, "d"=d, "link"=link,
98b8a5dd 121 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "weights"=weights)
1d014a86 122
b46623ad 123save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",strw,".RData",sep=""))