簡體   English   中英

用 R package 模擬數據 SimCorMultRes 相關二進制數據

[英]Simulate data with R package SimCorMultRes for correlated binary data

我正在用 R package SimCorMultRes 模擬數據。 下面是我的代碼,適用於簇大小 5 和 9。現在我想對簇大小 100 執行相同的操作。寫下 p1、p2、...、p99 是不現實的。 我只是想知道是否有人可以幫助我。

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")

我想辦法做到

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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM