Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CreateClusteredData <- function(num.groups, sample.size, beta, randomize.by.group = FALSE){
individual <- 1:sample.size # index for individuals
group <- sample(1:num.groups, size = sample.size, replace = TRUE) # randomly assign everyone to a group
group.effect <- rnorm(num.groups) # create a group-specific effect
eta <- group.effect[group] # give each individual their group-specific effect
epsilon <- rnorm(sample.size) # given each individual an individual-specific effect
if (randomize.by.group){
group.trt.assignment <- rbinom(num.groups, 1, 0.5)
trt <- group.trt.assignment[group]
} else {
trt <- rbinom(sample.size, 1, 0.5)
}
y <- beta * trt + epsilon + eta
data.frame(y, individual, group, trt, epsilon, eta)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment