Commit | Line | Data |
---|---|---|
ffdf9447 | 1 | #' generateXY |
086ca318 BA |
2 | #' |
3 | #' Generate a sample of (X,Y) of size n | |
4 | #' | |
5 | #' @param n sample size | |
6 | #' @param π proportion for each cluster | |
7 | #' @param meanX matrix of group means for covariates (of size p) | |
8 | #' @param covX covariance for covariates (of size p*p) | |
9 | #' @param β regression matrix, of size p*m*k | |
10 | #' @param covY covariance for the response vector (of size m*m*K) | |
11 | #' | |
12 | #' @return list with X and Y | |
13 | #' | |
14 | #' @export | |
ffdf9447 | 15 | generateXY <- function(n, π, meanX, β, covX, covY) |
086ca318 | 16 | { |
ffdf9447 BA |
17 | p <- dim(covX)[1] |
18 | m <- dim(covY)[1] | |
19 | k <- dim(covY)[3] | |
1b698c16 | 20 | |
ffdf9447 BA |
21 | X <- matrix(nrow = 0, ncol = p) |
22 | Y <- matrix(nrow = 0, ncol = m) | |
1b698c16 | 23 | |
ffdf9447 BA |
24 | # random generation of the size of each population in X~Y (unordered) |
25 | sizePop <- rmultinom(1, n, π) | |
1b698c16 BA |
26 | class <- c() #map i in 1:n --> index of class in 1:k |
27 | ||
ffdf9447 BA |
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) | |
33 | Y <- rbind(Y, t(apply(newBlockX, 1, function(row) MASS::mvrnorm(1, row %*% | |
34 | β[, , i], covY[, , i])))) | |
35 | } | |
1b698c16 | 36 | |
ffdf9447 BA |
37 | shuffle <- sample(n) |
38 | list(X = X[shuffle, ], Y = Y[shuffle, ], class = class[shuffle]) | |
086ca318 | 39 | } |