From: Benjamin Auder Date: Tue, 17 Dec 2019 12:55:12 +0000 (+0100) Subject: Fix reporting scripts + reset W to identity at each OptimParams() X-Git-Url: https://git.auder.net/doc/html/app_dev.php/current/%7B%7B?a=commitdiff_plain;h=86f257f88727805ee73f69e2b9ad57bea2f40b82;p=morpheus.git Fix reporting scripts + reset W to identity at each OptimParams() --- diff --git a/pkg/R/optimParams.R b/pkg/R/optimParams.R index 894aebd..5a88ed9 100644 --- a/pkg/R/optimParams.R +++ b/pkg/R/optimParams.R @@ -102,7 +102,7 @@ setRefClass( n <<- nrow(X) d <<- ncol(X) - W <<- diag(d+d^2+d^3) #initialize at W = Identity + # W will be initialized when calling run() }, expArgs = function(v) @@ -264,6 +264,10 @@ setRefClass( θ0$b = rep(0, K) else if (!is.numeric(θ0$b) || length(θ0$b) != K || any(is.na(θ0$b))) stop("θ0$b: length K, no NA") + + # (Re)Set W to identity, to allow several run from the same object + W <<- diag(d+d^2+d^3) + # TODO: stopping condition? N iterations? Delta <= epsilon ? loopMax <- 2 for (loop in 1:loopMax) diff --git a/reports/accuracy.R b/reports/accuracy.R index fd22a31..d628436 100644 --- a/reports/accuracy.R +++ b/reports/accuracy.R @@ -1,13 +1,13 @@ -optimBeta <- function(N, n, K, p, beta, b, link, ncores) +optimBeta <- function(N, n, p, beta, b, link, ncores) { library(morpheus) res <- multiRun( - list(n=n, p=p, beta=beta, b=b, K=K, link=link), + list(n=n, p=p, beta=beta, b=b, link=link), list( # morpheus function(fargs) { library(morpheus) - K <- fargs$K + K <- ncol(fargs$beta) M <- computeMoments(fargs$X, fargs$Y) mu <- computeMu(fargs$X, fargs$Y, list(K=K, M=M)) op <- optimParams(fargs$X, fargs$Y, K, fargs$link, M) @@ -23,7 +23,7 @@ optimBeta <- function(N, n, K, p, beta, b, link, ncores) # function(fargs) { # library(flexmix) # source("../patch_Bettina/FLXMRglm.R") -# K <- fargs$K +# K <- ncol(fargs$beta) # dat <- as.data.frame( cbind(fargs$Y,fargs$X) ) # res2 <- NULL # tryCatch({ @@ -60,7 +60,7 @@ optimBeta <- function(N, n, K, p, beta, b, link, ncores) res } -#model = binomial; default values: +# Default values: link = "logit" N <- 10 d <- 2 @@ -87,28 +87,20 @@ for (arg in cmd_args) } if (d == 2) { - K <- 2 p <- .5 b <- c(-.2, .5) - beta <- matrix( c(1,-2, 3,1), ncol=K ) + beta <- matrix( c(1,-2, 3,1), ncol=2 ) } else if (d == 5) { - K <- 2 p <- .5 b <- c(-.2, .5) - beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=K ) + beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=2 ) } else if (d == 10) { - K <- 3 p <- c(.3, .3) b <- c(-.2, 0, .5) - 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 ) -} else if (d == 20) { - K <- 3 - p <- c(.3, .3) - b <- c(-.2, 0, .5) - 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 ) + 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 ) } -mr <- optimBeta(N, n, K, p, beta, b, link, ncores) +mr <- optimBeta(N, n, p, beta, b, link, ncores) mr_params <- list("N"=N, "nc"=ncores, "n"=n, "K"=K, "d"=d, "link"=link, "p"=c(p,1-sum(p)), "beta"=beta, "b"=b) diff --git a/reports/multistart.R b/reports/multistart.R index 6804b6b..7cc0a5e 100644 --- a/reports/multistart.R +++ b/reports/multistart.R @@ -1,15 +1,15 @@ library(morpheus) -testMultistart <- function(N, n, d, K, p, beta, b, link, nstart, ncores) +testMultistart <- function(N, n, p, beta, b, link, nstart, ncores) { res <- multiRun( - list(n=n,p=p,beta=beta,b=b,optargs=list(K=K,d=d,link=link,nstart=nstart)), + list(n=n, p=p, beta=beta, b=b, link=link, nstart=nstart), list( function(fargs) { # 1 start library(morpheus) - K <- fargs$optargs$K - op <- optimParams(K, fargs$optargs$link, fargs$optargs) + K <- ncol(fargs$beta) + op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M) x_init <- list(p=rep(1/K,K-1), beta=fargs$mu, b=rep(0,K)) res2 <- NULL tryCatch({ @@ -20,12 +20,12 @@ testMultistart <- function(N, n, d, K, p, beta, b, link, nstart, ncores) function(fargs) { # B starts library(morpheus) - K <- fargs$optargs$K - d <- fargs$optargs$d - op <- optimParams(K, fargs$optargs$link, fargs$optargs) + K <- ncol(fargs$beta) + d <- nrow(fargs$beta) + op <- optimParams(fargs$X, fargs$Y, K, fargs$link, fargs$M) best_val <- Inf best_par <- list() - for (i in 1:fargs$optargs$nstart) + for (i in 1:fargs$nstart) { #x_init <- list(p=rep(1/K,K-1), beta=i*fargs$mu, b=rep(0,K)) M <- matrix(rnorm(d*K), nrow=d, ncol=K) @@ -51,10 +51,12 @@ testMultistart <- function(N, n, d, K, p, beta, b, link, nstart, ncores) ), prepareArgs = function(fargs, index) { library(morpheus) - io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$optargs$link) - fargs$optargs$M <- computeMoments(io$X, io$Y) - mu <- computeMu(io$X, io$Y, fargs$optargs) + io = generateSampleIO(fargs$n, fargs$p, fargs$beta, fargs$b, fargs$link) + fargs$M <- computeMoments(io$X, io$Y) + mu <- computeMu(io$X, io$Y, list(M=fargs$M)) fargs$mu <- mu + fargs$X <- io$X + fargs$Y <- io$Y fargs }, N=N, ncores=ncores, verbose=TRUE) for (i in 1:2) @@ -62,14 +64,9 @@ testMultistart <- function(N, n, d, K, p, beta, b, link, nstart, ncores) res } -#model = binomial -K <- 2 -p <- .5 -b <- c(-.2, .5) # Default values: link = "logit" N <- 10 -d <- 2 n <- 1e4 ncores <- 1 nstart <- 3 #nstart-1 random starting points for each MC run @@ -95,14 +92,23 @@ for (arg in cmd_args) } } } -betas <- list( - matrix( c(1,-2, 3,1), ncol=K ), #d=2 - matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=K ), #d=5 - matrix( c(1,2,-1,0,3,4,-1,-3,0,2, 2,-3,0,1,0,-1,-4,3,2,0), ncol=K ) ) #d=10 -beta <- betas[[ ifelse( d==2, 1, ifelse(d==5,2,3) ) ]] -mr <- testMultistart(N, n, d, K, p, beta, b, link, nstart, ncores) -mr_params <- list("N"=N, "nc"=ncores, "n"=n, "K"=K, "d"=d, "link"=link, +if (d == 2) { + p <- .5 + b <- c(-.2, .5) + beta <- matrix( c(1,-2, 3,1), ncol=2 ) +} else if (d == 5) { + p <- .5 + b <- c(-.2, .5) + beta <- matrix( c(1,2,-1,0,3, 2,-3,0,1,0), ncol=2 ) +} else if (d == 10) { + p <- c(.3, .3) + b <- c(-.2, 0, .5) + 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 ) +} + +mr <- testMultistart(N, n, p, beta, b, link, nstart, ncores) +mr_params <- list("N"=N, "nc"=ncores, "n"=n, "link"=link, "p"=c(p,1-sum(p)), "beta"=beta, "b"=b, "nstart"=nstart) save("mr", "mr_params", file=paste("res_",n,"_",d,"_",link,"_",nstart,".RData",sep=""))