[R] Creating clustered data with a random effect for a MC analysis

jjh21 jjharden at gmail.com
Fri Jan 23 19:35:27 CET 2009


I am trying to create a data set in R with a random cluster effect. I would
like to do it so I can set the intraclass correlation coefficient (ICC) in
the data generating process. I have tried a few methods, but cannot seem to
get enough control over the ICC. I have been estimating the ICC using the
"deff" command in the "Hmisc" package. The main problem is that when I
adjust the number of clusters, the ICC changes. Below is an example of one
approach I have tried to generate the data. Thanks for any help you can
give.



set.seed(82184)
n <- 1200 # Sample size
nc <- 50 # Number of clusters
p <- .1 # ICC
sims <- 100 # Number of Monte Carlo simulations

a <- 0 # True model coefficients
B1 <- 0.85
B2 <- 0.5

cluster <- rep(1:nc, each = n/nc) # Cluster label

  for (i in 1:sims){ # Simulate a clustered data set

  c.sigma <- matrix(c(4, 0, 0, p), ncol = 2) # Cluster-level random effects
  c.values <- rmvnorm(n = nc, sigma = c.sigma)
  randeff1 <- rep(c.values[ , 1], each = n/nc)
  randeff2 <- rep(c.values[ , 2], each = n/nc)

  i.sigma <- matrix(c(1, 0, 0, 1), ncol = 2) # Individual-level random
effects
  i.values <- rmvnorm(n = n, sigma = i.sigma)
  randeff3 <- i.values[ , 1]
  randeff4 <- i.values[ , 2]

  X1 <- 3 + randeff1 + randeff3 # X1 values unique to individual
observations
  X2 <- randeff1 # X2 values unique to clusters of observations
  epsilon <- randeff2 + randeff4 # Two components of the error term

  Y <- a + B1*X1 + B2*X2 + epsilon # True model
}
-- 
View this message in context: http://www.nabble.com/Creating-clustered-data-with-a-random-effect-for-a-MC-analysis-tp21630341p21630341.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list