downgrade roxygen2 to v5.0.1, rename EMGLLF -> EMGLLF_R
[valse.git] / pkg / R / generateSampleInputs.R
1 #' Generate a sample of (X,Y) of size n
2 #' @param meanX matrix of group means for covariates (p x K)
3 #' @param covX covariance for covariates (p x p x K)
4 #' @param covY covariance for the response vector (m x m x K)
5 #' @param pi proportion for each cluster
6 #' @param beta regression matrix, of size p*m*k
7 #' @param n sample size
8 #'
9 #' @return list with X and Y
10 #' @export
11 generateXY = function(meanX, covX, covY, pi, beta, n)
12 {
13 p = dim(covX)[1]
14 m = dim(covY)[1]
15 k = dim(covY)[3]
16
17 X = matrix(nrow=n,ncol=p)
18 Y = matrix(nrow=n,ncol=m)
19 class = matrix(nrow = n)
20
21 require(MASS) #simulate from a multivariate normal distribution
22 for (i in 1:n)
23 {
24 class[i] = sample(1:k, 1, prob=pi)
25 X[i,] = mvrnorm(1, meanX[,class[i]], covX[,,class[i]])
26 Y[i,] = mvrnorm(1, X[i,] %*% beta[,,class[i]], covY[,,class[i]])
27 }
28
29 return (list(X=X,Y=Y, class = class))
30 }
31
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
38 #' @export
39 generateXYdefault = function(n, p, m, k)
40 {
41 rangeX = 100
42 meanX = rangeX * matrix(1 - 2*runif(p*k), ncol=k)
43 covX = array(dim=c(p,p,k))
44 covY = array(dim=c(m,m,k))
45 for(r in 1:k)
46 {
47 covX[,,r] = diag(p)
48 covY[,,r] = diag(m)
49 }
50 pi = rep(1./k,k)
51 #initialize beta to a random number of non-zero random value
52 beta = array(0, dim=c(p,m,k))
53 for (j in 1:p)
54 {
55 nonZeroCount = sample(1:m, 1)
56 beta[j,1:nonZeroCount,] = matrix(runif(nonZeroCount*k), ncol=k)
57 }
58
59 sample_IO = generateXY(meanX, covX, covY, pi, beta, n)
60 return (list(X=sample_IO$X,Y=sample_IO$Y))
61 }
62
63 #' Initialize the parameters in a basic way (zero for the conditional mean, uniform for weights,
64 #' identity for covariance matrices, and uniformly distributed for the clustering)
65 #' @param n sample size
66 #' @param p number of covariates
67 #' @param m size of the response
68 #' @param k number of clusters
69 #' @return list with phiInit, rhoInit,piInit,gamInit
70 #' @export
71 basicInitParameters = function(n,p,m,k)
72 {
73 phiInit = array(0, dim=c(p,m,k))
74
75 piInit = (1./k)*rep(1,k)
76
77 rhoInit = array(dim=c(m,m,k))
78 for (i in 1:k)
79 rhoInit[,,i] = diag(m)
80
81 gamInit = 0.1 * matrix(1, nrow=n, ncol=k)
82 R = sample(1:k, n, replace=TRUE)
83 for (i in 1:n)
84 gamInit[i,R[i]] = 0.9
85 gamInit = gamInit/sum(gamInit[1,])
86
87 return (list("phiInit" = phiInit, "rhoInit" = rhoInit, "piInit" = piInit, "gamInit" = gamInit))
88 }