Adjustments for CRAN upload
[valse.git] / pkg / R / generateXY.R
CommitLineData
0ba1b11c 1#' generateXY
3453829e
BA
2#'
3#' Generate a sample of (X,Y) of size n
4#'
5#' @param n sample size
8b284010 6#' @param prop proportion for each cluster
3453829e
BA
7#' @param meanX matrix of group means for covariates (of size p)
8#' @param covX covariance for covariates (of size p*p)
1196a43d 9#' @param beta regression matrix, of size p*m*k
8b284010 10#' @param covY covariance for the response vector (of size m*m)
3453829e 11#'
6af1d489 12#' @return list with X (of size n*p) and Y (of size n*m)
3453829e
BA
13#'
14#' @export
8b284010 15generateXY <- function(n, prop, meanX, beta, covX, covY)
3453829e
BA
16{
17 p <- dim(covX)[1]
18 m <- dim(covY)[1]
8b284010 19 k <- dim(beta)[3]
3453829e
BA
20
21 X <- matrix(nrow = 0, ncol = p)
22 Y <- matrix(nrow = 0, ncol = m)
23
24 # random generation of the size of each population in X~Y (unordered)
8b284010 25 sizePop <- stats::rmultinom(1, n, prop)
3453829e
BA
26 class <- c() #map i in 1:n --> index of class in 1:k
27
28 for (i in 1:k)
29 {
30 class <- c(class, rep(i, sizePop[i]))
31 newBlockX <- MASS::mvrnorm(sizePop[i], meanX, covX)
32 X <- rbind(X, newBlockX)
0ba1b11c 33 Y <- rbind(Y, t(apply(newBlockX, 1, function(row) MASS::mvrnorm(1, row %*%
8b284010 34 beta[, , i], covY[,]))))
3453829e
BA
35 }
36
37 shuffle <- sample(n)
38 list(X = X[shuffle, ], Y = Y[shuffle, ], class = class[shuffle])
39}