1 #' Generate a sample of (X,Y) of size n
2 #' @param meanX matrix of group means for covariates (of size p*K)
3 #' @param covX covariance for covariates (of size p*p*K)
4 #' @param covY covariance for the response vector (of size m*m*K)
5 #' @param pi proportion for each cluster
6 #' @param beta regression matrix
7 #' @param n sample size
9 #' @return list with X and Y
11 generateXY = function(meanX, covX, covY, pi, beta, n)
17 X = matrix(nrow=n,ncol=p)
18 Y = matrix(nrow=n,ncol=m)
20 require(MASS) #simulate from a multivariate normal distribution
23 class = sample(1:k, 1, prob=pi)
24 X[i,] = mvrnorm(1, meanX[,class], covX[,,class])
25 Y[i,] = mvrnorm(1, X[i,] %*% beta[,,class], covY[,,class])
28 return (list(X=X,Y=Y))
31 #' Generate a sample of (X,Y) of size n with default values
32 #' @param n sample size
33 #' @param p number of covariates
34 #' @param m size of the response
35 #' @param k number of clusters
36 #' @return list with X and Y
38 generateXYdefault = function(n, p, m, k)
41 meanX = rangeX * matrix(1 - 2*runif(p*k), ncol=k)
42 covX = array(dim=c(p,p,k))
43 covY = array(dim=c(m,m,k))
50 #initialize beta to a random number of non-zero random value
51 beta = array(0, dim=c(p,m,k))
54 nonZeroCount = sample(1:m, 1)
55 beta[j,1:nonZeroCount,] = matrix(runif(nonZeroCount*k), ncol=k)
58 sample_IO = generateXY(meanX, covX, covY, pi, beta, n)
59 return (list(X=sample_IO$X,Y=sample_IO$Y))
62 #' Initialize the parameters in a basic way (zero for the conditional mean, uniform for weights,
63 #' identity for covariance matrices, and uniformly distributed for the clustering)
64 #' @param n sample size
65 #' @param p number of covariates
66 #' @param m size of the response
67 #' @param k number of clusters
68 #' @return list with phiInit, rhoInit,piInit,gamInit
70 basicInitParameters = function(n,p,m,k)
72 phiInit = array(0, dim=c(p,m,k))
74 piInit = (1./k)*rep(1,k)
76 rhoInit = array(dim=c(m,m,k))
78 rhoInit[,,i] = diag(m)
80 gamInit = 0.1 * matrix(1, nrow=n, ncol=k)
81 R = sample(1:k, n, replace=TRUE)
84 gamInit = gamInit/sum(gamInit[1,])
86 return (list("phiInit" = phiInit, "rhoInit" = rhoInit, "piInit" = piInit, "gamInit" = gamInit))