1 #' Generate a sample of (X,Y) of size n
2 #' @param meanX matrix of group means for covariates (of size p)
3 #' @param covX covariance for covariates (of size p*p)
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, of size p*m*k
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)
19 class = matrix(nrow = n)
21 require(MASS) #simulate from a multivariate normal distribution
24 class[i] = sample(1:k, 1, prob=pi)
25 X[i,] = mvrnorm(1, meanX, covX)
26 Y[i,] = mvrnorm(1, X[i,] %*% beta[,,class[i]], covY[,,class[i]])
29 return (list(X=X,Y=Y, class = class))
32 #' Generate a sample of (X,Y) of size n with default values
33 #' @param n sample size
34 #' @param p number of covariates
35 #' @param m size of the response
36 #' @param k number of clusters
37 #' @return list with X and Y
39 generateXYdefault = function(n, p, m, k)
43 covY = array(dim=c(m,m,k))
49 #initialize beta to a random number of non-zero random value
50 beta = array(0, dim=c(p,m,k))
53 nonZeroCount = sample(1:m, 1)
54 beta[j,1:nonZeroCount,] = matrix(runif(nonZeroCount*k), ncol=k)
57 sample_IO = generateXY(meanX, covX, covY, pi, beta, n)
58 return (list(X=sample_IO$X,Y=sample_IO$Y))
61 #' Initialize the parameters in a basic way (zero for the conditional mean, uniform for weights,
62 #' identity for covariance matrices, and uniformly distributed for the clustering)
63 #' @param n sample size
64 #' @param p number of covariates
65 #' @param m size of the response
66 #' @param k number of clusters
67 #' @return list with phiInit, rhoInit,piInit,gamInit
69 basicInitParameters = function(n,p,m,k)
71 phiInit = array(0, dim=c(p,m,k))
73 piInit = (1./k)*rep(1,k)
75 rhoInit = array(dim=c(m,m,k))
77 rhoInit[,,i] = diag(m)
79 gamInit = 0.1 * matrix(1, nrow=n, ncol=k)
80 R = sample(1:k, n, replace=TRUE)
83 gamInit = gamInit/sum(gamInit[1,])
85 return (list("phiInit" = phiInit, "rhoInit" = rhoInit, "piInit" = piInit, "gamInit" = gamInit))