basic test OK, but problem with 'fast' EMGLLF output in last EMilie test
[valse.git] / test / helper.R
CommitLineData
ef67d338 1#' Generate a sample of (X,Y) of size n with default values
321e13a9 2#'
ef67d338
BA
3#' @param n sample size
4#' @param p number of covariates
5#' @param m size of the response
6#' @param k number of clusters
321e13a9 7#'
ef67d338 8#' @return list with X and Y
321e13a9 9#'
ef67d338
BA
10generateXYdefault = function(n, p, m, k)
11{
39062512
BA
12 meanX = rep(0, p)
13 covX = diag(p)
14 covY = array(dim=c(m,m,k))
15 for(r in 1:k)
39062512 16 covY[,,r] = diag(m)
321e13a9 17 π = rep(1./k,k)
39062512 18 #initialize beta to a random number of non-zero random value
321e13a9 19 β = array(0, dim=c(p,m,k))
39062512
BA
20 for (j in 1:p)
21 {
22 nonZeroCount = sample(1:m, 1)
321e13a9 23 β[j,1:nonZeroCount,] = matrix(runif(nonZeroCount*k), ncol=k)
39062512
BA
24 }
25
321e13a9 26 sample_IO = generateXY(n, π, meanX, β, covX, covY)
39062512 27 return (list(X=sample_IO$X,Y=sample_IO$Y))
ef67d338
BA
28}
29
321e13a9
BA
30#' Initialize the parameters in a basic way (zero for the conditional mean, uniform for
31#' weights, identity for covariance matrices, and uniformly distributed for the
32#' clustering)
33#'
ef67d338
BA
34#' @param n sample size
35#' @param p number of covariates
36#' @param m size of the response
37#' @param k number of clusters
321e13a9 38#'
ef67d338 39#' @return list with phiInit, rhoInit,piInit,gamInit
321e13a9 40#'
ef67d338
BA
41basicInitParameters = function(n,p,m,k)
42{
39062512
BA
43 phiInit = array(0, dim=c(p,m,k))
44
45 piInit = (1./k)*rep(1,k)
46
47 rhoInit = array(dim=c(m,m,k))
48 for (i in 1:k)
49 rhoInit[,,i] = diag(m)
50
51 gamInit = 0.1 * matrix(1, nrow=n, ncol=k)
52 R = sample(1:k, n, replace=TRUE)
53 for (i in 1:n)
54 gamInit[i,R[i]] = 0.9
55 gamInit = gamInit/sum(gamInit[1,])
56
321e13a9 57 return (list("phiInit"=phiInit, "rhoInit"=rhoInit, "piInit"=piInit, "gamInit"=gamInit))
ef67d338 58}