Created
December 9, 2015 10:33
-
-
Save johnjosephhorton/53318f40b1d7f1f24543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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