Simulate data with R package SimCorMultRes for correlated binary data

Viewed 51

I am simulating data with R package SimCorMultRes. My code is below for cluster size 5 and 9. Now I want to do the same thing for cluster size 100. It's unrealistic to write down p1, p2, ..., p99. I am just wondering if anybody can help me.

library(SimCorMultRes)
cluster_size <- 5
x <- runif(1000*cluster_size)
p1 <- rep(c(1,0,0,0,0),1000)
p2 <- rep(c(0,1,0,0,0),1000)
p3 <- rep(c(0,0,1,0,0),1000)
p4 <- rep(c(0,0,0,1,0),1000)
beta_intercepts <- -3
beta_coefficients <- c(0,rnorm(4))
latent_correlation_matrix <- toeplitz(c(1, 0.2, 0.2, 0.2, 0.2))
# simulation of clustered binary responses
simulated_binary_dataset <- rbin(clsize = cluster_size, intercepts = beta_intercepts,
                                 betas = beta_coefficients, xformula = ~x+p1+p2+p3+p4, cor.matrix = latent_correlation_matrix,
                                 link = "logit")
library(SimCorMultRes)
cluster_size <- 9
x <- runif(1000*cluster_size)
p1 <- rep(c(1,rep(0,8)),1000)
p2 <- rep(c(0,1,rep(0,7)),1000)
p3 <- rep(c(rep(0,2),1,rep(0,6)),1000)
p4 <- rep(c(rep(0,3),1,rep(0,5)),1000)
p5 <- rep(c(rep(0,4),1,rep(0,4)),1000)
p6 <- rep(c(rep(0,5),1,rep(0,3)),1000)
p7 <- rep(c(rep(0,6),1,rep(0,2)),1000)
p8 <- rep(c(rep(0,7),1,rep(0,1)),1000)
beta_intercepts <- -3
beta_coefficients <- c(0,rnorm(8))
latent_correlation_matrix <- toeplitz(c(1, rep(0.2, 8)))
simulated_binary_dataset <- rbin(clsize = cluster_size, intercepts = beta_intercepts,
                                 betas = beta_coefficients, xformula = ~x+p1+p2+p3+p4+p5+p6+p7+p8, cor.matrix = latent_correlation_matrix,
                                 link = "logit")
1 Answers

I find a way to do it

library(SimCorMultRes)
cluster_size <- 9
size=1000
x <- runif(size*cluster_size)
p1 <- rep(c(1,rep(0,cluster_size-1)),size)
for (i in 1:(cluster_size-2)) {
  a <- paste0("p", i+1)
  assign(a, rep(c(rep(0,i),1,rep(0,cluster_size-1-i)),size))
}
PredictorVariables <- paste("p", 1:(cluster_size-1), sep="")
Formula <- formula(paste("~ x + ", 
                         paste(PredictorVariables, collapse=" + ")))
beta_intercepts <- -3
beta_coefficients <- c(0,rnorm(cluster_size-1))
latent_correlation_matrix <- toeplitz(c(1, rep(0.2, cluster_size-1)))
simulated_binary_dataset <- rbin(clsize = cluster_size, intercepts = beta_intercepts,
                                 betas = beta_coefficients, xformula = Formula, cor.matrix = latent_correlation_matrix,
                                 link = "logit")
Related