update Emilie
[valse.git] / pkg / R / generateXY.R
index 496d4e5..fde4b0f 100644 (file)
@@ -3,37 +3,37 @@
 #' Generate a sample of (X,Y) of size n
 #'
 #' @param n sample size
-#' @param π proportion for each cluster
+#' @param prop proportion for each cluster
 #' @param meanX matrix of group means for covariates (of size p)
 #' @param covX covariance for covariates (of size p*p)
-#' @param β regression matrix, of size p*m*k
-#' @param covY covariance for the response vector (of size m*m*K)
+#' @param beta regression matrix, of size p*m*k
+#' @param covY covariance for the response vector (of size m*m)
 #'
 #' @return list with X and Y
 #'
 #' @export
-generateXY = function(n, π, meanX, β, covX, covY)
+generateXY <- function(n, prop, meanX, beta, covX, covY)
 {
-       p <- dim(covX)[1]
-       m <- dim(covY)[1]
-       k <- dim(covY)[3]
-       
-       X <- matrix(nrow=0, ncol=p)
-       Y <- matrix(nrow=0, ncol=m)
+  p <- dim(covX)[1]
+  m <- dim(covY)[1]
+  k <- dim(beta)[3]
 
-       #random generation of the size of each population in X~Y (unordered)
-       sizePop <- rmultinom(1, n, pi)
-       class <- c() #map i in 1:n --> index of class in 1:k
+  X <- matrix(nrow = 0, ncol = p)
+  Y <- matrix(nrow = 0, ncol = m)
 
-       for (i in 1:k)
-       {
-               class <- c(class, rep(i, sizePop[i]))
-               newBlockX <- MASS::mvrnorm(sizePop[i], meanX, covX)
-               X <- rbind( X, newBlockX )
-               Y <- rbind( Y, apply( newBlockX, 1, function(row)
-                       mvrnorm(1, row %*% beta[,,i], covY[,,i]) ) )
-       }
+  # random generation of the size of each population in X~Y (unordered)
+  sizePop <- stats::rmultinom(1, n, prop)
+  class <- c() #map i in 1:n --> index of class in 1:k
 
-       shuffle = sample(n)
-       list("X"=X[shuffle,], "Y"=Y[shuffle,], "class"=class[shuffle])
+  for (i in 1:k)
+  {
+    class <- c(class, rep(i, sizePop[i]))
+    newBlockX <- MASS::mvrnorm(sizePop[i], meanX, covX)
+    X <- rbind(X, newBlockX)
+    Y <- rbind(Y, t(apply(newBlockX, 1, function(row) MASS::mvrnorm(1, row %*%
+      beta[, , i], covY[,]))))
+  }
+
+  shuffle <- sample(n)
+  list(X = X[shuffle, ], Y = Y[shuffle, ], class = class[shuffle])
 }