Fix constructionModelesLassoMLE: phiInit was reshaped from array to matrix. Add examp...
[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)
bb64f5cb
BA
14 covY = diag(m)
15 ω = rep(1./k,k)
39062512 16 #initialize beta to a random number of non-zero random value
321e13a9 17 β = array(0, dim=c(p,m,k))
39062512
BA
18 for (j in 1:p)
19 {
20 nonZeroCount = sample(1:m, 1)
bb64f5cb
BA
21 if (nonZeroCount >= 2)
22 β[j,1:nonZeroCount,] = matrix(runif(nonZeroCount*k), ncol=k)
23 else
24 β[j,1,] = runif(k)
39062512
BA
25 }
26
bb64f5cb 27 sample_IO = generateXY(n, ω, meanX, β, covX, covY)
39062512 28 return (list(X=sample_IO$X,Y=sample_IO$Y))
ef67d338
BA
29}
30
321e13a9
BA
31#' Initialize the parameters in a basic way (zero for the conditional mean, uniform for
32#' weights, identity for covariance matrices, and uniformly distributed for the
33#' clustering)
34#'
ef67d338
BA
35#' @param n sample size
36#' @param p number of covariates
37#' @param m size of the response
38#' @param k number of clusters
321e13a9 39#'
ef67d338 40#' @return list with phiInit, rhoInit,piInit,gamInit
321e13a9 41#'
ef67d338
BA
42basicInitParameters = function(n,p,m,k)
43{
39062512
BA
44 phiInit = array(0, dim=c(p,m,k))
45
46 piInit = (1./k)*rep(1,k)
47
48 rhoInit = array(dim=c(m,m,k))
49 for (i in 1:k)
50 rhoInit[,,i] = diag(m)
51
52 gamInit = 0.1 * matrix(1, nrow=n, ncol=k)
53 R = sample(1:k, n, replace=TRUE)
54 for (i in 1:n)
55 gamInit[i,R[i]] = 0.9
56 gamInit = gamInit/sum(gamInit[1,])
57
321e13a9 58 return (list("phiInit"=phiInit, "rhoInit"=rhoInit, "piInit"=piInit, "gamInit"=gamInit))
ef67d338 59}