I'm simulating a random dataset for a mixed-model. For mixed-model to make sense, the data points within a cluster need to be correlated (ICC). How do I generate such data?
These are my variables:
- ID
- base (baseline, first measurement)
- half (second measurement)
- final (final measurement)
- control (control or experimental group?)
- cluster (factor variable showing which cluster the person belongs to)
And I need the three measurements nested within in each cluster to correlate.
N <- 200
ID <- seq(from = 1, to = N, by = 1)
control <- rbinom(n=N, size = 1, prob = .5)
cluster <- as.factor(rbinom(n=N, size = 2, prob = .5) + 1)
base <- rnorm(n=N,mean = 100,sd=15)
half <- base + rnorm(n=N, mean = 0, sd = 3) + control*tau_1
final <- half + control*(tau_2-tau_1) + rnorm(n=N, mean = 0, sd = 3)
df.wide <- data.frame(ID,control,age,cluster,base,half,final)
The code is based on what I write above + I add some noise, thus the
rnorm(n=N, mean = 0, sd = 3)
in the 2nd and 3rd measures.
How do I make the three measurements intracorrelate within the clusters?
Thank you.