Skip to content

Instantly share code, notes, and snippets.

@JWiley
Created February 17, 2012 03:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JWiley/1850238 to your computer and use it in GitHub Desktop.
Save JWiley/1850238 to your computer and use it in GitHub Desktop.
function to simulate some binary data
bsim <- function(reps = 10^2, groups = 200, L1k = 6, L2k = 3, seed = 10) {
stopifnot(all.equal(L1k %% L2k, 0L))
f <- function(x) {
mdiff <- min(x, na.rm = TRUE) - 0
(x - mdiff)/(max(x, na.rm = TRUE) - mdiff)
}
N <- reps * groups
G <- matrix(rep(1:groups, reps))
set.seed(seed)
W <- prcomp(apply(matrix(rnorm(groups * L2k), ncol = L2k), 2, scale))$x[G, ]
colnames(W) <- paste("W", 1:L2k, sep = '')
X <- prcomp(apply(matrix(rnorm(N * L1k), ncol = L1k), 2, scale))$x
X <- X + W[, rep(1:L2k, each = L1k/L2k)]
colnames(X) <- paste("X", 1:L1k, sep = '')
Y <- rbinom(N, 1, plogis(rowSums(cbind(X, W)) + .05 * (1:groups - mean(groups))[G]))
return(list(Y = Y, X = X, W = W, G = G))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment