Fix accuracy.R + add postTreatment draft
[morpheus.git] / reports / accuracy.R
CommitLineData
86f257f8 1optimBeta <- function(N, n, p, beta, b, link, ncores)
1d014a86 2{
f4e42a2b
BA
3 library(morpheus)
4 res <- multiRun(
86f257f8 5 list(n=n, p=p, beta=beta, b=b, link=link),
f4e42a2b
BA
6 list(
7 # morpheus
8 function(fargs) {
9 library(morpheus)
86f257f8 10 K <- ncol(fargs$beta)
f4e42a2b
BA
11 M <- computeMoments(fargs$X, fargs$Y)
12 mu <- computeMu(fargs$X, fargs$Y, list(K=K, M=M))
5af71d43 13 op <- optimParams(fargs$X, fargs$Y, K, fargs$link, M, 1) #only 1 OpenMP core
0ad4c8de 14 x_init <- list(p=rep(1/K,K-1), beta=mu, b=rep(0,K))
f4e42a2b
BA
15 res2 <- NULL
16 tryCatch({
0ad4c8de 17 res2 <- do.call(rbind, op$run(x_init))
f4e42a2b
BA
18 }, error = function(e) {})
19 res2
07f2d045
BA
20 }
21 ,
38c65767
BA
22 # flexmix
23 function(fargs) {
24 library(flexmix)
25 source("../patch_Bettina/FLXMRglm.R")
26 K <- ncol(fargs$beta)
27 dat <- as.data.frame( cbind(fargs$Y,fargs$X) )
28 res2 <- NULL
29 tryCatch({
b4aba3ce 30 fm <- flexmix( cbind(V1, 1-V1) ~ ., data=dat, k=K,
38c65767 31 model = FLXMRglm(family = binomial(link = link)) )
b4aba3ce 32 pf <- colMeans(fm@posterior[["scaled"]])
38c65767
BA
33 out <- refit(fm)
34 beta_b <- sapply( seq_len(K), function(i) {
35 as.double( out@components[[1]][[i]][,1] )
36 } )
0e0fa9ff 37 res2 <- rbind(pf, beta_b[2:nrow(beta_b),], beta_b[1,])
38c65767
BA
38 }, error = function(e) {
39 res2 <- NA
40 })
41 res2
f4e42a2b 42 }
f4e42a2b
BA
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
1d014a86
BA
61}
62
86f257f8 63# Default values:
1d014a86
BA
64link = "logit"
65N <- 10
66d <- 2
67n <- 1e4
68ncores <- 1
69
70cmd_args <- commandArgs()
71for (arg in cmd_args)
72{
f4e42a2b
BA
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 }
1d014a86
BA
87}
88
89if (d == 2) {
f4e42a2b
BA
90 p <- .5
91 b <- c(-.2, .5)
86f257f8 92 beta <- matrix( c(1,-2, 3,1), ncol=2 )
1d014a86 93} else if (d == 5) {
f4e42a2b
BA
94 p <- .5
95 b <- c(-.2, .5)
86f257f8 96 beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=2 )
1d014a86 97} else if (d == 10) {
f4e42a2b
BA
98 p <- c(.3, .3)
99 b <- c(-.2, 0, .5)
86f257f8 100 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 )
1d014a86
BA
101}
102
86f257f8 103mr <- optimBeta(N, n, p, beta, b, link, ncores)
38c65767 104mr_params <- list("N"=N, "nc"=ncores, "n"=n, "link"=link,
f4e42a2b 105 "p"=c(p,1-sum(p)), "beta"=beta, "b"=b)
1d014a86 106
074c721a 107save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,".RData",sep=""))