From: Benjamin Auder Date: Thu, 23 Mar 2017 16:00:39 +0000 (+0100) Subject: re-indent and remove prints in generateSampleInputs X-Git-Url: https://git.auder.net/css/vendor/doc/current/gitweb.js?a=commitdiff_plain;h=390625126a7ca58dafd2b4834f2f1d7a527d019f;p=valse.git re-indent and remove prints in generateSampleInputs --- diff --git a/pkg/R/generateSampleInputs.R b/pkg/R/generateSampleInputs.R index 4da1ea5..c7aa3c6 100644 --- a/pkg/R/generateSampleInputs.R +++ b/pkg/R/generateSampleInputs.R @@ -10,25 +10,23 @@ #' @export generateXY = function(meanX, covX, covY, pi, beta, n) { - p = dim(covX)[1] - m = dim(covY)[1] - k = dim(covY)[3] - - X = matrix(nrow=n,ncol=p) - Y = matrix(nrow=n,ncol=m) - class = matrix(nrow = n) - - require(MASS) #simulate from a multivariate normal distribution - for (i in 1:n) - { - class[i] = sample(1:k, 1, prob=pi) - X[i,] = mvrnorm(1, meanX, covX) - print(X[i,]) - print(beta[,,class[i]]) - Y[i,] = mvrnorm(1, X[i,] %*% beta[,,class[i]], covY[,,class[i]]) - } - - return (list(X=X,Y=Y, class = class)) + p = dim(covX)[1] + m = dim(covY)[1] + k = dim(covY)[3] + + X = matrix(nrow=n,ncol=p) + Y = matrix(nrow=n,ncol=m) + class = matrix(nrow = n) + + require(MASS) #simulate from a multivariate normal distribution + for (i in 1:n) + { + class[i] = sample(1:k, 1, prob=pi) + X[i,] = mvrnorm(1, meanX, covX) + Y[i,] = mvrnorm(1, X[i,] %*% beta[,,class[i]], covY[,,class[i]]) + } + + return (list(X=X,Y=Y, class = class)) } #' Generate a sample of (X,Y) of size n with default values @@ -40,24 +38,24 @@ generateXY = function(meanX, covX, covY, pi, beta, n) #' @export generateXYdefault = function(n, p, m, k) { - meanX = rep(0, p) - covX = diag(p) - covY = array(dim=c(m,m,k)) - for(r in 1:k) - { - covY[,,r] = diag(m) - } - pi = rep(1./k,k) - #initialize beta to a random number of non-zero random value - beta = array(0, dim=c(p,m,k)) - for (j in 1:p) - { - nonZeroCount = sample(1:m, 1) - beta[j,1:nonZeroCount,] = matrix(runif(nonZeroCount*k), ncol=k) - } - - sample_IO = generateXY(meanX, covX, covY, pi, beta, n) - return (list(X=sample_IO$X,Y=sample_IO$Y)) + meanX = rep(0, p) + covX = diag(p) + covY = array(dim=c(m,m,k)) + for(r in 1:k) + { + covY[,,r] = diag(m) + } + pi = rep(1./k,k) + #initialize beta to a random number of non-zero random value + beta = array(0, dim=c(p,m,k)) + for (j in 1:p) + { + nonZeroCount = sample(1:m, 1) + beta[j,1:nonZeroCount,] = matrix(runif(nonZeroCount*k), ncol=k) + } + + sample_IO = generateXY(meanX, covX, covY, pi, beta, n) + return (list(X=sample_IO$X,Y=sample_IO$Y)) } #' Initialize the parameters in a basic way (zero for the conditional mean, uniform for weights, @@ -70,19 +68,19 @@ generateXYdefault = function(n, p, m, k) #' @export basicInitParameters = function(n,p,m,k) { - phiInit = array(0, dim=c(p,m,k)) - - piInit = (1./k)*rep(1,k) - - rhoInit = array(dim=c(m,m,k)) - for (i in 1:k) - rhoInit[,,i] = diag(m) - - gamInit = 0.1 * matrix(1, nrow=n, ncol=k) - R = sample(1:k, n, replace=TRUE) - for (i in 1:n) - gamInit[i,R[i]] = 0.9 - gamInit = gamInit/sum(gamInit[1,]) - - return (list("phiInit" = phiInit, "rhoInit" = rhoInit, "piInit" = piInit, "gamInit" = gamInit)) + phiInit = array(0, dim=c(p,m,k)) + + piInit = (1./k)*rep(1,k) + + rhoInit = array(dim=c(m,m,k)) + for (i in 1:k) + rhoInit[,,i] = diag(m) + + gamInit = 0.1 * matrix(1, nrow=n, ncol=k) + R = sample(1:k, n, replace=TRUE) + for (i in 1:n) + gamInit[i,R[i]] = 0.9 + gamInit = gamInit/sum(gamInit[1,]) + + return (list("phiInit" = phiInit, "rhoInit" = rhoInit, "piInit" = piInit, "gamInit" = gamInit)) }