From 9007ccc114c127211639e7c5b82495bc39803eb0 Mon Sep 17 00:00:00 2001
From: Benjamin Auder <benjamin@auder>
Date: Fri, 20 Sep 2019 15:22:30 +0200
Subject: [PATCH] update

---
 reports/accuracy.R     | 64 +++++++++++++++++++++++++++---------------
 reports/genfig.R       | 45 +++++++++++++++++++++++++++++
 reports/run_accu_cl.sh |  8 ++++++
 3 files changed, 95 insertions(+), 22 deletions(-)
 create mode 100644 reports/genfig.R

diff --git a/reports/accuracy.R b/reports/accuracy.R
index 2f35792..2381524 100644
--- a/reports/accuracy.R
+++ b/reports/accuracy.R
@@ -2,7 +2,7 @@ optimBeta <- function(N, n, K, p, beta, b, link, ncores)
 {
 	library(morpheus)
 	res <- multiRun(
-		list(n=n,p=p,beta=beta,b=b,optargs=list(K=K,link=link)),
+		list(n=n, p=p, beta=beta, b=b, optargs=list(K=K, link=link)),
 		list(
 			# morpheus
 			function(fargs) {
@@ -11,25 +11,39 @@ optimBeta <- function(N, n, K, p, beta, b, link, ncores)
 				M <- computeMoments(fargs$X, fargs$Y)
 				fargs$optargs$M <- M
 				mu <- computeMu(fargs$X, fargs$Y, fargs$optargs)
-				op <- optimParams(K,link,fargs$optargs)
-				x_init <- list(p=rep(1/K,K-1), beta=mu, b=rep(0,K))
-				do.call(rbind, op$run(x_init))
-			},
-			# flexmix
-			function(fargs) {
-				library(flexmix)
-				source("../patch_Bettina/FLXMRglm.R")
-				K <- fargs$optargs$K
-				dat <- as.data.frame( cbind(fargs$Y,fargs$X) )
-				fm <- flexmix( cbind(V1, 1-V1) ~ .-V1, data=dat, k=K,
-					model = FLXMRglm(family = binomial(link = link)) )
-				p <- mean(fm@posterior[["scaled"]][,1])
-				out <- refit(fm)
-				beta_b <- sapply( seq_len(K), function(i) {
-					as.double( out@components[[1]][[i]][,1] )
-				} )
-				rbind(p, beta_b[2:nrow(beta_b),], beta_b[1,])
-			} ),
+				res2 <- NULL
+				tryCatch({
+					op <- optimParams(K,link,fargs$optargs)
+					x_init <- list(p=rep(1/K,K-1), beta=mu, b=rep(0,K))
+					res2 <- do.call(rbind, op$run(x_init))
+				}, error = function(e) {
+					res2 <- NA
+				})
+				res2
+			}
+#			,
+#			# flexmix
+#			function(fargs) {
+#				library(flexmix)
+#				source("../patch_Bettina/FLXMRglm.R")
+#				K <- fargs$optargs$K
+#				dat <- as.data.frame( cbind(fargs$Y,fargs$X) )
+#				res2 <- NULL
+#				tryCatch({
+#					fm <- flexmix( cbind(V1, 1-V1) ~ .-V1, data=dat, k=K,
+#						model = FLXMRglm(family = binomial(link = link)) )
+#					p <- mean(fm@posterior[["scaled"]][,1])
+#					out <- refit(fm)
+#					beta_b <- sapply( seq_len(K), function(i) {
+#						as.double( out@components[[1]][[i]][,1] )
+#					} )
+#					res2 <- rbind(p, beta_b[2:nrow(beta_b),], beta_b[1,])
+#				}, error = function(e) {
+#					res2 <- NA
+#				})
+#				res2
+#			}
+		),
 		prepareArgs = function(fargs, index) {
 			library(morpheus)
 			io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$optargs$link)
@@ -40,8 +54,14 @@ optimBeta <- function(N, n, K, p, beta, b, link, ncores)
 			fargs
 		}, N=N, ncores=ncores, verbose=TRUE)
 	p <- c(p, 1-sum(p))
-	for (i in 1:2)
+	for (i in 1:length(res)) {
+		for (j in N:1) {
+			if (is.null(res[[i]][[j]]) || is.na(res[[i]][[j]]))
+				res[[i]][[j]] <- NULL
+		}
+		print(paste("Count valid runs for ",i," = ",length(res[[i]]),sep=""))
 		res[[i]] <- alignMatrices(res[[i]], ref=rbind(p,beta,b), ls_mode="exact")
+	}
 	res
 }
 
@@ -97,4 +117,4 @@ mr <- optimBeta(N, n, K, p, beta, b, link, ncores)
 mr_params <- list("N"=N, "n"=n, "K"=K, "d"=d, "link"=link,
 	"p"=c(p,1-sum(p)), "beta"=beta, "b"=b)
 
-save("mr", "mr_params", file=paste("multirun_",d,"_",link,".RData",sep=""))
+save("mr", "mr_params", file=paste("multirun_",n,"_",d,"_",link,".RData",sep=""))
diff --git a/reports/genfig.R b/reports/genfig.R
new file mode 100644
index 0000000..2edd546
--- /dev/null
+++ b/reports/genfig.R
@@ -0,0 +1,45 @@
+nvals <- c(5000,10000,100000,500000,1000000)
+for (link in c("logit","probit"))
+{
+	for (d in c(2,5,10))
+	{
+		par(mfrow=c(2,2), lwd=2, cex.axis=2, cex=2)
+		res <- list()
+		for (n in nvals)
+		{
+			load(paste("multirun_",n,"_",d,"_",link,".RData",sep=""))
+			res <- c(res, mr)
+		}
+		for (i in 1:2) #our, fm
+		{
+			for (j in 1:2) #beta, p,b
+			{
+				if (j == 1) #beta
+				{
+					for (dim in 1:d)
+					{
+						ypts <- c()
+						for (k in 1:5)
+							ypts <- c(ypts, mean(sapply(1:length(res[[k]][[i]]), function(x) mean((res[[k]][[i]][[x]][2:(d+1),dim] - mr_params$beta[,dim])^2))))
+						plot(nvals, ypts)
+						if (dim < d)
+							par(new=TRUE)
+					}
+				}
+				else #p + b
+				{
+					for (rowidx in c(1,d+2))
+					{
+						ypts <- c()
+						ref <- if (rowidx==1) { mr_params$p } else { mr_params$b }
+						for (k in 1:5)
+							ypts <- c(ypts, mean(sapply(1:length(res[[k]][[i]]), function(x) mean((res[[k]][[i]][[x]][rowidx,] - ref)^2))))
+						plot(nvals, ypts)
+						if (rowidx==1)
+							par(new=TRUE)
+					}
+				}
+			}
+		}
+	}
+}
diff --git a/reports/run_accu_cl.sh b/reports/run_accu_cl.sh
index d1d8e37..50b2844 100644
--- a/reports/run_accu_cl.sh
+++ b/reports/run_accu_cl.sh
@@ -17,3 +17,11 @@ for d in 2 5 10 20; do
 		R --slave --args N=1000 n=1e5 nc=15 d=$d link=$link <accuracy.R >out$d$link 2>&1
 	done
 done
+
+#for d in 2 5; do
+#	for n in 5000 10000 100000 500000 1000000; do
+#		for link in "logit" "probit"; do
+#			R --slave --args N=1000 n=$n nc=64 d=$d link=$link <accuracy.R >out_$n$link$d 2>&1
+#		done
+#	done
+#done
-- 
2.44.0